I have two buttons that I want side by side with a little space between them:
I originally did this by using display: flex; and justify-content: space-between;.
But, I want the block that wraps the two buttons to be horizontally and vertically centered on a full page. Therefore, I wrapped everything in a parent div and applied display: flex; justify-content: center; align-items: center to the parent div. However, this results in the spacing of the buttons being reset.
How do I get the overall block to be horizontally and vertically centered on the page while also keeping the spacing of the buttons?
The code is here and here is a Fiddle.
HTML
<div class="form-wrap">
<div class="button-wrap">
<button class="button">
Button 1
</button>
<button class="button">
Button 2
</button>
</div>
</div>
CSS
.form-wrap {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
border: 1px solid gray;
max-width: 100vw;
width: 100vw;
max-height: 100vh;
height: 100vh;
}
.button-wrap {
display: flex;
justify-content: space-between;
max-width: 200px;
}
.button {
padding: 20px;
border: 1px solid gray;
cursor: pointer;
}
.button:hover {
background: lightyellow;
}
You can simply add a margin to your button elements.
.form-wrap {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
border: 1px solid gray;
max-width: 100vw;
width: 100vw;
max-height: 100vh;
height: 100vh;
}
.button-wrap {
display: flex;
justify-content: space-around;
max-width: 200px;
}
.button {
padding: 20px;
border: 1px solid gray;
cursor: pointer;
margin:0 15px;
}
.button:hover {
background: lightyellow;
}
<div class="form-wrap">
<div class="button-wrap">
<button class="button">
Button 1
</button>
<button class="button">
Button 2
</button>
</div>
</div>
Solution #2: Or you can set a width to your button-wrap element that is larger than the total width of the 2 buttons, then you will have a space between the 2 buttons, like below snippet:
.form-wrap {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
border: 1px solid gray;
max-width: 100vw;
width: 100vw;
max-height: 100vh;
height: 100vh;
}
.button-wrap {
display: flex;
justify-content: space-around;
width:250px;max-width: 250px;
}
.button {
padding: 20px;
border: 1px solid gray;
cursor: pointer;
}
.button:hover {
background: lightyellow;
}
<div class="form-wrap">
<div class="button-wrap">
<button class="button">
Button 1
</button>
<button class="button">
Button 2
</button>
</div>
</div>
Related
I need to reproduce this div:
It simply has a circle (an emoji for semplicity) on the top, and two texts on the bottom.
I try this but it doesn't work as I expect
.container {
width: 70px;
height: 400px;
background-color: lightgray;
display: flex;
flex-wrap: wrap;
align-content: space-between;
justify-content: center;
}
.circle {
}
.bottom-container {
display: flex;
flex-direction: column;
}
.artist {
border: 1px solid red;
writing-mode: vertical-rl;
transform: rotate(180deg);
}
.number {
border: 1px solid lime;
}
<div class="container">
<div class="circle">🔴</div>
<div class="bottom-container">
<div class="artist">Artist - Never forget my love</div>
<div class="number">03</div>
</div>
</div>
What's the problem?
You almost had it. Added flex-direction: column and switch your align content and justify content on your container div.
.container {
width: 70px;
height: 400px;
background-color: lightgray;
display: flex;
flex-wrap: wrap;
flex-direction: column;
align-content: center;
justify-content: space-between;
}
.circle {
}
.bottom-container {
display: flex;
flex-direction: column;
}
.artist {
border: 1px solid red;
writing-mode: vertical-rl;
transform: rotate(180deg);
}
.number {
border: 1px solid lime;
}
<div class="container">
<div class="circle">🔴</div>
<div class="bottom-container">
<div class="artist">Artist - Never forget my love</div>
<div class="number">03</div>
</div>
</div>
The circle div should take full space. Adding width and text-align to the circle class did the job:
.circle {
width: 100%;
text-align: center;
}
I am using this to center things in CSS:
.testclass {
display: flex;
justify-content: center;
}
but when i want to scale elements using width and height, it doesn't work and my elements are not centered.
Like this:
.testclass {
display: flex;
justify-content: center;
width: 200px;
height: 200px;
}
What's the problem?
This looks like the expected behavior.
Remember that in this case justify-content: center; centers what is inside the container - not the container itself.
EDIT:
I added margin: 0 auto; to center the container.
#container1 {
display: flex;
justify-content: center;
border: 1px solid red;
}
#container1 > div {
border: 1px solid blue;
background: yellow;
}
#container2 {
display: flex;
justify-content: center;
border: 1px solid red;
width: 200px;
height: 200px;
margin: 0 auto;
}
#container2 > div {
border: 1px solid blue;
background: yellow;
}
<div id="container1">
<div>test 1</div>
</div>
<div id="container2">
<div>test 2</div>
</div>
display: flex; and justify-content: center;
works for parent elements. That is, child elements of that particular parent will be centered, not the parent.
To center .testclassHTML
<div class="parent">
<div class="testclass"></div>
</div>
CSS
.parent {
background-color: red;
display: flex;
justify-content: center;
}
.testclass {
background-color: green;
width: 200px;
height: 200px;
}
If you want full center (horizontal vertical) you can use this code:
.container {
position: relative;
width: 100%;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
<div class="container">
<div class="testclass">Content</div>
</div>
I don't know how to float my button right and I hope someone can help me out here.
The yellow color shows the background of the div. The width of the div is set to 50%.
.faq {
width: 100%;
padding: 12px 20px;
display: block;
box-sizing: border-box;
width: 50%;
margin: auto;
margin-top: 30px;
}
.outerDiv {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
text-align: center;
margin-top: 20px;
background-color: yellow;
}
.save {
float: right;
background-color: red;
}
<div class="outerDiv">
<h2>New FAQ</h2>
<input type="text" class="faq">
<br>
<button class="save" mat-raised-button color="primary">Primary</button>
</div>
What am I doing wrong?
Floats do not work in a flex container
Use align-self:flex-end instead
.faq {
padding: 12px 20px;
display: block;
box-sizing: border-box;
width: 50%;
margin: auto;
margin-top: 30px;
}
.outerDiv {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
text-align: center;
margin-top: 20px;
background-color: yellow;
}
.save {
align-self:flex-end;
background-color: red;
}
<div class="outerDiv">
<h2>New FAQ</h2>
<input type="text" class="faq">
<br>
<button class="save" mat-raised-button color="primary">Primary</button>
</div>
.faq {
width: 100%;
padding: 12px 20px;
display: block;
box-sizing: border-box;
width: 50%;
margin: auto;
margin-top: 30px;
}
.outerDiv {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
text-align: center;
margin-top: 20px;
background-color: yellow;
}
.save {
margin-left:auto;
background-color: red;
}
<div class="outerDiv">
<h2>New FAQ</h2>
<input type="text" class="faq">
<br>
<button class="save" mat-raised-button color="primary">Primary</button>
</div>
Floats do not work with flex.
There are two approaches that you can take here.
Approach #1:
Use align-self: flex-end on the button. This would make the button appear in the rightmost corner of your parent.
Approach #2:
In case you want more control over the position of the button in terms of alignment with the text area, you can wrap the button in a div and keep the float right on the button.
HTML
<div class="button-wrapper">
<button class="save" mat-raised-button color="primary">Primary</button>
</div>
CSS
.button-wrapper {
width: 50%; /* same as the width of your input*/
}
How can I align center an icon and text (below icon), so that they stay at the same position inside a rectangular div when it's getting resized? Here is the example of what I meant. Example
.card{
width: 300px;
height: 400px;
border: 1px solid skyblue;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.circle{
width: 30px;
height: 30px;
border-radius: 100%;
background: green;
}
<div class="card">
<span class="circle"></span>
</div>
Try to use display: flex
I am using flexbox to create part of a page and I have 2 buttons that will open modals in one of the flexbox items. I would like to fix the buttons to the bottom of the item like a footer if possible.
I have created a CodePen to demo the issue. I have tried several solutions but I can't seem to get the buttons aligned at the bottom. I am relatively new to HTML and CSS so it's very possible that I've missed something trivial.
#container {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-around;
align-items: center;
align-content: center;
margin-top: 20px;
}
.one {
background-color: black;
color: white;
font-size: 30px;
padding: 10px;
width: 450px;
flex-grow: 1;
height: 600px;
}
.two {
background-color: grey;
color: white;
font-size: 30px;
padding: 10px;
flex-grow: 1.2;
width: 524px;
height: 600px;
}
button {
background-color: green;
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
}
<div id="container">
<div class="one">
<p> I would like to align the buttons to the bottom of this item </p>
<button id="Btn1">Open Modal 1</button>
<button id="Btn2">Open Modal 2</button>
</div>
<div class="two">
<p> No buttons for this item </p>
</div>
You can make .one display: flex; flex-direction: column;, wrap the buttons in an element, and use justify-content: space-between on .one to push the buttons to the bottom of the column.
#container{
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-around;
align-items: center;
align-content: center;
margin-top: 20px;
}
.one{
background-color: black;
color: white;
font-size: 30px;
padding: 10px;
width: 450px;
flex-grow: 1;
height: 600px;
display: flex;
flex-direction: column;
justify-content: space-between;
}
.two{
background-color: grey;
color: white;
font-size: 30px;
padding: 10px;
flex-grow: 1.2;
width: 524px;
height: 600px;
}
button {
background-color: green;
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
}
<div id="container">
<div class="one">
<p> I would like to align the buttons to the bottom of this item </p>
<div class="buttons">
<button id="Btn1">Open Modal 1</button>
<button id="Btn2">Open Modal 2</button>
</div>
</div>
<div class="two">
<p> No buttons for this item </p>
</div>
Since flex properties only apply to the children of a flex container, your buttons are not flex items.
The buttons are descendants of the flex container (#container), but not children, so they are beyond the scope of flex layout.
One method to resolve the issue would be to make the parent of the buttons a flex container. Then flex properties can be applied to the buttons.
#container {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-around;
align-items: center;
align-content: center;
margin-top: 20px;
}
.one {
background-color: black;
color: white;
font-size: 30px;
padding: 10px;
width: 450px;
flex-grow: 1;
height: 600px;
display: flex; /* new */
flex-wrap: wrap; /* new */
}
p {
flex: 0 0 100%; /* new */
}
button {
margin: auto 10px 0; /* new */
background-color: green;
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
}
.two {
background-color: grey;
color: white;
font-size: 30px;
padding: 10px;
flex-grow: 1.2;
width: 524px;
height: 600px;
}
<div id="container">
<div class="one">
<p> I would like to align the buttons to the bottom of this item </p>
<button id="Btn1">Open Modal 1</button>
<button id="Btn2">Open Modal 2</button>
</div>
<div class="two">
<p> No buttons for this item </p>
</div>
Learn more about the flex formatting context here:
Proper use of flex properties when nesting flex containers
Learn more about flex auto margins here:
In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?
One approach would be to put your buttons in their own <div> like so:
<div id="container">
<div class="one">
<p> I would like to align the buttons to the bottom of this item </p>
<div class="btn-container">
<button id="Btn1">Open Modal 1</button>
<button id="Btn2">Open Modal 2</button>
</div>
</div>
<div class="two">
<p> No buttons for this item </p>
</div>
and change your css to:
#container{
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-around;
align-items: center;
align-content: center;
margin-top: 20px;
}
.one {
display: flex;
flex-direction: column;
flex-grow: 1;
justify-content: space-between;
height: 100vh;
background-color: black;
color: white;
font-size: 30px;
padding: 10px;
width: 450px;
height: 600px;
}
.two {
background-color: grey;
color: white;
font-size: 30px;
padding: 10px;
flex-grow: 1.2;
width: 524px;
height: 600px;
}
.btn-container {
justify-content: flex-end;
display: flex;
}
button {
margin: 10px;
background-color: green;
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
}