【问题标题】:how to vertically center an element in IE7 and IE6?如何在 IE7 和 IE6 中垂直居中元素?
【发布时间】:2012-08-30 06:32:05
【问题描述】:

我有一个没有固定高度的 div 元素,我想让它在其父元素内垂直居中而不管它的高度。我在现代浏览器中使用display: table-cell 技术,但似乎这种技术在 IE7 及更低版本中不起作用。有没有办法在IE7及以下使用纯CSS实现这种垂直居中的效果?提前致谢。

【问题讨论】:

  • 您能给我们您的代码、jsfiddle 或您页面的链接吗?
  • 不确定是否可以通过其他方式实现:jsfiddle.net/9TE5t(此处为父元素上的padding:40% 0;
  • @Stano 但是包装器的高度取决于它的宽度......你想要吗?

标签: css internet-explorer-7 internet-explorer-6


【解决方案1】:

您可以将容器的height 值设置为其line-height 属性,并将display:inline-block 设置为居中元素。

HTML:

<div id="container">
    <span>content</span>
</div>

CSS:

#container {
    display: table-cell; vertical-align:middle;
    border:1px solid red;
    height:300px;
    background-color:green;
    width:400px;
    text-align:center;
    line-height:300px; /* Same as height */
}
#container>span{
    background-color:lightblue;
    height:50px;
    width:100px;
    line-height:normal;
    display:inline-block; 
    vertical-align:middle;
}

在这里查看:http://jsfiddle.net/9TE5t/4/

但请注意,在 IE 7 及更早版本中,如果您将 display:inline-block 设置为默认块元素(例如 &lt;div&gt;),其行为将类似于 display:block

然后,您可以使用默认的内联元素(例如&lt;span&gt;),或者如果浏览器是IE7 或更早版本,则可以使用IE 条件cmets 并设置display:inline(它们的行为类似于display:inline-block)。

【讨论】:

猜你喜欢
  • 2011-11-07
  • 1970-01-01
  • 1970-01-01
  • 2014-04-05
  • 1970-01-01
  • 2011-11-28
  • 2013-10-27
  • 1970-01-01
相关资源
最近更新 更多