2016-05-08 53 views
0

我試圖做一個容器的div所有的div將具有相同的寬度(每行兩個div,每個容器的50%寬度)。Flex項目覆蓋最大寬度屬性

我已經在容器內設置了div,因爲我希望它們是平等的,但在這行中只有一個項目時,它似乎並不尊重這個max-width

HTML

<div id="container"> 
    <div id="left" class="block">Left</div> 
    <div id="center" class="block"> 
    <div class="flexContainer"> 
     <div class="flexDiv"></div> 
    </div> 
    <div class="flexContainer"> 
     <div class="flexDiv"></div> 
    </div> 
    <div class="flexContainer"> 
     <div class="flexDiv"></div> 
    </div> 
    </div> 
    <div id="right" class="block">Right</div> 
</div> 

CSS

html, body{ 
    width: 100%; 
    height: 100%; 
} 

#container{ 
    display: flex; 
    height: 100%; 
    background-color: blue; 
} 

.block{ 
    flex: 1; 
} 

#left{ 
    background-color: green; 
} 

#center{ 
    display: flex; 
    flex: 1; 
    flex-wrap: wrap; 
    align-content: flex-start; 
} 

#right{ 
    background-color: orange; 
} 

.flexContainer{ 
    flex: 1; 
    min-width: 100px; 
    max-width: 50%; 
    height: 150px; 
    background-color: red; 
    padding: 10px; 
} 

.flexDiv{ 
    width: 100%; 
    height: 100%; 
    background-color: yellow; 
} 

JSFiddle中,你可以看到第三個元素的寬度是如何比別人更大。

爲什麼flex容器內的div不尊重max-width屬性?

在此先感謝!

+0

你的意思是喜歡這個? - https://jsfiddle.net/yz9cd8e7/ –

+0

@Paulie_D是的,就是這個。如果您在答案中提供解釋,我會將其標記爲已接受:) –

+0

沒問題...基本上它是一個「盒子大小」問題。 @Gyryrillus已經提供了答案。 –

回答