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

Html 如何在div中垂直对齐文本?,html,css,vertical-alignment,Html,Css,Vertical Alignment,我正试图找到最有效的方法将文本与div对齐。我尝试了一些方法,但似乎没有一种有效 。推荐文本{ 位置:绝对位置; 左:15px; 顶部:15px; 宽度:150px; 高度:309px; 垂直对齐:中间对齐; 文本对齐:居中; 字体系列:佐治亚州,“泰晤士报新罗马”,泰晤士报,衬线; 字体:斜体; 填充:1em 0 1em 0; } 知识产权是一种权利,是一种精英的权利,是劳动和财富的暂时性权利。但是,在最低限度上,我们需要一个实验室来进行日常工作。二人世界 这是一种令人厌恶的行为,因为它是

我正试图找到最有效的方法将文本与div对齐。我尝试了一些方法,但似乎没有一种有效

。推荐文本{
位置:绝对位置;
左:15px;
顶部:15px;
宽度:150px;
高度:309px;
垂直对齐:中间对齐;
文本对齐:居中;
字体系列:佐治亚州,“泰晤士报新罗马”,泰晤士报,衬线;
字体:斜体;
填充:1em 0 1em 0;
}

知识产权是一种权利,是一种精英的权利,是劳动和财富的暂时性权利。但是,在最低限度上,我们需要一个实验室来进行日常工作。二人世界
这是一种令人厌恶的行为,因为它是一种不平等的行为。除偶尔因疏忽而死亡外,不得因疏忽而导致动物死亡。
在现代浏览器中执行此操作的正确方法是使用Flexbox。 有关详细信息,请参阅

有关在旧浏览器中工作的一些旧方法,请参见下文


CSS中的垂直居中

文章摘要:

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

样本也可从以下网址获得:

div{边框:1px纯绿色;}

在现代IE8+和其他应用程序中,所有内容都垂直居中。

您需要添加
行高度
属性,该属性必须与
div的高度相匹配。就你而言:

.center{
高度:309px;
线条高度:309px;/*与高度相同*/
}

一行。

您可以通过将显示设置为“表格单元格”并应用
垂直对齐:中间

    {
        display: table-cell;
        vertical-align: middle;
    }
然而,根据我未经许可复制的这段摘录,并非所有版本的Internet Explorer都支持这一点

注意:InternetExplorer7及更早版本不支持值“内联表”、“表”、“表标题”、“表单元格”、“表列”、“表列组”、“表行组”和“继承”。InternetExplorer8需要一个!DOCTYPE。InternetExplorer9支持这些值

下表显示了允许的显示值

这里有一个很好的资源 发件人:

以CSS为中心是一件让人头疼的事。根据各种因素,似乎有无数种方法可以做到这一点。这将整合它们,并为您提供每种情况所需的代码

使用Flexbox 为了让这篇文章与最新技术保持同步,这里有一个更简单的方法,可以使用Flexbox将内容居中。InternetExplorer9及更低版本不支持Flexbox

以下是一些很棒的资源:

li{
显示器:flex;
证明内容:中心;
对齐内容:居中对齐;
弯曲方向:立柱;
/*列|行*/
}
  • 一些文本

  • 多一点文字,两行

  • 更多的文本演示了行如何跨越多行


尝试嵌入表元素


哥本哈根

我使用以下方法轻松地将随机元素垂直居中:

HTML:

这是div中垂直对齐的文本
CSS:
#mytext{
位置:相对位置;
最高:50%;
转化:translateY(-50%);
-webkit转换:translateY(-50%);
}
这将使my
div
中的文本居中于200px高的外部
div
的准确垂直中间。请注意,您可能需要使用浏览器前缀(在我的示例中,如
-webkit-
)才能使其适用于您的浏览器


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

嗯,显然有很多方法可以解决这个问题

但我有一个绝对定位的
高度:100%
(实际上,
顶部:0;底部:0
和固定宽度)和
显示:表格单元格
无法垂直居中文本。我的解决方案确实需要一个内部跨度元素,但我看到许多其他解决方案也需要,因此我不妨添加它:

我的容器是一个
.label
,我希望数字垂直居中。我通过绝对定位在
顶部:50%
并设置
行高:0

<div class="label"><span>1.</span></div>

查看具体操作:

有一种更简单的方法可以垂直对齐内容,而无需借助表格/表格单元格:

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

它似乎在InternetExplorer和Firefox(最新版本)中工作。我没有检查其他浏览器

  <div class="t">
     <div>
         everything is vertically centered in modern IE8+ and others.
     </div>
      <div></div>
   </div>

检查此简单解决方案:

HTML
这是
div
模式中使用CSS中的
calc()
div
的另一个变体

<div style="height:300px; border:1px solid green;">
  Text in outer div.
  <div style="position:absolute; height:20px; top:calc(50% - 10px); border:1px solid red;)">
    Text in inner div.
  </div>
</div>

