简体   繁体   English

父Div的宽度=子Div的最大宽度?

[英]Width of Parent Div = Maximum Width of Child Divs?

Can I make the width of a parent div be automatically equal to the maximum width of its children? 我可以使父div的宽度自动等于其子级的最大宽度吗? Ie I want the the parent to be as wide as the widest child, no wider. 也就是说,我希望父母与最大的孩子一样宽,而不是更大。 I can accept both pure css and javascript solutions. 我可以接受纯CSS和javascript解决方案。

In particular, I have a parent div with two floating divs inside . 特别是, 我有一个父div,其中有两个浮动div I want the parent div to resize when the right div drops below (if the window becomes too narrow), to make the background of the parent div display properly. 我希望当右div降到下方时(如果窗口变得太窄),父div的大小将重新调整,以使父div的背景正确显示。

Thank you! 谢谢!

<div class="container"> 
    <div class="logo-div">
        <img class="logo" src="http://bit.ly/1qCKrtJ" />
    </div>
    <div class="text-div">
        <h4 style="display: inline;">Because Sometimes It Takes a Village</h4><br />
        What about robots the size of tea cups that scoot around on tiny wheels, snapping pictures with miniature cameras and keeping track of where they are in relation to dozens of others?
    </div>
</div>

Make the container display:inline-block 使容器display:inline-block

JSfiddle Demo JSfiddle演示

.container {
    background: green;
    overflow: hidden;
    max-width: 430px;
    display: inline-block; /* add this */
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM