如何在div中垂直居中具有可变高度的内容?

jes*_*vin 156 css vertical-alignment

当内容的高度可变时,垂直居中div内容的最佳方法是什么.在我的特殊情况下,容器div的高度是固定的,但如果有一个解决方案可以在容器具有可变高度的情况下工作,那将是很好的.此外,我想要一个没有或很少使用CSS黑客和/或非语义标记的解决方案.

替代文字

小智 157

这似乎是我发现这个问题的最佳解决方案,只要你的浏览器支持::before伪元素:CSS-Tricks:以未知为中心.

它不需要任何额外的标记,似乎工作得非常好.我无法使用该display: table方法,因为table元素不遵守该max-height属性.

.block {
  height: 300px;
  text-align: center;
  background: #c0c0c0;
  border: #a0a0a0 solid 1px;
  margin: 20px;
}

.block::before {
  content: '';
  display: inline-block;
  height: 100%; 
  vertical-align: middle;
  margin-right: -0.25em; /* Adjusts for spacing */

  /* For visualization 
  background: #808080; width: 5px;
  */
}

.centered {
  display: inline-block;
  vertical-align: middle;
  width: 300px;
  padding: 10px 15px;
  border: #a0a0a0 solid 1px;
  background: #f5f5f5;
}
Run Code Online (Sandbox Code Playgroud)
<div class="block">
    <div class="centered">
        <h1>Some text</h1>
        <p>But he stole up to us again, and suddenly clapping his hand on my
           shoulder, said&mdash;"Did ye see anything looking like men going
           towards that ship a while ago?"</p>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

  • 现在 - 这是一个-BRILLIANT-解决方案.非常感谢您发布此内容! (3认同)
  • +1,这个解决方案非常巧妙!顺便说一句,你可以添加`.block {white-space:nowrap; font-size:0; line-height:0; 并且在伪元素上留下负右边距,你只需要在.centered上重置fs和lh ......就像这样你确保元素正确定位,即使它比视口宽(并且不必须使用imo有点凌乱的负边缘). (3认同)

Bla*_*tha 139

只需添加

position: relative;
top: 50%;
transform: translateY(-50%);
Run Code Online (Sandbox Code Playgroud)

内心的div.

它的作用是将内部div的顶部边框移动到外部div(top: 50%;)的半高,然后将内部div移动高度的一半(transform: translateY(-50%)).这将适用于position: absoluterelative.

请记住,transform并且translate有供应商前缀,为简单起见,不包括这些前缀.

Codepen:http://codepen.io/anon/pen/ZYprdb

  • 只是想知道为什么这适用于Chrome和Firefox,但不适用于Safari.然后我注意到大多数`transform`相关的东西是`-webkit -`前缀,现在它可以正常工作.所以提醒一下:不要忘记添加`-webkit -`前缀. (6认同)
  • 这个答案应该在顶部. (6认同)
  • 使用https://github.com/postcss/autoprefixer等工具为您处理前缀. (5认同)
  • 这种方法在Chrome中有一个不幸的副作用 - 如果你的包装器有一个奇数个像素,那么`translate`会在它试图处理.5px高度时渲染所有内容. (4认同)
  • 这应该有更多的赞成!顺便说一句,它也适用于`position:absolute`,不是吗? (3认同)

Pre*_*aul 16

这是我需要多次执行的操作,一致的解决方案仍然需要添加一些非语义标记和一些特定于浏览器的黑客.当我们获得css 3的浏览器支持时,你将获得垂直居中而不会犯罪.

为了更好地解释该技术,你可以看一下我改编它的文章,但基本上它涉及添加一个额外的元素,并在IE和支持position:table\table-cell非表元素的浏览器中应用不同的样式.

<div class="valign-outer">
    <div class="valign-middle">
        <div class="valign-inner">
            Excuse me. What did you sleep in your clothes again last night. Really. You're gonna be in the car with her. Hey, not too early I sleep in on Saturday. Oh, McFly, your shoe's untied. Don't be so gullible, McFly. You got the place fixed up nice, McFly. I have you're car towed all the way to your house and all you've got for me is light beer. What are you looking at, butthead. Say hi to your mom for me.
        </div>
    </div>
</div>

<style>
    /* Non-structural styling */
    .valign-outer { height: 400px; border: 1px solid red; }
    .valign-inner { border: 1px solid blue; }
</style>

<!--[if lte IE 7]>
<style>
    /* For IE7 and earlier */
    .valign-outer { position: relative; overflow: hidden; }
    .valign-middle { position: absolute; top: 50%; }
    .valign-inner { position: relative; top: -50% }
</style>
<![endif]-->
<!--[if gt IE 7]> -->
<style>
    /* For other browsers */
    .valign-outer { position: static; display: table; overflow: hidden; }
    .valign-middle { position: static; display: table-cell; vertical-align: middle; width: 100%; }
</style>
Run Code Online (Sandbox Code Playgroud)

在特定的浏览器集中应用样式有很多方法(黑客).我使用了有条件的评论,但看看上面链接的文章,看看另外两种技巧.

注意:如果您事先知道某些高度,如果您试图将单行文本居中,或者在其他几种情况下,可以通过简单的方法获得垂直居中.如果你有更多的细节然后扔进去,因为可能有一个方法不需要浏览器黑客或非语义标记.

更新:我们开始获得更好的浏览器支持CSS3,将flex-box和变换作为获得垂直居中(以及其他效果)的替代方法.有关现代方法的更多信息,请参阅此其他问题,但请记住,CSS3的浏览器支持仍然是粗略的.

  • 不,我错了.那个方法完全有效! (3认同)

has*_*efi 11

您可以使用弹性显示,例如以下代码:

.example{
  background-color:red;
  height:90px;
  width:90px;
  display:flex;
  align-items:center; /*for vertically center*/
  justify-content:center; /*for horizontally center*/
}
Run Code Online (Sandbox Code Playgroud)
<div class="example">
    <h6>Some text</h6>
</div>
Run Code Online (Sandbox Code Playgroud)


小智 9

使用子选择器,我已经把Fadi上面的令人难以置信的答案归结为一个我可以应用的CSS规则.现在,我所要做的就是将contentCentered类名添加到我想要居中的元素中:

.contentCentered {
  text-align: center;
}

.contentCentered::before {
  content: '';
  display: inline-block;
  height: 100%; 
  vertical-align: middle;
  margin-right: -.25em; /* Adjusts for spacing */
}

.contentCentered > * {
  display: inline-block;
  vertical-align: middle;
}
Run Code Online (Sandbox Code Playgroud)
<div class="contentCentered">
  <div>
    <h1>Some text</h1>
    <p>But he stole up to us again, and suddenly clapping his hand on my
      shoulder, said&mdash;"Did ye see anything looking like men going
      towards that ship a while ago?"</p>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

Forked CodePen:http://codepen.io/dougli/pen/Eeysg