2017-02-27 73 views
0

這裏是我的代碼的JS小提琴:https://jsfiddle.net/o7nj1mj9/使用Flexbox將創建一個DIV元素的頂部網格

HTML:

<div id="div2"> 
     <div class = "flex-table"> 1</div> 
     <div class = "flex-table"> 2</div> 
     <div class = "flex-table"> 3</div> 
     <div class = "flex-table"> 4</div> 

CSS:

.container { 
display: flex; 
position: relative; 
max-width: 1600px; 
padding: 15px; 
height: 275px; 
overflow: visible; 
background-color: #84B6C0; 
box-shadow: 0px 10px 5px #888888; 
font-family: 'Open Sans'; 

#div2 { 
padding: 0; 
margin: 0; 
list-style: none; 
display: -webkit-box; 
display: -moz-box; 
display: -ms-flexbox; 
display: -webkit-flex; 
display: flex; 
-webkit-flex-flow: row; 
justify-content: space-around; 
height: 50px; 
line-height:30px; 
} 

.flex-table { 
background: tomato; 
margin: 5px; 
color: white; 
font-weight: bold; 
font-size: 1.5em; 
text-align: center; 
height: auto; 
flex-grow: 1; 
min-width: 25%; 
} 

我想在DIV元素頂部創建一個4格的網格,「投資組合」。正如你所看到的,我有4個方塊,但它們並沒有填滿DIV的整個空間。

道歉,如果代碼混亂。

回答

0

#div2 {,刪除height:50px並添加width: 100%

https://jsfiddle.net/o7nj1mj9/1/

+0

有趣!這工作,謝謝!你能解釋爲什麼嗎? – scareware

+0

隨着身高,你顯然限制你的div不要考慮父母的身高。而對於寬度,div不是貪婪的,所以,只是讓它100%繼承所有父寬度 – mehulmpt

+0

感謝您的解釋 – scareware