【发布时间】:2013-04-06 04:08:25
【问题描述】:
我试图在 div 中居中显示图像,我使用了 display:table & display: table-cell 方法。它居中,但距离顶部有几个像素。我能做些什么来纠正这个问题?
CSS
html, body {
width: 100%;
height: 100%;
}
body,html {
margin: 0;
padding: 0;
color:#ffffff;
text-align: center;
}
#wrapper{
text-align: left;
width: 940px;
margin: 0 auto;
margin-top: 22px;
background-color: #ffffff;
overflow: hidden;
}
header {
width: 908px;
height: 76px;
display: table;
border-style:solid;
border-top-width: 16px;
border-bottom-width: 16px;
border-top-color: #ffffff;
background-color: #cccccc;
}
#headerdiv { display: table-cell; vertical-align: middle; margin: 0; height: 28px; }
HTML
<div id="wrapper">
<header>
<div id="headerdiv"><img src="logotest.gif" height="28" width="180" alt="test"></div>
</header>
</div>
【问题讨论】:
-
我用你的问题做了一个 JSBIN 但我看不出你的问题是什么,jsbin.com/aroyej/1/edit也许你遇到了继承问题
-
将 Natalia 的 JSBIN 的屏幕截图粘贴到 Photoshop 中显示图像周围的顶部和底部空间相同(在我的情况下为 8px)。
-
@Natalia - 您看不到问题可能是有原因的。 JSBin 的结果 iframe 中的源在 doctype 之前有一个
<script>行,导致 iframe 以 quirks 模式呈现。在 jsfiddle 中尝试相同的代码,您会看到偏移量。
标签: css html vertical-alignment