我没有看到你拥有的所有代码,所以我不知道为什么它会在 Chrome 中这样做(因为我的代码没有这样做)。
您可能有两个 div 的“折叠边距”。您可以将 a 添加padding: 1px 0
到外部 div 以便垂直边距不会折叠。
我认为您使用窗口调整大小处理程序是因为您的外部 div 与窗口的大小相关联?所以我在下面的代码中使它与窗口的高度相同。
此代码适用于 Mac 上的 Chrome:
<!DOCTYPE html>
<style>
body { margin: 0; }
.wrapper { position: fixed; width: 100%; height: 100%;}
.cx1 { background: dodgerblue; height: 100%; width: 300px; padding: 1px 0; }
.cx2 { background: #ffc; height: 256px; width: 200px;}
</style>
<div class="wrapper">
<div class="cx1">
<div class="cx2">
hello
</div>
</div>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script>
function verticallyCenter() {
var heightf = $('.cx1').height();
var heightg = $('.cx2').height();
heighth = (heightf - heightg) / 2;
if (heighth > 0) {
$('.cx2').css({ "marginTop": heighth });
}
}
$(verticallyCenter);
$(window).resize(verticallyCenter);
</script>