This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?
(6 answers)
Closed 3 years ago.
I need to put button under button, with display flex.
When I remove display: flex the button goes up the page, but when I use display: flex the buttons are centered but they are side by side.
#wrapper {
width: 100%;
height: 400px;
border: 1px solid black;
display: flex;
align-items: center;
justify-content: center;
}
button {
height: 20px;
width: 100px;
}
<div id="wrapper">
<button type="button">hello</button>
<button type="button">hello</button>
</div>
You can use flex-direction: column. This will stack the flex items vertically (from top to bottom)
#wrapper {
width: 100%;
height: 400px;
border: 1px solid black;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}
button {
height: 20px;
width: 100px;
}
<div id="wrapper">
<button type="button">hello</button>
<button type="button">hello</button>
</div>
Related
This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
How can I horizontally center an element?
(133 answers)
How to align text below an image in CSS?
(7 answers)
Closed last year.
struggling to centre my icon above the text, the height of the icon is fine but it should sit like so:
tried using flex but couldn't find the correct solution.
.container {
display: block;
width: 400px;
height: 100px;
border: 2px solid black;
}
.icon {
width: 30px;
height: 30px;
background-color: red;
}
.text {
text-align: center;
}
<div class="container">
<div class="icon"></div>
<p class="text">We couldn't find any matches to your search. <br></br>Please try again.</p>
</div>
You can use CSS flexbox: but remember that the default direction is row. If you switch it to flex-direction: column, then you will achieve what you want:
.container {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
width: 400px;
height: 100px;
border: 2px solid black;
}
.icon {
width: 30px;
height: 30px;
background-color: red;
}
.text {
text-align: center;
}
<div class="container">
<div class="icon"></div>
<p class="text">We couldn't find any matches to your search. <br />Please try again.</p>
</div>
This question already has answers here:
How to Right-align flex item?
(13 answers)
In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?
(6 answers)
Closed 2 years ago.
How do I align the album cover div to the right inside the card. I tried with align-self: end property but that does not seem to work. Could anyone please help?
.card {
border: 1px red solid;
width: 450px;
height: 150px;
border-radius: 5px;
display: flex;
flex-direction: row;
}
.image {
width: 150px;
height: 150px;
align-self: end;
border: 1px solid green;
}
<div class="card">
<div>
<div>Music controls</div>
</div>
<div class="image">Album Cover</div>
</div>
You can add justify-content: space-between; to your card's css:
.card {
border: 1px red solid;
width: 450px;
height: 150px;
border-radius: 5px;
display: flex;
flex-direction: row;
justify-content: space-between;
}
.image {
width: 150px;
height: 150px;
align-self: end;
border: 1px solid green;
float: right;
}
<div class="card">
<div>
<div>Music controls</div>
</div>
<div class="image">Album Cover</div>
</div>
Flex has two main axis, if you want to align the album cover to the right in the main axis, you need to use the property justify-content: space-between; that will expand your music controls and album cover, with space-between ittems are evenly distributed in the line; first item is on the start line, last item on the end line, you can check more about flex here
I added a new class to the music controls and added a flex-grow value of 2 and align-self value of flex-start.
then for .image, i added align-self value of flex-end
at the end, i removed the hard-coded width/ height from image
Good luck!
.card {
display: flex;
flex-direction: row;
background-color: #005792;
width: 450px;
height: 150px;
border-radius: 5px;
overflow: hidden;
}
.music-controls{
display: flex;
flex-grow: 2;
}
.image {
display: flex;
flex-grow: 1;
background-color: #fd5f00;
}
<div class="card">
<div class="music-controls">
<div>Music controls</div>
</div>
<div class="image">Album Cover</div>
</div>
This question already has answers here:
How to center an element horizontally and vertically
(27 answers)
How can I horizontally center an element?
(133 answers)
How can I vertically center a div element for all browsers using CSS?
(48 answers)
Flexbox: center horizontally and vertically
(14 answers)
Closed 4 years ago.
I want to center my content both vertically and horizontally in the page. I couldn't seem to get it working. Anyone here can help?
Thanks.
<style type = "text/css">
#loginMenu {
border:5px solid green;
padding:10px 10px 10px 10px;
background-color:#FF9;
display: table;
position: absolute;
text-align:center;
height: 200px;
width: 400px;
}
.centrediv
{
margin: auto;
vertical-align:middle;
}
</style>
<div id="loginMenu"></div>
Flexbox does this the best. Example:
<div class="Aligner">
<div class="Aligner-item">…</div>
</div>
.Aligner {
display: flex;
align-items: center;
justify-content: center;
}
.Aligner-item {
max-width:50%;
}
You should use flexbox as it serves your need with ease. Have a look of the snippet:
<!-- HTML -->
<body>
<div></div>
</body>
<!-- CSS -->
body{
display: flex;
vertical-align: middle;
height: 100vh;
width: 100%;
justify-content: center;
align-items: center;
box-sizing: border-box;
margin: 0px;
}
div{
height: 100px;
width: 100px;
background-color: #f00;
margin-top: -50px;
}
Based on a page from W3Schools
.centrediv
{
padding: 100px 0;
text-align: center;
}
This question already has answers here:
Center and bottom-align flex items
(3 answers)
Closed 5 years ago.
How to align a div vertically to middle and another div vertically to bottom inside a flex column?
Expected result:
.container {
border: 1px solid black;
height: 200px;
width: 50px;
display: flex;
flex-direction: column;
justify-content: center;
}
.first-item {
margin-top: auto;
margin-bottom: auto;
}
<div class="container">
<div class="first-item">First</div>
<div class="second-item">Second</div>
</div>
That should do it. Then the second item should be pushed to the bottom while the first item stays in the middle. A pure flexbox solution not using absolute positioning.
You have to use the line-height property with the same height value
.parent{
display: table;
position: relative;
border: 1px solid;
height: 150px;
line-height: 150px;
}
.parent div{
position: absolute;
bottom: 0;
line-height: 20px;
}
<div class="parent">
test
<div>test</div>
</div>
Take a look at this.
.parent{
display: flex;
height: 150px;
width: 50px;
border: 1px solid black;
align-items: center;
justify-content: center;
}
.two{
align-self: flex-end;
position: absolute;
}
<div class="parent">
<div>test</div>
<div class="two">test</div>
</div>
This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
Closed 5 years ago.
I have a div with a display: flex, and I would like to display the output in a row center. For some reason it is displaying left. I want the imgBoxBot to be centered inside the imgBot div. But text-align: center is only centering the text inside the imgBoxBot, how do I center the actual div?
#imgBot {
position: absolute;
height: 100px;
width: 100%;
bottom: 0;
display: flex;
flex-direction: row;
text-align: center;
}
#imgBoxBot {
height: 100px;
width: 100px;
}
<div id="imgBot">
<div id="imgBoxBot">
test
</div>
<div id="imgBoxBot">
test
</div>
</div>
Use justify-content: center. You can find more information here
#imgBot {
position: absolute;
height: 100px;
width: 100%;
bottom: 0;
display: flex;
flex-direction: row;
justify-content: center;
}
.imgBoxBot {
height: 100px;
width: 100px;
}
<div id="imgBot">
<div class="imgBoxBot">
test
</div>
<div class="imgBoxBot">
test
</div>
</div>