如何在div中垂直对齐文本?

shi*_*juo 1102 html css vertical-alignment

我试图找到最有效的方法来将文本与div对齐.我尝试了一些东西,似乎都没有用.

.testimonialText {
  position: absolute;
  left: 15px;
  top: 15px;
  width: 150px;
  height: 309px;
  vertical-align: middle;
  text-align: center;
  font-family: Georgia, "Times New Roman", Times, serif;
  font-style: italic;
  padding: 1em 0 1em 0;
}
Run Code Online (Sandbox Code Playgroud)
<div class="testimonialText">
  Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
  in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
Run Code Online (Sandbox Code Playgroud)

Rob*_*vey 750

CSS中的垂直居中
http://www.jakpsatweb.cz/css/css-vertical-center-solution.html

文章摘要:

对于CSS2浏览器,可以使用display:table/ display:table-cell来集中内容.

JSFiddle也提供了样本:

div { border:1px solid green;}
Run Code Online (Sandbox Code Playgroud)
<div style="display: table; height: 400px; overflow: hidden;">
  <div style="display: table-cell; vertical-align: middle;">
    <div>
      everything is vertically centered in modern IE8+ and others.
    </div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

可以将旧浏览器(IE6/7)的黑客合并到样式中,使用#隐藏新浏览器的样式:

div { border:1px solid green;}
Run Code Online (Sandbox Code Playgroud)
<div style="display: table; height: 400px; #position: relative; overflow: hidden;">
  <div style=
    "#position: absolute; #top: 50%;display: table-cell; vertical-align: middle;">
    <div style=" #position: relative; #top: -50%">
      everything is vertically centered
    </div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

  • 我不明白,为什么我们要颠覆这个东西呢?这是21世纪!为什么他们只是不包括一个该死的选项来在css中垂直对齐该死的文本......就像这样:内容居中:两者或垂直或水平.多年来使用的技术是如此愚蠢,这简直令人难以置信. (882认同)
  • 很糟糕的是桌子不受欢迎,因为某种老式的黑客只会让新手使用,而在这里我们使用DIV中的"display:table-cell"作为一种更加黑客的解决方法. (54认同)
  • @Flextra:这就是为什么人们仍然使用表格进行网格布局.使用纯CSS时,垂直对齐(或任何具有高度和动态数据的内容)可能具有挑战性.你必须愿意做这样奇怪的黑客攻击(有点挫败"从布局中分离内容"的想法),或者采取多遍渲染命中并使用非静态表.尽管我经常打破CSS粉丝们的心,但我从未有过最终用户对"桌子"的抱怨.大多数人只设计简单的博客和静态网站.我们中的一些人构建商业软件并需要密集的数据显示,我们的用户更关心功能. (33认同)
  • 是的.不要误会我的意思,如果我建立一个"网站",我会使用精简版HTML并且更喜欢纯CSS,但是我不知道有多少次我用几个小时来填充内容,当我刚刚说'F it'并使用了`table`.现在事情变得更好了,但是我们制作商业网络应用程序的一些人必须支持旧浏览器(IE6仍然在某些客户端使用!),而制作博客的人可以生活在云端并假装世界上的每个人都有快速连接,新计算机和最新版本的X浏览器与CSS 3等.例如:一些Jira插件使用单行表进行布局(或者无论如何) (6认同)
  • 第一个链接更好我认为即使它看起来很复杂,因为第二个链接只使用一行文本或一个你知道高度的图像.如果您有2行或3行文本或者如果您不知道文本的高度,会导致它有所不同.如何使用line-height?所以总结一下.我测试了第一个版本,它适用于IE,Firefox和CHROME. (3认同)
  • 我相信是时候更新已接受的答案,涵盖新的Flexbox方式. (3认同)
  • @ Alexander.Iljushkin不是傻瓜,这不是为此而设计的。CSS是为超链接文本文档而不是富Web应用程序设计的。考虑维基百科,而不是YouTube。是的,CSS很烂,我100%同意。但是没有人预见到网络将成为_application_平台,更不用说_biggest_应用程序平台了。然而,随着React的兴起,这一事实才成为人们关注的焦点,因为它具有分离实际的_concerns_(组件)的哲学,而不是简单地按文件类型(HTML / CSS / JS)进行分离。 (2认同)
  • @Desty 我也这么想,直到我开始研究可访问性。直到我看到屏幕阅读器如何做他们的事情,我才开始接受这种“新”思维方式。 (2认同)

Dav*_*vid 724

您需要添加line-height属性,该属性必须与高度相匹配div.在你的情况下:

.center {
  height: 309px;
  line-height: 309px; /* same as height! */
}
Run Code Online (Sandbox Code Playgroud)
<div class="center">
  A single line.
</div>
Run Code Online (Sandbox Code Playgroud)

实际上,您可以height完全删除该属性.

这只适用于一行文字,所以要小心.

  • 我相信只有在div中有一行文本才有效. (351认同)
  • 当然,如果它在1行以上运行,你的文本中会有疯狂的行间距. (46认同)
  • 它仅适用于指定的高度div (8认同)
  • 不使用百分比:高度:100%; 行高:100% (4认同)
  • line-height传播给每个孩子 (2认同)

Ada*_*mat 445

更新 - 这是一个很好的资源

http://howtocenterincss.com/

以CSS为中心是一个痛苦的屁股.根据各种因素,似乎有很多方法可以做到这一点.这可以整合它们并为您提供每种情况所需的代码.

更新 - 使用Flexbox

为了使这篇文章与最新的技术保持同步,这里有一个更简单的方法来使用flexbox中心化.不支持Flexbox __CODE__.

这里有一些很棒的资源:

jsfiddle与浏览器前缀

HTML

li {
  display: flex;
  justify-content: center;
  align-content: center;
  flex-direction: column;
  /* Column | row */
}
Run Code Online (Sandbox Code Playgroud)

CSS

<ul>
  <li>
    <p>Some Text</p>
  </li>
  <li>
    <p>A bit more text that goes on two lines</p>
  </li>
  <li>
    <p>Even more text that demonstrates how lines can span multiple lines</p>
  </li>
</ul>
Run Code Online (Sandbox Code Playgroud)

更新 - 另一个解决方案

这是来自zerosixthree,让你以6行css为中心

不支持此方法 __CODE__

的jsfiddle

HTML

p {
  text-align: center;
  position: relative;
  top: 50%;
  -ms-transform: translateY(-50%);
  -webkit-transform: translateY(-50%);
  transform: translateY(-50%);
}
Run Code Online (Sandbox Code Playgroud)

CSS

<ul>
  <li>
    <p>Some Text</p>
  </li>
  <li>
    <p>A bit more text that goes on two lines</p>
  </li>
  <li>
    <p>Even more text that demonstrates how lines can span multiple lines</p>
  </li>
</ul>
Run Code Online (Sandbox Code Playgroud)

以前的答案

简单和跨浏览器方法,作为标记答案中的链接有用略微过时.

如何在无序列表和div中垂直和水平居中文本而不使用JavaScript或css行高.无论您有多少文本,您都不必将任何特殊类应用于特定列表或div(每个代码都相同).这适用于所有主流浏览器,包括IE9,IE8,IE7,IE6,Firefox,Chrome,Opera和Safari.由于现代浏览器没有的css限制,有2个特殊样式表(IE7为1,IE6为另一个)以帮助他们.

Andy Howard - 如何在无序列表或div中垂直和水平居中文本

编辑: 因为我对上一个项目的IE7/6并不在意,我使用了一个稍微剥离的版本(即删除了使它在IE7和6中工作的东西).可能对其他人有用......

的jsfiddle

HTML

.outerContainer {
  display: table;
  width: 100px;
  /* Width of parent */
  height: 100px;
  /* Height of parent */
  overflow: hidden;
}

.outerContainer .innerContainer {
  display: table-cell;
  vertical-align: middle;
  width: 100%;
  margin: 0 auto;
  text-align: center;
}

li {
  background: #ddd;
  width: 100px;
  height: 100px;
}
Run Code Online (Sandbox Code Playgroud)

而CSS:

<ul>
  <li>
    <div class="outerContainer">
      <div class="innerContainer">
        <div class="element">
          <p>
            <!-- Content -->
            Content
          </p>
        </div>
      </div>
    </div>
  </li>

  <li>
    <div class="outerContainer">
      <div class="innerContainer">
        <div class="element">
          <p>
            <!-- Content -->
            Content
          </p>
        </div>
      </div>
    </div>
  </li>
</ul>
Run Code Online (Sandbox Code Playgroud)

  • 没问题.我仍然喜欢使用`height:x; line-height:x;`但更常见的是我不需要多行文本.这就是我喜欢这个解决方案的原因.简单,可重复使用,跨浏览器,没有js,只需要一点额外的标记. (2认同)
  • @ThomasDavidBaker,如果你需要这些动态的另一个解决方案是设置100%的高度.[这里是一个jsfiddle](http://jsfiddle.net/Sw3Jd/29/),基本上只是将`height:100px;`改为`height:100%;`为`li`和`.outerContainer`. (2认同)
  • 不想成为一个鼻子挑选者,但在zerosixthree的第二个解决方案中.CSS中有一个拼写错误" - **wekbit**-transform:translateY(-50%);" 还是,非常感谢!! 好方案! (2认同)

小智 179

它很容易display: flex.使用以下方法,div将垂直居中的文本:

div {
  display: -webkit-flex;
  display: flex;
  align-items: center;
  /* More style: */
  height: 300px;
  background-color: #888;
}
Run Code Online (Sandbox Code Playgroud)
<div>
  Your text here.
</div>
Run Code Online (Sandbox Code Playgroud)

如果你想,横向:

div {
  display: -webkit-flex;
  display: flex;
  align-items: center;
  justify-content: center;
  /* More style: */
  height: 300px;
  background-color: #888;
}
Run Code Online (Sandbox Code Playgroud)
<div>
  Your text here.
</div>
Run Code Online (Sandbox Code Playgroud)

您必须看到所需的浏览器版本; 在旧版本中,代码不起作用.


Ste*_*ker 108

我使用以下内容轻松地垂直居中随机元素:

HTML:

<div style="height: 200px">
    <div id="mytext">This is vertically aligned text within a div</div>
</div>
Run Code Online (Sandbox Code Playgroud)

CSS:

#mytext {
    position: relative;
    top: 50%; 
    transform: translateY(-50%);
    -webkit-transform: translateY(-50%);
}
Run Code Online (Sandbox Code Playgroud)

这使我的文本div居中于200px高的外部的精确垂直中间div.请注意,您可能需要使用浏览器前缀(就像-webkit-我的情况一样)才能使其适用于您的浏览器.

这不仅适用于文本,也适用于其他元素.

  • 我用'position:absolute;'得到了更可靠的结果. - 谢谢!*NB您可能希望包含`-ms-transform`或IE将填充其他内容* (3认同)
  • 我认为这是最好的答案,即使在绝对div内也能100%工作 (3认同)

Sha*_*ina 38

您可以通过将显示设置为"table-cell"并应用vertical-align:middle来完成此操作;

    {
        display: table-cell;
        vertical-align: middle;
    }
Run Code Online (Sandbox Code Playgroud)

但是,根据我在未经许可的情况下从http://www.w3schools.com/cssref/pr_class_display.asp复制的摘录,所有版本的Internet Explorer都不支持此功能.

注意:值"inline-table","table","table-caption","table-cell","table-column","table-column-group","table-row","table-row IE7及更早版本不支持-group"和"inherit".IE8需要一个!DOCTYPE.IE9支持这些值.

下表显示了http://www.w3schools.com/cssref/pr_class_display.asp中允许的显示值.我希望这有帮助

在此输入图像描述

  • 很好的主意!显示:table-cell有其他效果,但如果你对它们没问题,这是一个很好的解决方案 (2认同)

Ton*_*bet 32

UPD: 这几天(我们不再需要IE6-7-8)我只是使用css display:table来解决这个问题

.vcenter {
    display: table;
    background: #eee; /* optional */
    width: 150px;
    height: 150px;
    text-align: center; /* optional */
}

.vcenter > :first-child {
    display: table-cell;
    vertical-align: middle;
}
Run Code Online (Sandbox Code Playgroud)
<div class="vcenter">
  <p>This is my Text</p>
</div>
Run Code Online (Sandbox Code Playgroud)


这是我对这个问题最喜欢的解决方案(简单且非常好的浏览器支持):

.vcenter {
    display: flex; /* <-- Here */
    align-items: center; /* <-- Here */
    justify-content: center; /* optional */
    height: 150px; /* <-- Here */
    background: #eee;  /* optional */
    width: 150px;
}
Run Code Online (Sandbox Code Playgroud)
<div class="vcenter">
  <p>This is my text</p>
</div>
Run Code Online (Sandbox Code Playgroud)

  • @Imray他基本上在文本旁边添加一个0px宽度的内联块元素(伪:之前).然而,这个人造块的高度为100%,这就是诀窍.由于块和文本都是v对齐的,因此它会按预期呈现. (2认同)

Mim*_*ina 21

Flexbox非常适合我,将父 div 中的多个元素水平和垂直居中。

HTML代码:

<div class="parent-div">
    <div class="child-div">
      <a class="footer-link" href="https://www.github.com/">GitHub</a>
      <a class="footer-link" href="https://www.facebook.com/">Facebook</a>
      <p class="footer-copywrite">© 2019 Lorem Ipsum.</p>
    </div>
  </div>
Run Code Online (Sandbox Code Playgroud)

CSS-Code:
下面的代码将 parent-div 内的所有元素堆叠在一列中,将元素水平和垂直居中。我使用 child-div 将两个 Anchor 元素保持在同一行(行)上。如果没有 child-div,则所有三个元素(Anchor、Anchor 和 Paragraph)都堆叠在 parent-div 的列中。这里只有 child-div 堆叠在 parent-div 列中。

/* */
.parent-div {
  height: 150px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}
Run Code Online (Sandbox Code Playgroud)


Kam*_*esh 15

非常非常简单的解决方案,我在我的项目中多次使用它:

源代码

.welcome_box{
    height: 300px;
    display: flex;
    justify-content: center;
    align-content: center;
    flex-direction: column;
    border: solid 1px #dadada;
}

<div class="welcome_box">Hello Friends</div>
Run Code Online (Sandbox Code Playgroud)

输出:

在此输入图像描述

谢谢你提出这个问题。乐于分享。


Ker*_*092 14

没有一个答案帮助了我,尝试这个,添加父div:

display: flex;
align-items: center;
Run Code Online (Sandbox Code Playgroud)


Cra*_*aig 8

这是一个最适合单行文本的解决方案.

如果已知行数,它也适用于多行文本,并进行一些调整

.testimonialText {
    font-size: 1em; /* Set a font size */
}
.testimonialText:before { /* Add a pseudo element */
    content: "";
    display: block;
    height: 50%;
    margin-top: -0.5em; /* Half of the font size */
}
Run Code Online (Sandbox Code Playgroud)

这是一个JSFiddle


Nik*_*iya 8

<!DOCTYPE html>
<html>

  <head>
    <style>
      .container {
        height: 250px;
        background: #f8f8f8;
        display: -ms-flexbox;
        display: -webkit-flex;
        display: flex;
        -ms-flex-align: center;
        align-items: center;
        -webkit-box-align: center;
        justify-content: center;
      }
      p{
        font-size: 24px;
      }
    </style>
  </head>

  <body>
    <div class="container">
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
    </div>
  </body>

</html>
Run Code Online (Sandbox Code Playgroud)


Cya*_*zar 7

您可以使用flexbox在div内垂直对齐中心文本.

<div>
   <p class="testimonialText">This is the testimonial text.</p>
</div>

div {
   display: flex;
   align-items: center;
}
Run Code Online (Sandbox Code Playgroud)

您可以在以下链接中了解有关它的更多信息:https: //css-tricks.com/snippets/css/a-guide-to-flexbox/


小智 6

检查一下这个简单的解

HTML

<div class="block-title"><h3>I'm a vertically centered element</h3></div>
Run Code Online (Sandbox Code Playgroud)

CSS

.block-title {
    float: left;
    display: block;
    width: 100%;
    height: 88px
}

.block-title h3 {
    display: table-cell;
    vertical-align: middle;
    height: inherit
}
Run Code Online (Sandbox Code Playgroud)

的jsfiddle


小智 5

有一种更简单的方法可以垂直对齐内容,而无需使用table / table-cell:

http://jsfiddle.net/bBW5w/1/

我在其中添加了一个不可见的(width = 0)div,它假定了容器的整个高度。

它似乎可以在Internet Explorer和Firefox(最新版本)中使用。我没有与其他浏览器核对

  <div class="t">
     <div>
         everything is vertically centered in modern IE8+ and others.
     </div>
      <div></div>
   </div>
Run Code Online (Sandbox Code Playgroud)

当然还有CSS:

.t, .t > div:first-child
{
    border: 1px solid green;
}
.t
{
    height: 400px;
}
.t > div
{
    display: inline-block;
    vertical-align: middle
}
.t > div:last-child
{
    height: 100%;
}
Run Code Online (Sandbox Code Playgroud)


Jus*_*tin 5

这是我发现的最干净的解决方案(IE9 +),并通过使用transform-style省略的其他答案添加了"off by .5 pixel"问题的修复程序.

.parent-element {
  -webkit-transform-style: preserve-3d;
  -moz-transform-style: preserve-3d;
  transform-style: preserve-3d;
}

.element {
  position: relative;
  top: 50%;
  -webkit-transform: translateY(-50%);
  -ms-transform: translateY(-50%);
  transform: translateY(-50%);
}
Run Code Online (Sandbox Code Playgroud)

资料来源:http://zerosixthree.se/vertical-align-anything-with-just-3-lines-of-css/


ezz*_*ato 5

使用 CSS 网格为我做到了:

.outer {
    background-color: grey;
    width: 10rem;
    height: 10rem;
    display: grid;
    justify-items: center;
}

.inner {
    background-color: #FF8585;
    align-self: center;
}
Run Code Online (Sandbox Code Playgroud)
<div class='outer'>
    <div class='inner'>
       Content
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)


Mal*_*ith 5

HTML :

<div class="col-md-2 ml-2 align-middle">
    <label for="option2" id="time-label">Time</label>
</div>
Run Code Online (Sandbox Code Playgroud)

CSS :

.align-middle {
    margin-top: auto;
    margin-bottom: auto;
}
Run Code Online (Sandbox Code Playgroud)


Bla*_*gas 5

您可以使用显示网格和放置项目中心:

.container {
  width: 200px;
  height: 200px;
  border: solid red;
  display: grid;
  place-items: center;
}
Run Code Online (Sandbox Code Playgroud)
<div class="container">
  Lorem, ipsum dolor.
</div>
Run Code Online (Sandbox Code Playgroud)