Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
282 views
in Technique[技术] by (71.8m points)

html - 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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your flex container has no extra height. The only height is the height of the content. Therefore, there is no space for vertical centering.

First step, add some height:

.row-centered {  
  display: flex;
  height: 100vh;
}

Second step, remove unnecessary rules, some of which are preventing vertical alignment.

.col-centered {
    display: flex;
    flex-direction: column;
   /* align-self: flex-start;
    float: none;
    margin-right: 0 auto; */
}

.row-centered {
  display: flex;
  height: 100vh;
}
.col-centered {
  display: flex;
  flex-direction: column;
}
.us-container {
  display: flex;
  justify-content: center;
  align-items: center;

}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...