【问题标题】:How to center an element horizontally and vertically如何使元素水平和垂直居中
【发布时间】:2013-10-27 23:56:05
【问题描述】:

我试图将标签内容垂直居中,但是当我添加 CSS 样式 display:inline-flex 时,水平文本对齐消失了。

如何为每个选项卡设置 x 和 y 文本对齐方式?

* { box-sizing: border-box; }
#leftFrame {
  background-color: green;
  position: absolute;
  left: 0;
  right: 60%;
  top: 0;
  bottom: 0;
}
#leftFrame #tabs {
  background-color: red;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 25%;
}
#leftFrame #tabs div {
  border: 2px solid black;
  position: static;
  float: left;
  width: 50%;
  height: 100%;
  text-align: center;
  display: inline-flex;
  align-items: center;
}
<div id=leftFrame>
  <div id=tabs>
    <div>first</div>
    <div>second</div>
  </div>
</div>

【问题讨论】:

标签: html css


【解决方案1】:

.container {
    position: absolute;
    top: 50%;
    left: 50%;
    -moz-transform: translateX(-50%) translateY(-50%);
    -webkit-transform: translateX(-50%) translateY(-50%);
    transform: translateX(-50%) translateY(-50%);
}
<div class="container">
    <span>I'm vertically/horizontally centered!</span>
</div>

  • 方法 2 - Flexbox 方法:

    Example Here/Full Screen Example

    supported browsers 中,将目标元素的display 设置为flex,并使用align-items: center 进行垂直居中,使用justify-content: center 进行水平居中。只是不要忘记添加供应商前缀以获得额外的浏览器支持 (see example)。

html, body, .container {
    height: 100%;
}

.container {
    display: -webkit-flexbox;
    display: -ms-flexbox;
    display: -webkit-flex;
    display: flex;
    -webkit-flex-align: center;
    -ms-flex-align: center;
    -webkit-align-items: center;
    align-items: center;
    justify-content: center;
}
<div class="container"> 
  <span>I'm vertically/horizontally centered!</span>
</div>

  • 方法 3 - table-cell/vertical-align: middle:

    Example Here/Full Screen Example

    在某些情况下,您需要确保将html/body 元素的高度设置为100%

    对于垂直对齐,将父元素的width/height 设置为100% 并添加display: table。然后对于子元素,将display 更改为table-cell 并添加vertical-align: middle

    对于水平居中,您可以添加 text-align: center 以使文本和任何其他 inline 子元素居中。或者,您可以使用margin: 0 auto,假设元素是block 级别。

html, body {
    height: 100%;
}
.parent {
    width: 100%;
    height: 100%;
    display: table;
    text-align: center;
}
.parent > .child {
    display: table-cell;
    vertical-align: middle;
}
<section class="parent">
    <div class="child">I'm vertically/horizontally centered!</div>
</section>

  • 方法 4 - 绝对定位 50% 从顶部位移:

    Example Here/Full Screen Example

    此方法假定文本具有已知高度 - 在本例中为 18px。只是相对于父元素从顶部绝对定位元素50%。使用负的margin-top 值,该值是元素已知高度的一半,在本例中为-9px

html, body, .container {
    height: 100%;
}

.container {
    position: relative;
    text-align: center;
}

.container > p {
    position: absolute;
    top: 50%;
    left: 0;
    right: 0;
    margin-top: -9px;
}
<div class="container">
    <p>I'm vertically/horizontally centered!</p>
</div>

  • 方法 5 - line-height 方法(最不灵活 - 不建议):

    Example Here

    在某些情况下,父元素会有一个固定的高度。对于垂直居中,您所要做的就是在子元素上设置一个等于父元素固定高度的line-height 值。

    虽然此解决方案在某些情况下会起作用,但值得注意的是,当有多行文本时它不起作用 - like this

.parent {
    height: 200px;
    width: 400px;
    background: lightgray;
    text-align: center;
}

.parent > .child {
    line-height: 200px;
}
<div class="parent">
    <span class="child">I'm vertically/horizontally centered!</span>
</div>

