2015-04-05 23 views
1

我想使所有div并排。如何让下来的div上沿其他div

我的意思是除去#div3#div4顶部的空格。

我试图floatdisplay:inline-block

我的代码:

<div id="wrapper"> 
    <div id="div1">The first</div> 
    <div id="div2">next to each other.</div> 
    <div id="div3">The two divs are</div> 
    <div id="div4">The two divs are</div> 
</div> 
#div1 { 
    display: inline-block; 
    width:220px; 
    height:120px; 
    border: 1px solid red; 
} 
#div2 { 
    display: inline-block; 
    width:260px; 
    height:260px; 
    border: 1px solid green; 
} 
#div3 { 
    display: inline-block; 
    width:100px; 
    height:160px; 
    border: 1px solid green; 
} 
#div4 { 
    display: inline-block; 
    width:100px; 
    height:160px; 
    border: 1px solid green; 
} 

http://jsfiddle.net/u5y6tuwm/

回答

0
vertical-align: top; 

每个DIV1,2,3和4

您可以添加此影响所有div的页面

div { 
    vertical-align: top; 
} 

或本作#wrapper指定专区内的所有div

#wrapper div { 
    vertical-align: top; 
} 

或这个目标每个div都想要(1-4)

#div1, #div2, #div3, #div4 { 
    vertical-align: top; 
} 

,或者每一个单独,或内联样式等等。

+0

感谢Hastigm但我将代码复制并没有什么 – Deosantess 2015-04-06 08:23:03

+0

嗯,我想也许你的意思是砌体风格的布局,因为如果div 3和4低于div 1和2?如果是这样,对以下问题的回应将帮助您了解它。http://stackoverflow.com/questions/12117195/masonry-style-layout-only-with-css>或这里 2015-04-06 11:21:57

0

你只需要添加

#div1, #div2, #div3, #div4 { 
    float:left; 
} 

这将很好地工作。也不要忘记清除浮动后

1

一种解决方案是在父容器使用flex

#wrapper { 
 
    display: flex; 
 
    /*set display to flex*/ 
 
    margin-top: 20px; 
 
} 
 
#wrapper > div { 
 
    margin-right: 10px; 
 
    /*add some margin to the right to direct children div*/ 
 
} 
 
#div1 { 
 
    width: 220px; 
 
    height: 120px; 
 
    border: 1px solid red; 
 
} 
 
#div2 { 
 
    width: 260px; 
 
    height: 260px; 
 
    border: 1px solid green; 
 
} 
 
#div3 { 
 
    width: 100px; 
 
    height: 160px; 
 
    border: 1px solid green; 
 
} 
 
#div4 { 
 
    width: 100px; 
 
    height: 160px; 
 
    border: 1px solid green; 
 
}
<div id="wrapper"> 
 
    <div id="div1">The first</div> 
 
    <div id="div2">next to each other.</div> 
 
    <div id="div3">The two divs are</div> 
 
    <div id="div4">The two divs are</div> 
 
</div>