Html 如何在IE7中居中放置绝对定位的div?
更新,为布局提供上下文 我的页面有一个相对简单的结构。该页面由两个div组成,两个div都处于绝对位置。一个在另一个中间Html 如何在IE7中居中放置绝对定位的div?,html,css,internet-explorer,layout,internet-explorer-7,Html,Css,Internet Explorer,Layout,Internet Explorer 7,更新,为布局提供上下文 我的页面有一个相对简单的结构。该页面由两个div组成,两个div都处于绝对位置。一个在另一个中间 <div id="protocol_index_body_wrapper"> <div id="protocol_index_body"> </div> </div> 预期的行为如上图所示。IE8、Firefox和Chrome中都存在这种行为。然而,在IE7中,应该居中的div与左侧齐平。有什么想法吗?文本对齐
<div id="protocol_index_body_wrapper">
<div id="protocol_index_body">
</div>
</div>
预期的行为如上图所示。IE8、Firefox和Chrome中都存在这种行为。然而,在IE7中,应该居中的div与左侧齐平。有什么想法吗?
文本对齐:居中对齐包装,或
(我知道很难看,但很有效)
或使用JS:
document.getElementById("protocol_index_body_wrapper").style.marginRight = (document.body.clientWidth - 50)/2_+"px"
仅适用于IE6+。请尝试以下方法:
#protocol_index_body {
width: 50px;
margin: 0 auto 0 -25px;
position: absolute;
top: 0;
left: 50%;
right: 0;
bottom: 0;
background-color: red;
}
或者
#protocol_index_body {
width: 50px;
margin: 0 auto 0 50%;
position: absolute;
top: 0;
left: -25px;
right: 0;
bottom: 0;
background-color: red;
}
除非您需要父div具有流体宽度(当您设置子div的宽度时,这会有点愚蠢),为什么不直接设置父div的宽度并添加边距:0 auto
?好的,我使用过它,在FF、Opera和IE7中效果相同:
#protocol_index_body_wrapper {
background-color:black;
padding: 0 0 20px 0;
position: absolute;
top: 120px;
left: 0px;
right: 0px;
bottom: 10px;
text-align: center;
width: 100%;
height: 100%;
}
#protocol_index_body {
width: 50px;
margin: 0 auto;
background-color: red;
height: 100%;
}
autocenterallign=function(){
var bodyWidth=$(“body”).innerWidth();
var protocolWidth=$(“#协议索引_body”).innerWidth();
if(协议宽度<正文宽度){
$(“#协议索引_body”).css(“左”((bodyWidth protocolWidth)/2)+“px”);
}
}
window.onload=自动中心对齐;
window.onresize=自动中心对齐;
jQuery(window).load(函数(){
自动中心对齐()
});
尝试添加文本对齐:居中
到#协议索引_body
尝试在body\u包装div上设置100%的宽度。我不确定为什么要给内部div一个绝对位置。如果你想让它在一个已经绝对定位的元素中居中,那么你不需要它是绝对的。我想我已经把它固定到你需要的位置了。我查看了FF中的初始代码,然后从那里开始。请看我下面的代码。父div是子div上的一个全宽背景。它可以工作,但这只是因为您使用了固定的边距。无论屏幕大小如何,都需要保持在屏幕中央。设置的高度和宽度会扰乱屏幕的布局。见更新的原始帖子。
#protocol_index_body_wrapper {
background-color:black;
padding: 0 0 20px 0;
position: absolute;
top: 120px;
left: 0px;
right: 0px;
bottom: 10px;
text-align: center;
width: 100%;
height: 100%;
}
#protocol_index_body {
width: 50px;
margin: 0 auto;
background-color: red;
height: 100%;
}
autoCenterAlign = function() {
var bodyWidth = $("body").innerWidth();
var protocolWidth = $("#protocol_index_body").innerWidth();
if(protocolWidth < bodyWidth) {
$("#protocol_index_body").css("left",((bodyWidth-protocolWidth)/2)+"px");
}
}
window.onload = autoCenterAlign;
window.onresize = autoCenterAlign;
jQuery(window).load(function () {
autoCenterAlign()
});