javascript - 调整父 div 的大小以匹配绝对定位的子 div 高度

标签 javascript jquery html css height

我有一个 CSS 问题:

我在容器中有一个绝对定位的 div。容器不会根据内容的高度调整大小。为什么?

最佳答案

您需要为此使用 JavaScript。使用 jQuery 你可以做到

var parentHeight = $('#parent').height(),
    childHeight = $('#child').height();

if (parentHeight <= childHeight) {
    $('#parent').height(childHeight);
}

http://jsfiddle.net/mkCU5/2/ 查看工作示例

关于javascript - 调整父 div 的大小以匹配绝对定位的子 div 高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5359574/

相关文章:

javascript - datauri/图像到 Canvas

javascript - Promise 处理错误

javascript - 为什么我的代码总是以 undefined 结尾?

javascript - 如何在 javascript 中创建图像叠加图像

javascript - 尝试修改 Bootstrap 工具提示的工具提示颜色

jquery - 通过 HTTPS 的跨域 JSONP

Javascript 加载选项卡,仅在 50% 的时间内有效

Javascript 变量未在函数范围之外更改

html - 使用 html 和下拉菜单 html css 的 Img 排序

javascript - 使用 JavaScript 模拟 Tab 按键