[英]Resize height of a div according to other one
Is it possible to resize right div to the height of left one using just CSS? 是否可以仅使用CSS将right div调整为左一个的高度?
My Example 我的例子
I've tried a jQuery approach like this: 我尝试过这样的jQuery方法:
$(document).ready(function () {
$("#right").css("height", $("#left").height());
});
This approach isn't working well because I have dynamically loaded content in the left div and jQuery approach sometimes miscalculates the height. 这种方法效果不佳,因为我已经在左div中动态加载了内容,而jQuery方法有时会错误地计算高度。
I also tried height:100%
on the right div but it didn't work. 我也尝试在正确的div上尝试height:100%
,但是没有用。
For this you can use display:table property for this. 为此,您可以为此使用display:table属性。 Write like this: 这样写:
#left{
margin-right: 15px;
width: 425px;
background-color:#11DD52;
}
#right{
width:200px;
background-color:#4477AA;
}
#left, #right{
display:table-cell;
}
Check this http://jsfiddle.net/ZZBM5/ 检查此http://jsfiddle.net/ZZBM5/
EDIT: If you love hacky way of doing things, @sandeep created this great workaround. 编辑:如果您喜欢简单的处理方式,@ sandeep创建了这个很好的解决方法。
I ended up using this great jQuery resize plugin. 我最终使用了这个很棒的jQuery调整大小插件。 I was hoping there is an easy CSS solution, but the couple of answers in the comments above give me additional browser compatibility issues. 我希望有一个简单的CSS解决方案,但是上面注释中的几个答案给了我其他浏览器兼容性问题。
This is what I've done: 这是我所做的:
$("#left").resize(function () {
$("#right").css("height", $(this).height());
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.