【讨论】:

  • 是的,行得通。我已经习惯了 css3 上的 inline-flex,所以我完全忘记了如何正确地采用这种方法。非常感谢,非常感谢。
  • 我打开了上面所有的“全屏”示例,我注意到垂直对齐在屏幕底部附近总是有点太多(分辨率为 1920x1080,标签在屏幕上太低了 20 像素) .我在我的网页中实现了方法#2,即使 div 不是全屏的,也遇到了同样的问题。这个问题在我的网页上更严重。 (100px 太低了)...
  • 您可能需要添加方法 6,using flexbox(适用于 IE9+,[90%+ 浏览器市场覆盖率](caniuse.com/#feat=flexbox)):display: flex 在父级中, 和align-self: center 在孩子们。
  • 我改变了一点方法 2:jsfiddle.net/yeaqrh48/278。结果,通过降低窗口的高度,文本超出范围,变得无法看到。如何解决这个问题?
  • @josh-crozier 刚刚在他们的 css xd 中找到了一个网站,其中包含指向您帖子的链接
【解决方案2】:

如果 CSS3 是一个选项(或者您有备用),您可以使用 transform:

.center {
    right: 50%;
    bottom: 50%;
    transform: translate(50%,50%);
    position: absolute;
}

与上面的第一种方法不同,您不想将 left:50% 用于否定翻译,因为 IE9+ 中存在溢出错误。使用正的右值,您将看不到水平滚动条。

【讨论】:

  • 我相信转换应该是transform: translateX(50%) translateY(50%);。根据Mozilla transform docs,上面的转换在语法上是不正确的。
  • @EirikH 不确定您在看什么,但该语法很好(并且在文档中这么说)。大多数可以取 X 和 Y 值的transform 值都有一个可以同时取 X 和 Y 值的基本函数和只取一个的专用函数。
【解决方案3】:

另一种方法是使用表格:

<div style="border:2px solid #8AC007; height:200px; width:200px;">
  <table style="width:100%; height:100%">
    <tr style="height:100%">
      <td style="height:100%; text-align:center">hello, multiple lines here, this is super long, and that is awesome, dude</td>
    </tr>
  </table>
</div>

【讨论】:

    【解决方案4】:

    对我来说最简单、最干净的解决方案是使用 CSS3 属性“transform”:

    .container {
      position: relative;
    }
    
    .container a {
      position: absolute;
      top: 50%;
      transform: translate(0,-50%);
    }
    <div class="container">
      <a href="#">Hello world!</a>
    </div>

    【讨论】:

      【解决方案5】:

      将盒子垂直和水平居中的最佳方法是使用两个容器:

      ##外部容器:

      • 应该有display: table;

      ##内部容器:

      • 应该有display: table-cell;
      • 应该有vertical-align: middle;
      • 应该有text-align: center;

      ##内容框:

      • 应该有display: inline-block;
      • 应调整文本水平对齐方式,除非您希望文本居中

      ##演示:

      body {
          margin : 0;
      }
      
      .outer-container {
          display: table;
          width: 80%;
          height: 120px;
          background: #ccc;
      }
      
      .inner-container {
          display: table-cell;
          vertical-align: middle;
          text-align: center;
      }
      
      .centered-content {
          display: inline-block;
          text-align: left;
          background: #fff;
          padding : 20px;
          border : 1px solid #000;
      }
      <div class="outer-container">
         <div class="inner-container">
           <div class="centered-content">
              Center this!
           </div>
         </div>
      </div>

      另见this Fiddle

      ##在页面中间居中:

      要将您的内容置于页面中间,请将以下内容添加到您的外部容器中:

      • position : absolute;
      • width: 100%;
      • height: 100%;

      这是一个演示:

      body {
          margin : 0;
      }
      
      .outer-container {
          position : absolute;
          display: table;
          width: 100%;
          height: 100%;
          background: #ccc;
      }
      
      .inner-container {
          display: table-cell;
          vertical-align: middle;
          text-align: center;
      }
      
      .centered-content {
          display: inline-block;
          text-align: left;
          background: #fff;
          padding : 20px;
          border : 1px solid #000;
      }
      <div class="outer-container">
         <div class="inner-container">
           <div class="centered-content">
              Center this!
           </div>
         </div>
      </div>

      另见this Fiddle

      【讨论】:

        【解决方案6】:
        • 方法 6

        /*Change units to "%", "px" or whatever*/
        
        #wrapper{
          width: 50%;
          height: 70vh;
          background: rgba(0,0,0,.5);
          position: absolute;
          top: 0;
          right: 0;
          bottom: 0;
          left: 0;
          margin: auto;
        }
        #left{
          width: 50%;
          height: 50vh;
          position: absolute;
          top: 0;
          bottom: 0;
          left: 0;
          margin: auto;
          background: red;
        }
        #right{
          width: 50%;
          height: 50vh;
          position: absolute;
          top: 0;
          bottom: 0;
          right: 0;
          margin: auto;
          background: green;
        }
        .txt{
          text-align: center;
          line-height: 50vh;
        }
        <div id="wrapper">
           <div id="left" class="txt">Left</div>
           <div id="right" class="txt">Right</div>
        </div>
            .container{ 
                       width: 50%;  //Your container width here
                       height: 50%; //Your container height here
                       position: absolute; 
                       top: 0; 
                       right: 0;  
                       bottom: 0; 
                       left: 0; 
                       margin: auto;
            }
        

        【讨论】:

          【解决方案7】:

          使 Div 在页面中居中 check the fiddle link

          #vh {
              border-radius: 15px;
              box-shadow: 0 0 8px rgba(0, 0, 0, 0.4);
              padding: 25px;
              width: 200px;
              height: 200px;
              background: white;
              text-align: center;
              margin: auto;
              position: absolute;
              top: 0;
              left: 0;
              bottom: 0;
              right: 0;
          }
          &lt;div id="vh"&gt;Div to be aligned vertically&lt;/div&gt;

          更新 另一种选择是使用弹性框 check the fiddle link

          .vh {
              background-color: #ddd;
              height: 200px;
              align-items: center;
              display: flex;
          }
          .vh > div {
              width: 100%;
              text-align: center;
              vertical-align: middle;
          }
          <div class="vh">
              <div>Div to be aligned vertically</div>
          </div>

          【讨论】:

            【解决方案8】:

            以下是获得所需结果的 Flex-box 方法

            <!DOCTYPE html>
            <html>
            <head>
              <meta charset="utf-8">
              <meta name="viewport" content="width=device-width">
              <title>Flex-box approach</title>
            <style>
              .tabs{
                display: -webkit-flex;
                display: flex;
                width: 500px;
                height: 250px;
                background-color: grey;
                margin: 0 auto;
                
              }
              .f{
                width: 200px;
                height: 200px;
                margin: 20px;
                background-color: yellow;
                margin: 0 auto;
                display: inline; /*for vertically aligning */
                top: 9%;         /*for vertically aligning */
                position: relative; /*for vertically aligning */
              }
            </style>
            </head>
            <body>
            
                <div class="tabs">
                    <div class="f">first</div>
                    <div class="f">second</div>        
                </div>
            
            </body>
            </html>

            【讨论】:

              【解决方案9】:

              运行这段代码 sn-p 并看到一个垂直和水平对齐的 div。

              html,
              body,
              .container {
                height: 100%;
                width: 100%;
              }
              .container {
                display: flex;
                align-items: center;
                justify-content: center;
              }
              .mydiv {
                width: 80px;
              }
              <div class="container">
                <div class="mydiv">h & v aligned</div>
              </div>

              【讨论】:

                【解决方案10】:

                为了使元素垂直和水平居中,我们还可以使用下面提到的属性。

                此 CSS 属性 aligns-items 垂直并接受以下值:

                flex-start:项目与容器顶部对齐。

                flex-end:项目与容器底部对齐。

                center:项目在容器的垂直中心对齐。

                基线:项目显示在容器的基线处。

                拉伸:项目被拉伸以适应容器。

                这个 CSS 属性 justify-content ,它水平对齐项目并接受以下值:

                flex-start:项目对齐到容器的左侧。

                flex-end:项目与容器右侧对齐。

                center:项目在容器的中心对齐。

                space-between:项目以相等的间距显示。

                space-around:项目以等间距显示。

                【讨论】:

                  【解决方案11】:

                  只需将顶部、底部、左侧和右侧设为 0。

                  <html>
                  <head>
                  <style>
                  <div> 
                  {
                  position: absolute;
                  margin: auto;
                  background-color: lightblue;
                  width: 100px;
                  height :100px;
                  padding: 25px;
                  top :0;
                  right :0;
                  bottom:0;
                  left:0;
                  }
                  
                  
                  </style>
                  </head>
                  <body>
                  
                  <div> I am in the middle</div>
                  
                  </body>
                  </html>
                  

                  【讨论】:

                    【解决方案12】:

                        .align {
                            display: flex;
                            width: 400px;
                            height: 400px;
                            border: solid 1px black;
                            align-items: center;
                            justify-content: space-around;
                        }
                        .align div:first-child {
                            width: 20px;
                            height: 20px;
                            background-color: red;
                            position: absolute; 
                        }
                        .align div {
                            width: 20px;
                            height: 20px;
                            background-color: blue;
                        }
                        <div class='align'>
                            <div></div>
                            <div></div>
                            <div></div>
                            <div></div>
                            <div></div>
                            <div></div>
                        </div>

                    第一个孩子将在中心垂直和水平对齐

                    【讨论】:

                      【解决方案13】:

                      网格 css 方法

                      #wrapper {
                          position: absolute;
                          top: 0;
                          bottom: 0;
                          right: 0;
                          left: 0;
                          display: grid;
                          grid-template-columns: repeat(3, 1fr);
                          grid-template-rows: repeat(3, 1fr);
                      }
                      .main {
                          background-color: #444;
                      }
                      <div id="wrapper">
                          <div class="box"></div>
                          <div class="box"></div>
                          <div class="box"></div>
                          <div class="box"></div>
                          <div class="box main"></div>
                      </div>

                      【讨论】:

                        【解决方案14】:

                        您可以使用 CSS(您的元素 display:inline-grid + grid-auto-flow: row;)Grid 和 Flex Box(父元素 display:flex;)来实现这一点,

                        见下sn-p

                        #leftFrame {
                          display: flex;
                          height: 100vh;
                          width: 100%;
                        }
                        
                        #tabs {
                          display: inline-grid;
                          grid-auto-flow: row;
                          grid-gap: 24px;
                          justify-items: center;
                          margin: auto;
                        }
                        
                        html,body {
                          margin:0;
                          padding:0;
                        }
                        <div>
                        <div id=leftFrame>
                          <div id=tabs>
                            <div>first</div>
                            <div>second</div>        
                          </div>
                        </div>
                        </div>

                        【讨论】:

                          【解决方案15】:

                          这应该可行

                          .center-div {
                              display: flex;
                              flex-direction: column;
                              justify-content: center;
                              align-items: center;
                              text-align: center;
                              min-height: 100vh;
                          }
                          &lt;div class="center-div"&gt;Center Div&lt;/div&gt;

                          【讨论】:

                            【解决方案16】:

                            将 div 垂直和水平居中的最简单方法如下:

                            <div style="display: table; width: 200px; height: 200px; border: 1px solid black;">
                                <div style="display: table-cell; vertical-align: middle; text-align: center;">
                                    Text Here
                                </div>
                            </div>

                            另一个例子

                            .parent {
                                display: table; 
                                width: 100%; 
                                height: 100%;
                            }
                            
                            .child {
                                display: table-cell; 
                                vertical-align: middle;
                                text-align: center;
                            }
                            
                            
                            <div class="parent">
                                <div class="child">
                                    <h4><u>SERVICE IN BANGLADESH FLEET RESERVE <br> AND <br> RE-ENGAGEMENT ORDER FOR DEFENCE SERVICE</u></h4>
                                </div>
                            </div>
                            

                            【讨论】:

                              【解决方案17】:

                              来源Link

                              方法一)显示类型flex

                              .child-element{
                                  display: flex;
                                  justify-content: center;
                                  align-items: center;
                              }
                              

                              方法2)二维变换

                              .child-element {
                                  top: 50%;
                                  left: 50%;
                                  transform: translate(-50% , -50%);
                                  position: absolute;
                              }
                              

                              查看其他方法here

                              【讨论】:

                                【解决方案18】:

                                这里是如何使用两个简单的 flexbox 属性来使 n 个 div 在两个轴上居中:

                                • 设置容器的高度:此处将主体设置为至少 100 视口高度。
                                • align-items: center; 如果 flex 方向是 row 则将块垂直居中,如果 flex 方向是 column 则水平居中
                                • justify-content: space-around; 将垂直分配可用空间,如果 flex 方向是行,否则如果 flex 方向是 div 元素周围的列,则水平分配空间

                                body {
                                    min-height: 100vh;
                                    display: flex;
                                    align-items: center;
                                    justify-content: space-around;
                                }
                                <div>foo</div>
                                <div>bar</div>

                                【讨论】:

                                • 感谢您的解决方案。为什么最小高度是视口的 100% 而不是 50%?
                                • @Lucas 这是因为默认情况下,正文将具有其内容的高度(在这种情况下为两行文本)。如果你想在 something 中垂直居中,这个东西需要一个定义的高度(100%、100px 等...)。 50vh 会将元素放置在视口高度的 25%
                                【解决方案19】:

                                这是人们在搜索时可能会来到此页面的一个相关问题:当​​我想将 div 居中以获得“等待..”100px 方形动画 gif 时,我使用:

                                .centreDiv {
                                    position: absolute;
                                    top:     -moz-calc(50vh - 50px);
                                    top:  -webkit-calc(50vh - 50px);
                                    top:          calc(50vh - 50px);
                                    left:    -moz-calc(50vw - 50px);
                                    left: -webkit-calc(50vw - 50px);
                                    left:         calc(50vw - 50px);
                                    z-index: 1000; /* whatever is required */
                                }
                                

                                【讨论】:

                                  【解决方案20】:

                                  如果你喜欢它没有 flexbox、grid、table 或vertical-align: middle;

                                  你可以这样做:

                                  HTML

                                  <div class="box">
                                      <h2 class="box__label">square</h2>
                                  </div>
                                  

                                  CSS

                                  .box {
                                      box-sizing: border-box;
                                      width: 100px;
                                      height: 100px;
                                      text-align: center;
                                      border: 1px solid black;
                                  }
                                  
                                  .box__label {
                                      box-sizing: border-box;
                                      display: inline-block;
                                      transform: translateY(50%);
                                      text-align: center;
                                      border: 1px solid black;
                                  }
                                  

                                  【讨论】:

                                    【解决方案21】:

                                    需要遵循以下简单的解决方案:

                                      .centered-class {
                                          align-items: center;
                                          display: flex;
                                          justify-content: center;
                                          width: 100vw;
                                          height: 100vh;
                                        }
                                    <div class="centered-class">
                                      I'm in center vertically and horizontally.
                                    </div>

                                    【讨论】:

                                      【解决方案22】:

                                      最简单的flexbox方法:

                                      单个元素垂直和水平居中的最简单方法是将其设为弹性项目并将其边距设置为auto

                                      如果您将自动边距应用于弹性项目,该项目将自动 扩展其指定的边距以占用 flex 中的额外空间 容器...

                                      .flex-container {
                                        height: 150px;
                                        display: flex;
                                      }
                                      
                                      .flex-item {
                                        margin: auto;
                                      }
                                      <div class="flex-container">
                                        <div class="flex-item">
                                          This should be centered!
                                        </div>
                                      </div>

                                      每个方向的边距扩展会将元素到其容器的中间。

                                      【讨论】:

                                        【解决方案23】:

                                        CSS Grid: place-items

                                        最后,我们为 CSS Grid 提供了 place-items: center 以使其更容易。

                                        HTML

                                        <div class="parent">
                                          <div class="to-center"></div>
                                        </div>
                                        

                                        CSS

                                        .parent {
                                          display: grid;
                                          place-items: center;
                                        }
                                        

                                        输出:

                                        html,
                                        body {
                                          height: 100%;
                                        }
                                        
                                        .container {
                                          display: grid;
                                          place-items: center;
                                          height: 100%;
                                        }
                                        
                                        .center {
                                          background: #5F85DB;
                                          color: #fff;
                                          font-weight: bold;
                                          font-family: Tahoma;
                                          padding: 10px;
                                        }
                                        <div class="container">
                                          <div class="center" contenteditable>I am always super centered within my parent</div>
                                        </div>

                                        【讨论】:

                                          【解决方案24】:

                                          我使用这个 CSS 代码:

                                          display: grid;
                                          justify-content: center;
                                          align-items: center;
                                          

                                          来源是CSS-Tricks

                                          【讨论】:

                                            【解决方案25】:

                                            在我尝试垂直对齐 button::beforebutton::after 内的文本内容的情况下,我能够使用 vertical-align: text-top 使其正常工作。

                                            button::after {
                                              vertical-align: text-top;
                                            }
                                            

                                            【讨论】:

                                              【解决方案26】:

                                              如果它仅关于文本对齐 只需使用它就可以非常简单:

                                              vertical-align: middle; /* vertical centering*/
                                              text-align: center; /* horizontal centering*/
                                              

                                              不需要父亲风格或类似的东西。 当然在某些情况下,当父亲具有某些样式属性时,它可能会在此解决方案中影响孩子,并且此解决方案将无法正常工作。

                                              【讨论】:

                                                猜你喜欢
                                                • 2012-04-22
                                                • 2017-05-28
                                                • 2021-01-01
                                                • 1970-01-01
                                                • 2017-02-28
                                                • 2014-01-12
                                                • 1970-01-01
                                                • 2013-03-07
                                                相关资源
                                                最近更新 更多