如何将元素水平和垂直居中

HTML CSS

阳光小胖

2020-03-19

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

第2420篇《如何将元素水平和垂直居中》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

20个回答
逆天西里 2020.03.19

有很多人都这样做。您可以使用以下方法在页面中间创建div。

.center-box {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
}
<div class="center-box">
  <h1>Text in center of page</h1>
</div>

欢呼!

Gil飞云 2020.03.19

很简单!在容器上使用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日)

Sam神乐 2020.03.19

链接

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

在这里查看其他方法

村村理查德 2020.03.19

这应该工作

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

神乐逆天 2020.03.19

如果您更喜欢不带
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;
}
西门LEY 2020.03.19

这是一个相关的问题,人们在搜索时可能会转至此页面:当我想将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*/
    }
L村村小宇宙 2020.03.19

将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>
乐小哥L 2020.03.19

您可以使用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>

西门Sam 2020.03.19

只需将top,bottom,left和right设为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>
GOL 2020.03.19
  • 方法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;
    }
Itachi达蒙 2020.03.19

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

此CSS属性垂直对齐项目,并接受以下值:

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

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

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

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

Stretch:将物品拉伸以适合容器。

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

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

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

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

间隔:项目之间以相等的间距显示。

空格:项目以相等的间距显示。

LPro 2020.03.19

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

小胖阿飞 2020.03.19

另一种方法是使用表:

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

Green樱 2020.03.19

以下是使用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>

飞云十三 2020.03.19

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

Mandy小胖 2020.03.19

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

.container {
  position: relative;
}

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

蛋蛋LEY 2020.03.19

运行此代码段,查看垂直和水平对齐的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>

乐乐 2020.03.19

这是如何使用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>

西门村村古一 2020.03.19

垂直和水平放置框的最佳方法是使用两个容器:

外部容器:

  • 应该有 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>

另请参阅此小提琴

在页面中间居中:

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

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

另请参阅此小提琴

Green老丝 2020.03.19

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

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

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

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android