简体   繁体   English

CSS-定位多个相邻的div,以便在2个div之间彼此堆叠

[英]CSS - Positioning of multiple adjacent divs to have one stack on top of each other between 2 divs

I'm facing an issue where I need to make several adjacent div to position a certain way: 我面临的一个问题是,我需要使几个相邻的div定位到特定的位置:

在此处输入图片说明

Their html layout positions are right next to each other: 它们的html布局位置彼此相邻:

<div class="parent">
    <div class="div1">....</div>
    <div class="div2">....</div>
    <div class="div3">....</div>
    <div class="div4">....</div>
</div>

I've tried with flex boxes and floating out Div1 and Div4 out but it's not working. 我已经尝试过使用弹性盒,然后将Div1和Div4浮动出来,但是它不起作用。 I also need Div1 and Div4's height to all be vertically aligned to its correct dynamic height depending on the contents of Div2 and Div3. 我还需要根据Div2和Div3的内容将Div1和Div4的高度全部垂直对齐为其正确的动态高度。

CSS grid may help you solve it easily. CSS网格可以帮助您轻松解决它。

 .parent { display: grid; grid-gap: 10px; grid-template-columns: [col1-start] 100px [col2-start] 100px [col3-start] 100px [col3-end]; grid-template-rows: [row1-start] auto [row2-start] auto [row2-end]; } .div1, .div2, .div3, .div4 { background-color: #444; color: #fff; padding: 20px; } .div1 { grid-column: col1-start; grid-row: row1-start / row2-end ; } .div2 { grid-column: col2-start ; grid-row: row1-start; } .div3 { grid-column: col2-start; grid-row: row2-start ; } .div4 { grid-column: col3-start ; grid-row: row1-start / row2-end ; } 
 <div class="parent"> <div class="div1">....</div> <div class="div2">....</div> <div class="div3">....</div> <div class="div4">....</div> </div> 

Some more examples could be found HERE . 这里可以找到更多示例。

Here is an idea with flexbox: 这是flexbox的一个想法:

 * { box-sizing:border-box; } .parent { display:flex; height:200px; flex-direction:column; flex-wrap:wrap; } .div1,.div4 { height:100%; width:20%; border:1px solid green; } .div2,.div3 { height:50%; width:60%; border:1px solid red; } 
 <div class="parent"> <div class="div1">1</div> <div class="div2">2</div> <div class="div3">3</div> <div class="div4">4</div> </div> 

I would do it this way: 我会这样:

 *{ margin: 0; border: 0; padding: 0; box-sizing: border-box; } .parent{ display: flex; padding: 15px; } .a, .b{ background: #ddd; margin: 10px; flex-basis: 20%; } .container{ width: 60%; } .container div{ background: #ddd; margin: 10px; } 
 <div class="parent"> <div class="a">div1</div> <div class="container"> <div>div</div> <div>div</div> </div> <div class="b">div2</div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM