Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
是否可以仅使用 CSS 将右 div 的大小调整为左 div 的高度? 我的例子
我试过这样的jQuery方法:
$(document).ready(function () { $("#right").css("height", $("#left").height()); });
这种方法效果不佳,因为我在左侧 div 中动态加载了内容,而 jQuery 方法有时会错误计算高度。
我也尝试height:100%了正确的 div 但它没有用。
height:100%
为此,您可以使用display:table属性。像这样写:
#left{ margin-right: 15px; width: 425px; background-color:#11DD52; } #right{ width:200px; background-color:#4477AA; } #left, #right{ display:table-cell; }
检查这个http://jsfiddle.net/ZZBM5/
编辑:如果你喜欢 hacky 的做事方式,@sandeep 创造了这个伟大的解决方法。
我最终使用了这个很棒的 jQuery 调整大小插件。我希望有一个简单的 CSS 解决方案,但上面评论中的几个答案给了我额外的浏览器兼容性问题。 这就是我所做的:
$("#left").resize(function () { $("#right").css("height", $(this).height()); });