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

时间:2008-09-12 15:29:55

标签: css vertical-alignment

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

alt text

9 个答案:

答案 0 :(得分:157)

这似乎是我发现此问题的最佳解决方案,只要您的浏览器支持::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>

答案 1 :(得分:131)

添加

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

到内部div。

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

请注意,transformtranslate都有供应商前缀,为简单起见,这些前缀不包括在内。

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

答案 2 :(得分:16)

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

为了更好地解释该技术,您可以查看the article I adapted it from,但基本上它涉及添加额外元素并在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>

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

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

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

答案 3 :(得分:11)

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

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

答案 4 :(得分:9)

使用子选择器,我已经上面的Fadi's incredible answer并将其简化为一个我可以应用的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>

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

答案 5 :(得分:4)

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

div居中:

position: absolute;
top: 50%;
transform: translateY(-50%);
margin: 0 auto;
right: 0;
left: 0;

答案 6 :(得分:2)

您可以使用保证金自动。对于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>

答案 7 :(得分:1)

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

.container{
  position: relative;
}

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

优点是不必明确显示高度

答案 8 :(得分: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>  

Try this by yourself.