簡體   English   中英

如何使父級寬度等於其中所有元素的寬度?

[英]how to make the parent width equals the width of all element inside it?

我的CSS代碼

.child{
    width:100px;
    height:100px;
    display:inline-block;
}

我的HTML代碼

<div id="parent">
    <div class="child">
    </div>
    <div class="child">
    </div>
</div>

.parent寬度將為100%,但我想使父寬度完全等於所有孩子的寬度,但是我不知道我將擁有多少個孩子,我也不知道每個孩子的寬度

使用CSS AND / OR Jquery做到這一點的最簡單方法是什么?

您可以浮動.parent或內聯顯示。

$(document).ready(function(){
    var sumwidth=0;
    $("#parent").children().each(function() {
        var child = $(this);
        sumwidth+=child.width();
    });
    $("#parent").width(sumwidth);
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM