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

标签 javascript html css layout web

我可以让父div的宽度自动等于其子div的最大宽度吗? IE。我希望父级与最宽的子级一样宽,而不是更宽。我可以接受纯 css 和 javascript 解决方案。

特别是,I have a parent div with two floating divs inside 。我希望当右侧 div 下降到下方时(如果窗口变得太窄)调整父 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>

最佳答案

使容器display:inline-block

JSfiddle Demo

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

关于javascript - 父 Div 的宽度 = 子 Div 的最大宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24757105/

相关文章:

javascript - 将 flash youtube 更改为嵌入对象

javascript - CSS 转换 - 元素缩放但不转换

html - 使用谷歌字体

javascript - angular-block-ui 不会将我的 html 作为自定义消息执行

javascript - Node.JS - 循环对象内的数组

javascript - React Hooks - 根据状态有条件地渲染组件

javascript - jQuery:查找所有具有 &lt;input type ="number"> 的元素

html - 如何更改数据列表的显示宽度

html表格边框问题

jquery - Tinymce 按钮未出现在某些页面中