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
353 views
in Technique[技术] by (71.8m points)

html - Why is these flex items not wrapping?

I have a flex item that is also a flex container .sub-con, problem is the flex item of .sub-con is refusing to wrap, even after adding : flex-flow: row wrap.

Can anyone fix this for me, or point out what I'm doing wrong.

.container {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-flow: row wrap;
  height: 100vh;
}

.sub-con {
  margin-right: 50px;
  margin-left: 50px;
  height: 500px;
  background-color: #fff;
  display: flex;
  justify-content: center;
  flex-flow: row wrap;
}

.col-one {
  height: 100px;
  border: 1px solid lightgreen;
  flex-grow: 2;
}

.col-two {
  height: 100px;
  border: 1px solid red;
  flex-grow: 1;
}
<div class="container">
  <div class="sub-con">
    <div class="col-one"></div>
    <div class="col-two"></div>
  </div>
</div>
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 items in the nested container are sized with percentages.

.col-one{
  width: 40%;
  height: 100px;
  border: 1px solid lightgreen;
}
.col-two{
  width: 40%;
  height: 100px;
  border: 1px solid red;
}

Because percentage lengths are based on the length of the parent they have no reason to wrap. They will always be 40% of the parent, even if the parent has a width of 1%.

If you use other units for length, such as px or em, they will wrap.

jsFiddle demo

 .container {
   display: flex;
   justify-content: center;
   align-items: center;
   flex-flow: row wrap;
   height: 100vh;
 }
 
 .sub-con {
   flex: 1;  /* for demo only */
   align-content: flex-start; /* for demo only */
   margin-right: 50px;
   margin-left: 50px;
   height: 500px;
   background-color: #fff;
   display: flex;
   justify-content: center;
   flex-flow: row wrap;
 }
 
 .col-one {
   width: 200px;
   height: 100px;
   border: 1px solid lightgreen;
 }
 
 .col-two {
   width: 200px;
   height: 100px;
   border: 1px solid red;
 }
<div class="container">
  <div class="sub-con">
    <div class="col-one"></div>
    <div class="col-two"></div>
  </div>
</div>

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

...