html - 在 FF、IE6 和 IE7 中工作的 HTML 中垂直和水平居中的实用解决方案

标签 html css

在 Firefox、IE6 和 IE7 中工作的 HTML 内容垂直和水平居中的实用解决方案是什么?

一些细节:

  • 我正在寻找整个页面的解决方案。

  • 您只需指定要居中的元素的宽度。元素的高度在设计时未知。

  • 最小化窗口时,只有当所有空白都消失时,滚动才会出现。 换句话说,屏幕宽度应表示为:

“leftSpace width=(screenWidth-widthOfCenteredElement)/2”+
"centeredElement width=widthOfCenteredElement"+
"rightSpace width=(screenWidth-widthOfCenteredElement)/2"

同样的高度:

“topSpace height=(screenHeight-heightOfCenteredElement)/2”+
"centeredElement height=heightOfCenteredElement"+
"bottomSpace height=(screenWidth-heightOfCenteredElement)/2"

  • 我所说的实用是指可以使用表格。我打算将此布局主要用于登录等特殊页面。因此 CSS 纯度在这里并不那么重要,而遵循标准对于 future 的兼容性是可取的。

最佳答案

来自 http://www.webmonkey.com/codelibrary/Center_a_DIV

#horizon        
    {
    text-align: center;
    position: absolute;
    top: 50%;
    left: 0px;
    width: 100%;
    height: 1px;
    overflow: visible;
    display: block
    }

#content    
    {
    width: 250px;
    height: 70px;
    margin-left: -125px;
    position: absolute;
    top: -35px;
    left: 50%;
    visibility: visible
    }

<div id="horizon">
   <div id="content">
      <p>This text is<br><emphasis>DEAD CENTRE</emphasis ><br>and stays there!</p>
   </div><!-- closes content-->
</div><!-- closes horizon-->

关于html - 在 FF、IE6 和 IE7 中工作的 HTML 中垂直和水平居中的实用解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49350/

相关文章:

html - 在表格行上使用 flexbox?

html - "Mashable"样式导航栏

css - 更改行流体跨度之间的边距大小

jquery - 拖放到另一个 div li 后,第一个 div 'li' 元素的颜色应该改变

重命名 div 的 JQuery 代码

html - 对齐附加表格旁边的文本

html - 以下图像设计的CSS

html - Firefox 的 Inspector 疯了吗?

javascript - css overflow hidden 增加了容器的高度

css - 我怎样才能让 Chrome 和 Firefox 对待围绕 float 的表格相同?