外分区中的文本。
内分区中的文本。
这是可行的,因为:

  • position:absolute
    用于将
    div
    精确放置在
    div
  • 我们知道内部
    div
    的高度,因为我们将其设置为
    20px
  • calc(50%-10px)
    用于50%-一半高度用于将内部
    div居中
    • HTML

      <div class="relative"><!--used as a container-->
          <!-- add content here to to make some height and width
          example:<img src="" alt=""> -->
          <div class="absolute">
              <div class="table">
                  <div class="table-cell">
                      Vertical contents goes here
                  </div>
              </div>
          </div>
      </div>
      
      <div class="main">
          <div class="center">
              whatever
          </div>
      </div>
      
      现在(我们不再需要InternetExplorer 6-7-8)我只会使用CSS
      display:table
      (或者
      display:flex
      )来解决这个问题


      对于较旧的浏览器: 表:
      .vcenter{
      显示:表格;
      背景:#eee;/*可选*/
      宽度:150px;
      高度:150像素;
      文本对齐:居中;/*可选*/
      }
      .vcenter>:第一个孩子{
      显示:表格单元格;
      垂直对齐:中间对齐;
      }
      
      这是我的文本

      <
      .block-title {
          float: left;
          display: block;
          width: 100%;
          height: 88px
      }
      
      .block-title h3 {
          display: table-cell;
          vertical-align: middle;
          height: inherit
      }
      
      <div style="height:300px; border:1px solid green;">
        Text in outer div.
        <div style="position:absolute; height:20px; top:calc(50% - 10px); border:1px solid red;)">
          Text in inner div.
        </div>
      </div>
      
      <div class="relative"><!--used as a container-->
          <!-- add content here to to make some height and width
          example:<img src="" alt=""> -->
          <div class="absolute">
              <div class="table">
                  <div class="table-cell">
                      Vertical contents goes here
                  </div>
              </div>
          </div>
      </div>
      
       .relative {
           position: relative;
       }
       .absolute {
           position: absolute;
           top: 0;
           bottom: 0;
           left: 0;
           right: 0;
           background: rgba(0, 0, 0, 0.5);
       }
       .table {
           display: table;
           height: 100%;
           width: 100%;
           text-align: center;
           color: #fff;
       }
       .table-cell {
           display: table-cell;
           vertical-align: middle;
       }
      
      <div class="Center-Container is-Table">
        <div class="Table-Cell">
          <div class="Center-Block">
              CONTENT
          </div>
        </div>
      </div>
      
      .Center-Container.is-Table { display: table; }
      .is-Table .Table-Cell {
        display: table-cell;
        vertical-align: middle;
      }
      .is-Table .Center-Block {
        width: 50%;
        margin: 0 auto;
      }
      
      <div class="Center-Container is-Inline">
        <div class="Center-Block">
           CONTENT
        </div>
      </div>
      
      .Center-Container.is-Inline {
        text-align: center;
        overflow: auto;
      }
      
      .Center-Container.is-Inline:after,
      .is-Inline .Center-Block {
        display: inline-block;
        vertical-align: middle;
      }
      
      .Center-Container.is-Inline:after {
        content: '';
        height: 100%;
        margin-left: -0.25em; /* To offset spacing. May vary by font */
      }
      
      .is-Inline .Center-Block {
        max-width: 99%; /* Prevents issues with long content causes the content block to be pushed to the top */
        /* max-width: calc(100% - 0.25em) /* Only for Internet&nbsp;Explorer 9+ */
      }
      
      <div style="position: relative; background: #ddd; border: 1px solid #ddd; height: 250px;">
        <div style="width: 50%; height: 60%; overflow: auto; margin: auto; position: absolute; top: 0; left: 0; bottom: 0; right: 0; background: #ccc; text-align: center;">
          <h4>ABSOLUTE CENTER, <br/>
      WITHIN CONTAINER.</h4>
          <p>This box is absolutely centered, horizontally and vertically, within its container</p>
        </div>
      </div>
      
      .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 */
      }
      
      .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%);
      }
      
      <div class="main">
          <div class="center">
              whatever
          </div>
      </div>
      
      .main {
          position: relative
      }
      
      .center {
          position: absolute;
          top: 50%;
          left: 50%;
          transform: translate(-50%, -50%);
          -webkit-transform: translate(-50%, -50%);
          -moz-transform: translate(-50%, -50%);
          -o-transform: translate(-50%, -50%);
      }
      
      <div>
         <p class="testimonialText">This is the testimonial text.</p>
      </div>
      
      div {
         display: flex;
         align-items: center;
      }
      
      <!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>
      
      <div class="cells-block">    
          text<br/>in the block   
      </div>
      
      .cells-block {
          display: flex;
          flex-flow: column;
          align-items: center;       /* Vertically   */
          justify-content: flex-end; /* Horisontally */
          text-align: right;         /* Addition: for text's lines */
      }
      
      <div class="information">
          <span>Some text</span>
          <mat-icon>info_outline</mat-icon>
      </div>
      
      .information {
          display: inline-block;
          padding: 4px 0;
          span {
              display: inline-block;
              vertical-align: middle;
          }
          mat-icon {
              vertical-align: middle;
          }
      }
      
      display: flex;
      align-items: center;
      
      .container {
        display: grid;
      }
      .element {
        margin: auto;
      }
      
      <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>
      
      /* */
      .parent-div {
        height: 150px;
        display: flex;
        flex-direction: column;
        justify-content: center;
      }
      
      <div class="col-md-2 ml-2 align-middle">
          <label for="option2" id="time-label">Time</label>
      </div>
      
      .align-middle {
          margin-top: auto;
          margin-bottom: auto;
      }