Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

css - Layout possible? Flexbox vs Float layout with multiple columns and rows

I'm curious if this layout is possible with flexbox. I can't seem to work out divs 3 & 4 to fall under #2. This is pretty easy with floats, just curious if I'm missing some properties that may help with flexbox.

Layout

+-------+-------+-------+
| div 1 |     div 2     |
+       +-------+-------+
|       | div 3 | div 4 |
+-------+-------+-------+

Markup

<div class="features">
  <div class="feature feature-1">1</div>
  <div class="feature feature-2">2</div>
  <div class="feature feature-3">3</div>
  <div class="feature feature-4">4</div>
</div>

Demo

http://codepen.io/mikevoermans/pen/xbWvJJ?editors=110

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Flexbox does not like flex items that expand through multiple columns or rows, because in fact flexbox has no grid notion.

However, using some tricks, you can achieve this layout (and more complicated ones too):

  • Use a row layout

    ┌─┬─┬─┬─┐
    │1│2│3│4│
    └─┴─┴─┴─┘
    
  • Allow line breaks with flex-wrap: wrap.

  • Use a pseudo element to force a line break after 2

    ┌─┬─┐
    │1│2│
    ├─┼─┤
    │3│4│
    └─┴─┘
    
  • Use flex: 1 on all flex items.

    ┌─────────┬─────────┐
    │1        │2        │
    ├─────────┼─────────┤
    │3        │4        │
    └─────────┴─────────┘
    
  • Set margin-left: 50% to 3

    ┌─────────┬─────────┐
    │1        │2        │
    └─────────┼────┬────┤
              │3   │4   │
              └────┴────┘
    
  • Set height: 200px, to 2, 3 and 4. Set height: 400px to 1.

    ┌─────────┬─────────┐
    │1        │2        │
    │         ├─────────┘
    │         │
    └─────────┼────┬────┐
              │3   │4   │
              └────┴────┘
    
  • Set margin-bottom: -200px to 1:

    ┌─────────┬─────────┐
    │1        │2        │
    │         ├────┬────┤
    │         │3   │4   │
    └─────────┴────┴────┘
    
  • Since you have borders, use box-sizing: border-box on all boxes to make height include the borders. Otherwise 1 would need height: 416px; margin-bottom: -216px.

  • Note flexbox introduces auto as the new initial value of min-width. That could allow the content to force some boxes to grow. That would break the layout, so disable it with min-width: 0 or setting overflow to anything but visible.

Here is the code:

.features {
  display: flex;
  flex-flow: row wrap;
}
.feature {
  background: #ccc;
  border: 8px solid #fff;
  height: 200px;
  box-sizing: border-box;
  min-width: 0;
  flex: 1;
}
.feature-1 {
  /* Make it taller without increasing the height of the flex line */
  height: 400px;
  margin-bottom: -200px;
}
.features:after {
  /* Force line break */
  content: '';
  width: 100%;
}
.feature-2 ~ .feature {
  /* Place 3 and 4 after the line break */
  order: 1;
}
.feature-3 {
  margin-left: 50%;
}
<div class="features">
  <div class="feature feature-1">1</div>
  <div class="feature feature-2">2</div>
  <div class="feature feature-3">3</div>
  <div class="feature feature-4">4</div>
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...