本文介绍了如何将元素水平和垂直居中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to center my tabs content vertically, but when I add the CSS style display:inline-flex, the horizontal text-align disappears.

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

How can I make both text alignments x and y for each of my tabs?

* { 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>

推荐答案

  • 方法1-transform translateX/translateY:

    此处的示例 / 全屏示例

    • Approach 1 - transform translateX/translateY:

      Example Here / Full Screen Example

      受支持的浏览器(大多数)中,您可以使用top: 50%/left: 50%结合translateX(-50%) translateY(-50%)可以动态地垂直/水平居中放置元素.

      In supported browsers (most of them), you can use top: 50%/left: 50% in combination with translateX(-50%) translateY(-50%) to dynamically vertically/horizontally center the element.

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

      受支持的浏览器中,将目标元素的display设置为flex并使用align-items: center用于垂直居中,而justify-content: center用于水平居中.只是不要忘记为其他浏览器支持添加供应商前缀(请参见示例).

      In supported browsers, set the display of the targeted element to flex and use align-items: center for vertical centering and justify-content: center for horizontal centering. Just don't forget to add vendor prefixes for additional browser support (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>
      • Approach 3 - table-cell/vertical-align: middle:

        Example Here / Full Screen Example

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

      In some cases, you will need to ensure that the html/body element's height is set to 100%.

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

      For vertical alignment, set the parent element's width/height to 100% and add display: table. Then for the child element, change the display to table-cell and add vertical-align: middle.

      对于水平居中,可以添加text-align: center来使文本居中,也可以添加任何其他inline子元素.另外,您可以使用margin: 0 auto,假设元素为block级别.

      For horizontal centering, you could either add text-align: center to center the text and any other inline children elements. Alternatively, you could use margin: 0 auto, assuming the element is block level.

      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>
      • Approach 4 - Absolutely positioned 50% from the top with displacement:

        Example Here / Full Screen Example

      此方法假定文本的高度已知-在这种情况下为18px.相对于父元素,仅从顶部绝对定位元素50%即可.使用负的margin-top值,该值是元素已知高度的一半,在这种情况下为-9px.

      This approach assumes that the text has a known height - in this instance, 18px. Just absolutely position the element 50% from the top, relative to the parent element. Use a negative margin-top value that is half of the element's known height, in this case - -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>
      • Approach 5 - The line-height method (Least flexible - not suggested):

        Example Here

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

      In some cases, the parent element will have a fixed height. For vertical centering, all you have to do is set a line-height value on the child element equal to the fixed height of the parent element.

      尽管此解决方案在某些情况下可以使用,但值得注意的是,当多行文本时,该解决方案将不起作用-喜欢这样.

      Though this solution will work in some cases, it's worth noting that it won't work when there are multiple lines of text - 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>

      这篇关于如何将元素水平和垂直居中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 01:43