html – 如何垂直居中的H1在一个div?

前端之家收集整理的这篇文章主要介绍了html – 如何垂直居中的H1在一个div?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
首先,我道歉。我知道这里有各种各样的解决方案,但对于我来说,我无法让任何人上班。

对于一个敏感的网站,我试图将一个h1放在一个div中。

水平居中不是一个问题,但是我在垂直居中时遇到问题。大概我在做错什么或误会我在这里发现的解释(或者也许两者)。

所以我可能会以错误的方式来解释早期的解决方案,有人可以解释一下,我必须添加到下面的代码才能使h1垂直中心?

(为了使这个问题与尽可能多的人相关,我已经剥离了我之前尝试过的所有代码。)

CSS:

  1. html,body {
  2. height: 100%;
  3. margin: 0;
  4. }
  5.  
  6. #section1 {
  7. min-height: 90%;
  8. text-align:center
  9. }

HTML:

  1. <div class="section" id="section1">
  2. <h1>Lorem ipsum</h1>
  3. </div>

解决方法

您可以实现与显示器的垂直对齐:table-cell:
  1. #section1 {
  2. height: 90%;
  3. text-align:center;
  4. display:table;
  5. width:100%;
  6. }
  7.  
  8. #section1 h1 {display:table-cell; vertical-align:middle}

Example

更新 – CSS3

对于垂直对齐的另一种方法,可以使用以下所有最新浏览器中应支持的css 3:

  1. #section1 {
  2. height: 90%;
  3. width:100%;
  4. display:flex;
  5. align-items: center;
  6. justify-content: center;
  7. }

Updated fiddle

猜你在找的HTML相关文章