2015-04-05 58 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>