【问题标题】:Display: Table-cell doesn't center the div显示:表格单元格不使 div 居中
【发布时间】: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 之前有一个 &lt;script&gt; 行,导致 iframe 以 quirks 模式呈现。在 jsfiddle 中尝试相同的代码,您会看到偏移量。

标签: css html vertical-alignment


【解决方案1】:

添加

img {
    vertical-align:middle;
}

jsFiddle example

【讨论】:

  • ... 或任何其他从图像下方移除字体下降间隙的解决方案。
猜你喜欢
  • 1970-01-01
  • 2013-02-14
  • 2013-02-08
  • 1970-01-01
  • 2014-09-03
  • 1970-01-01
  • 1970-01-01
  • 2012-04-14
  • 2016-12-19
相关资源
最近更新 更多