我正在從數據庫獲取輸出。有時我得到2個div,有時我得到3個div,所以我必須設置div的等寬。我的意思是如果有2個div,然後設置每個的50%,如果有3個div,則設置每個的33.33%。你能幫我嗎?如何使用CSS顯示div的相等寬度?
#container1
{
width: 100%;
display: inline-flex;
}
#container1 div{
color: #fff;
display: inline-block;
height: 100%;
width: 50%;
background: #24252A;
text-align: center;
cursor: default;
padding: 2em 0;
}
#container1 div:nth-of-type(2) {
background: red;
}
#container1 div:nth-of-type(3)
{
width: 33.33% !important;
}
<div id="container1">
<!--div depanding upload database-->
<div></div>
<div></div>
<div></div>
</div>
您可以定義寬度基於使用這個問題的答案子女數https://stackoverflow.com/questions/8720931/can-css-detect-the更新片段 - 子元素數量有 – kinggs
不是動態的。 Flexbox可以設置**初始**寬度,但內容會改變這些框的大小以反映其個別需求。 –
如果有人給予downvote然後請解釋它爲什麼我和答案得到downvote? –