简体   繁体   English

IE7无法抵消保证金

[英]IE7 unable to offset with a margin

Would anyone be able to tell me why my following code doesn't centre the "block" on the "pivot" in IE7? 谁能告诉我为什么我的以下代码没有将IE7中的“块”放在“枢轴”上?

Instead it seems to half the size of the "block" for those browsers. 取而代之的是,这些浏览器的大小只有“块”的一半。

<style>
.pivot {
   position: absolute;
   top: 50%;
   left: 50%;
}
.block {
   height: 200px;
   width: 200px;

   margin-left: -50%;
   margin-top: -50%;

   background-color: green;
}
</style>

<div class="pivot">
<div class="block"></div>
</div> <!-- end pivot -->

EDIT 编辑

To get this to work I used Erics code for IE6-IE-7 hacked into my margin approach for all other browsers 为了使其正常工作,我使用了适用于IE6-IE-7的Erics代码,入侵了所有其他浏览器的margin方法

Something in your code is wrong, try giving to pivot width and height and it wont work in any browser. 您的代码中的某些内容有误,请尝试设置枢轴的宽度和高度,但在任何浏览器中均无法使用。

If you want to center an element in the center of the screen you should do something like that: 如果要在屏幕中心居中放置元素,则应执行以下操作:

#element {
width: 200px;
height: 200px;
position: absolute;
top: 50%;
left: 50%;
margin-left: -100px;
margin-top: -100px;
background-color: green;
}

And it will work in all browsers, including IE6-7. 它将在包括IE6-7在内的所有浏览器中运行。

Is this not an option for you? 这不是您的选择吗?

.pivot {
    text-align: center;
}
.block {
    display: inline-block;
    text-align: left;
}

Normally the done thing is to use relative positioning: 通常,完成的工作是使用相对定位:

<style>
.pivot {
   position: absolute;
   top: 50%;
   left: 50%;
}
.block {
   height: 200px;
   width: 200px;

   position: relative;
   left: -50%;
   top: -50%;

   background-color: green;
}
</style>

<div class="pivot">
    <div class="block"></div>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM