父Div的宽度= Child Divs的最大宽度?

时间:2014-07-15 11:31:59

标签: javascript html css layout web

我可以让父div的宽度自动等于其子节点的最大宽度吗?即我希望父母和最宽的孩子一样宽,不要宽。我可以接受纯css和javascript解决方案。

特别是I have a parent div with two floating divs inside。我希望父div在右下方调整时调整大小(如果窗口变得太窄),以使父div的背景显示正确。

谢谢!

<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>

1 个答案:

答案 0 :(得分:1)

制作容器display:inline-block

JSfiddle Demo

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