我有两个div并排.我不知道他们的前期高度,它根据内容改变.有没有办法确保它们总是相同的高度,即使其中一个伸展,只有CSS?
我做了个小提示.我希望红色和蓝色的div高度相同......
这是css:
#wrapper {
width: 300px;
}
#left {
width:50px;
background: blue;
float:left;
height: 100%; /* sadly, this doesn't work... */
}
#right {
width:250px;
background: red;
float:left;
}
Run Code Online (Sandbox Code Playgroud)
ant*_*ony 26
您可以尝试使用而不是使用float display: table-cell.您可能会发现一些旧版浏览器不理解此规则.见下文:
#wrapper {
display: table; // See FelipeAls comment below
width: 300px;
}
#left {
display: table-cell;
width: 50px;
background: blue;
}
#right {
display: table-cell;
width: 250px;
background: red;
}
Run Code Online (Sandbox Code Playgroud)
Antony的答案工作正常,但你需要所有的div拥有相同的父级并拥有一个包装器,我有一个使用javascript的解决方案,但适用于任何类型的元素,他们只需要有相同的选择器.
function setEqualHeight(selector, triggerContinusly) {
var elements = $(selector)
elements.css("height", "auto")
var max = Number.NEGATIVE_INFINITY;
$.each(elements, function(index, item) {
if ($(item).height() > max) {
max = $(item).height()
}
})
$(selector).css("height", max + "px")
if (!!triggerContinusly) {
$(document).on("input", selector, function() {
setEqualHeight(selector, false)
})
$(window).resize(function() {
setEqualHeight(selector, false)
})
}
}
setEqualHeight(".sameh", true)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
51891 次 |
| 最近记录: |