167

当内容的高度可变时,将 div 的内容垂直居中的最佳方法是什么。在我的特定情况下,容器 div 的高度是固定的,但是如果有一种解决方案可以在容器也具有可变高度的情况下工作,那就太好了。另外,我希望有一个不使用或很少使用 CSS hack 和/或非语义标记的解决方案。

替代文字

4

9 回答 9

170

只需添加

position: relative;
top: 50%;
transform: translateY(-50%);

到内部 div。

它所做的是将内部 div 的顶部边框移动到外部 div ( top: 50%;) 的一半高度,然后将内部 div 向上移动一半高度 ( transform: translateY(-50%))。这将与position: absoluteor一起使用relative

请记住这一点,transformtranslate具有为简单起见未包括在内的供应商前缀。

代码笔: http ://codepen.io/anon/pen/ZYprdb

于 2014-12-27T00:15:48.383 回答
159

只要您的浏览器支持::before伪元素,这似乎是我发现的最佳解决方案:CSS-Tricks: Centering in the Unknown

它不需要任何额外的标记,而且似乎工作得非常好。我无法使用该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;
}
<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>

于 2012-10-25T19:35:36.820 回答
16

这是我需要做很多次的事情,并且一致的解决方案仍然需要您添加一些非语义标记和一些特定于浏览器的 hack。当我们获得对 css 3 的浏览器支持时,您将获得垂直居中而不会犯错。

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

<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>

有很多方法(hacks)可以在特定的浏览器集中应用样式。我使用了条件注释,但请查看上面链接的文章以了解其他两种技术。

注意:如果您事先知道一些高度,如果您试图将单行文本居中,或者在其他几种情况下,有一些简单的方法可以实现垂直居中。如果您有更多详细信息,请把它们扔进去,因为可能有一种方法不需要浏览器黑客或非语义标记。

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

于 2008-09-12T15:34:31.743 回答
13

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

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

于 2020-05-20T21:42:22.477 回答
10

使用子选择器,我采用了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;
}
<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>

分叉的 CodePen:http ://codepen.io/dougli/pen/Eeysg

于 2013-12-06T21:48:17.417 回答
4

到目前为止对我来说最好的结果:

div居中:

position: absolute;
top: 50%;
transform: translateY(-50%);
margin: 0 auto;
right: 0;
left: 0;
于 2016-11-21T04:30:37.203 回答
3

您可以使用边距自动。使用 flex,div 似乎也垂直居中。

body,
html {
  height: 100%;
  margin: 0;
}
.site {
  height: 100%;
  display: flex;
}
.site .box {
  background: #0ff;
  max-width: 20vw;
  margin: auto;
}
<div class="site">
  <div class="box">
    <h1>blabla</h1>
    <p>blabla</p>
    <p>blablabla</p>
    <p>lbibdfvkdlvfdks</p>
  </div>
</div>
于 2016-05-18T09:00:43.847 回答
1

对我来说,最好的方法是:

.container{
  position: relative;
}

.element{
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
}

优点是不必明确高度

于 2019-09-27T18:10:37.883 回答
0

这是我对于div具有动态(百分比)高度的出色解决方案。

CSS

.vertical_placer{
  background:red;
  position:absolute; 
  height:43%; 
  width:100%;
  display: table;
}

.inner_placer{  
  display: table-cell;
  vertical-align: middle;
  text-align:center;
}

.inner_placer svg{
  position:relative;
  color:#fff;
  background:blue;
  width:30%;
  min-height:20px;
  max-height:60px;
  height:20%;
}

HTML

<div class="footer">
    <div class="vertical_placer">
        <div class="inner_placer">
            <svg> some Text here</svg>
        </div>
    </div>
</div>  

自己试试这个。

于 2014-09-30T16:24:05.760 回答