CSS实现垂直水平居中的几个方法

flex实现垂直水平居中
使用CSS3中的flex布局可快速实现垂直水平居中,且居中不受父子元素的高度宽度影响。缺点是兼容性不太好,适用于现代浏览器。

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>flex布局</title>
  </head>
  <style type="text/css">
  .parent{
  	display:flex;
  	align-items:center;
  	justify-content:center;
  	height:200px;
  	width:200px;
  	background:yellow;
  }
  .children{
  	background:red;
  	width:100px;
  	height:100px;
  }
  </style>
  <body>
    <div class="parent">
    	<div class="children"></div>
    </div>
  </body>
</html>

伪元素实现水平垂直居中
使用伪元素after,before均可实现水平垂直居中,常使用于模态框的水平垂直居中,缺点是需要指定父容器的高度。

<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
  <meta charset="utf-8">
  <title>伪元素实现水平垂直居中</title>
</head>
<style type="text/css">
  .mask {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    text-align: center;
    background:red;
  }
  .mask:after{
    content: "";
    display: inline-block;
    height: 100%;
    width: 0;
    vertical-align: middle;
  }
  .modal {
    display: inline-block;
    width: 100px;
    height:100px;
    vertical-align: middle;
    background-color: yellow;
    border-radius: 4px;
    border: 1px solid #ebeef5;
    font-size: 18px;
    text-align: center;
  }
  .modal:before{
    content: "";
    display: inline-block;
    height: 100%;
    width:0;
    vertical-align: middle;
  }

  }
</style>

<body>
  <div class="mask">
    <div class="modal">文本</div>
  </div>
</body>
</html>

position:absolute实现水平垂直居中
使用时需注意absolute,相对于 static 定位以外的第一个父元素进行定位。transform: translate也是属于CSS3的属性

<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <title>positon:absolute实现水平垂直居中</title>
</head>
<style type="text/css">
  .container{
    position: relative;
    width:200px;
    height:200px;
    background: red;
  }
  .center{
    position: absolute;
    top:50%;
    left:50%;
    transform: translate(-50%,-50%);
    background: white;
    width: 50px;
    height: 50px;
    text-align: center;
    line-height: 50px;
  }
</style>

<body>
  <div class="container">
    <div class="center">居中</div>
  </div>
</body>

</html>

margin:auto实现水平居中
注意只能实现水平居中,且容器的宽度必须指定,否则将撑满父容器。

<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <title>margin:auto实现水平居中</title>
</head>
<style type="text/css">
  body{
    width:100%;
    height:100%;
  }
  .parent{
    width:200px;
    height:200px;
    background:yellow;
    margin:auto;
  }
  .children{
    width:100px;
    height:100px;
    background:red;
    margin:auto;
  }
</style>

<body>
  <div class="parent">
    <div class="children"></div>
  </div>
</body>

</html>

文本实现垂直水平居中
使用text-align,line-height可实现文本的垂直水平居中。但值得注意的是行内元素不支持line-height

<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <title>margin:auto实现水平居中</title>
</head>
<style type="text/css">

  .parent{
    width:200px;
    height:200px;
    background:yellow;
    margin:auto;
  }
  .children{
    width:100px;
    height:100px;
    background:red;
    margin:auto;
  }
</style>

<body>
  <div class="parent">
    <div class="children">142424</div>
  </div>
</body>

</html>

猜你喜欢

转载自blog.csdn.net/qq_32013641/article/details/86620233