如何水平和垂直居中元素

时间:2013-10-19 02:01:24

标签: html css css3

我正在尝试垂直居中我的标签内容,但是当我添加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>

25 个答案:

答案 0 :(得分:541)

  • 方法1 - transform translateX / translateY

    Example Here / Full Screen Example

    supported browsers(大多数)中,您可以将top: 50% / left: 50%translateX(-50%) translateY(-50%)结合使用,动态地垂直/水平居中元素。

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

答案 1 :(得分:20)

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

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

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

答案 2 :(得分: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

答案 3 :(得分:3)

对我来说,最简单,最干净的解决方案是使用CSS3属性&#34;转换&#34;:

&#13;
&#13;
.container {
  position: relative;
}

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

答案 4 :(得分:3)

运行此代码段并查看垂直和水平对齐的div。

&#13;
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/6.16.0/polyfill.min.js"></script>
&#13;
html,
body,
.container {
  height: 100%;
  width: 100%;
}
.container {
  display: flex;
  align-items: center;
  justify-content: center;
}
.mydiv {
  width: 80px;
}
&#13;
&#13;
&#13;

答案 5 :(得分:2)

<form action="{% url 'add-todo' %}" method="POST">{% csrf_token %}

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

HTML

CSS Grid: place-items

CSS

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

输出:

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

答案 6 :(得分:2)

&#13;
&#13;
    .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;
    }
&#13;
    <div class='align'>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
    </div>
&#13;
&#13;
&#13;

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

答案 7 :(得分:1)

另一种方法是使用表格:

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

答案 8 :(得分:1)

将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;
}
<div id="vh">Div to be aligned vertically</div>

<强>更新 另一个选择是使用弹性框 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>

答案 9 :(得分:1)

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

&#13;
&#13;
<!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>
&#13;
&#13;
&#13;

答案 10 :(得分:1)

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

这个CSS属性 aligns-items 是垂直的,并接受以下值:

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

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

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

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

拉伸:拉伸项目以适合容器。

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

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

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

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

空格 - :项目之间的间距相等。

space-around :项目周围的间距相等。

答案 11 :(得分:1)

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

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

答案 12 :(得分:1)

如果您更喜欢没有
flexbox /网格/表格
没有的:
vertical-align:middle < br />

您可以这样做:

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;
}

答案 13 :(得分:1)

这里是如何使用2个简单的flex属性将 n div置于2轴的中心:

  • 设置容器的高度:在此将车身设置为至少100vh。
  • align-items: center将使块垂直居中
  • justify-content: space-around将在div周围分配自由水平空间

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

答案 14 :(得分:0)

我使用这个 css 代码:

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

来源是:enter link description here

答案 15 :(得分:0)

这是一个相关的问题,人们在搜索时可能会转到此页面:当我想将div居中(100px正方形)“ waiting ..”动画gif时,我会使用:

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

答案 16 :(得分:0)

将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 :(得分:0)

您可以使用CSS(您的元素display:inline-grid + grid-auto-flow: row;)网格和Flex Box(父元素display:flex;)来实现此目的,

请参见下面的代码段

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

答案 18 :(得分:0)

网格css方法

&#13;
&#13;
#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;
}
&#13;
<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>
&#13;
&#13;
&#13;

答案 19 :(得分:0)

只需将顶部,底部,左侧和右侧设为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>

答案 20 :(得分:0)

  • 方法6

&#13;
&#13;
/*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;
}
&#13;
<div id="wrapper">
   <div id="left" class="txt">Left</div>
   <div id="right" class="txt">Right</div>
</div>
&#13;
&#13;
&#13;

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

答案 21 :(得分:-1)

来源Link

  

方法1)显示类型flex

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

方法2)2D变换

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

查看其他方法here

答案 22 :(得分:-1)

简单!在容器上使用display flex并在文本上使用margin自动!!!!

#hood{

 width : 300px;
 height: 100px;
 border: solid 1px #333333;
 display: flex; 
}

#hood span{

  margin: auto
}



<html>
<head></head>
    <body>
        <div id="hood">
            <span> I Am Centered</span>
        </div>
    </body>
</html>

演示:https://jsfiddle.net/ay95f08g/

经过测试:MacOs Mojave上的Safari,Chrome,Firefox和Opera。 (所有更新于2019年2月25日)

答案 23 :(得分:-1)

这应该有效

$ file_name=$(ssh testbox  "ls -t /home/ec2-user/hello* | head -1")

$ echo "[$file_name]"
[/home/ec2-user/hello]

$ scp testbox:$file_name ./
hello                                    100%    0     0.0KB/s   00:00
.center-div {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    text-align: center;
    min-height: 100vh;
}

答案 24 :(得分:-1)

最简单的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>

在每个方向上的边距扩展将精确地推入其容器的中间。