'Flex items not centering vertically
I'm trying to center vertically the content with flex boxes without success. I don't like to use position or translate because it is not a fixed size. What is wrong with my code?
.row-centered {
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
}
.col-centered {
display: flex;
flex-direction: column;
align-self: flex-start;
float: none;
margin-right: 0 auto;
}
.us-container{
display: flex;
justify-content: center;
align-items: center;
vertical-align: middle;
resize: both;
overflow: auto;
}
.us-container div{
resize: both;
overflow: auto;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<section id="us" class="container-fluid">
<div class="row row-centered us-container">
<div class="col-sm-8 col-md-8 col-xs-8 col-centered ">
<p class="text-justify">blah blah blah blah blah blahblah blah blahblah blah blahblah blah blahblah blah blahblah blah blahblah blah blahblah blah blahblah blah blahblah blah blahblah blah blahblah blah blahblah blah blahblah blah blahblah blah blahblah blah blahblah blah blahblah blah blahblah blah blahblah blah blahblah blah blahblah blah blahblah blah blahblah blah blahblah blah blahblah blah blahblah blah blahblah blah blah </p>
<h3 class="text-center">blah blah blah</h3>
</div>
</div>
</section>
Solution 1:[1]
Add height: 100% to html, body, .container-fluid and make .container-fluid also be a flex container as shown below.
html, body, .container-fluid {
height: 100%;
}
.row-centered, .container-fluid {
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
}
.col-centered {
display: flex;
flex-direction: column;
align-self: flex-start;
float: none;
margin-right: 0 auto;
}
.us-container{
display: flex;
justify-content: center;
align-items: center;
vertical-align: middle;
resize: both;
overflow: auto;
}
.us-container div{
resize: both;
overflow: auto;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<section id="us" class="container-fluid">
<div class="row row-centered us-container">
<div class="col-sm-8 col-md-8 col-xs-8 col-centered ">
<p class="text-justify">blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah </p>
<h3 class="text-center">blah blah blah</h3>
</div>
</div>
</section>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Community |
