水平垂直居中

笔记整理之一:水平垂直居中

PC固定宽高、一般使用maring负值进行居中
PC不固定宽高、一般使用relative和left进行

下面三种方案平常都很少被使用的到
    verticle-align: middle;
    display: table-cell;
    top:0; right:0; bottom:0; left:0;

移动端一般使用(PC方案在移动端都可以使用)

transform: translate(-50%,-50%);
display: flex; justify-content: center; align-items: center;
  1. 文本水平垂直居中

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>文本垂直</title>
    <style>
        .xx_wrap {
            width: 500px;
            height: 500px;
            background-color: greenyellow;
        }

        .xx_wrap p {
            line-height: 500px;
            text-align: center;
        }
    </style>
</head>
<body>
<div class="xx_wrap">
    <p>文本垂直居中</p>
</div>
</body>
</html>

移动端水平垂直居中

1、 CSS3垂直居中方案

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>快级元素水平垂直居中(不固定宽高)</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            font-size: 12px;
            font-family: Arial, Helvetica, sans-serif;
        }

        ol, ul {
            list-style: none;
        }

        .box {
            position: relative;
            width: 500px;
            height: 500px;
            background-color: blueviolet;
        }

        .myUl {
            position: absolute;
            left: 50%;
            top: 50%;
            -webkit-transform: translate(-50%, -50%);
            -moz-transform: translate(-50%, -50%);
            -ms-transform: translate(-50%, -50%);
            -o-transform: translate(-50%, -50%);
            transform: translate(-50%, -50%);
        }
    </style>
</head>
<body>
<div class="box">
    <ul class="myUl">
        transform: translate(-50%,-50%);居中方案
    </ul>
</div>
</body>
</html>

2、flex居中方案

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>flex水平垂直居中</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            font-size: 12px;
            font-family: Arial, Helvetica, sans-serif;
        }

        ol, ul {
            list-style: none;
        }

        .box {
            width: 500px;
            height: 500px;
            background-color: #77BBDD;
            /** 各种版本兼容**/
            display: -webkit-box;
            display: -moz-box;
            display: -ms-flexbox;
            display: -webkit-flex;
            display: flex;
            /** 垂直居中核心、兼容**/
             -webkit-box-pack: center;//09版水平居中
                -moz-box-pack: center;
                -ms-flex-pack: center;//过度版(混合版)
      -webkit-justify-content: center;//12版水平居中
              justify-content: center;
              
            -webkit-box-align: center;//09版垂直居中
               -moz-box-align: center;
               -ms-flex-align: center;//过度版(混合版)
          -webkit-align-items: center;//12版垂直居中
                  align-items: center;
            width: 0%;//低版本Android的flex-item无法等分
            display: block;
            /*
            实则只需三行
            display: flex;
            justify-content: center;
            align-items: center;
            */
        }

        .box .div1 {
            width: 200px;
            height: 200px;
            background-color: red;
        }
    </style>
</head>
<body>
<div class="box">
    <div class="div1">flex水平垂直居中</div>
</div>
</body>
</html>

PC端水平垂直居中

1、快级元素水平垂直居中(固定宽高、最常用)

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>水平、垂直居中</title>
    <style>
        .xx_wrap {
            position: relative;
            width: 500px;
            height: 500px;
            background-color: greenyellow;
        }
        .xx_wrap p {
            width: 200px;
            height: 200px;
            background-color: red;
            position: absolute;
            left: 50%;top: 50%;
            margin-left: -100px;
            margin-top: -100px;
        }
    </style>
</head>
<body>
<div class="xx_wrap">
    <p>快级元素水平垂直居中(固定宽高)</p>
</div>
</body>
</html>

4、快级元素水平垂直居中(不固定宽高)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>快级元素水平垂直居中(不固定宽高)</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            font-size: 12px;
            font-family: Arial, Helvetica, sans-serif;
        }

        ol, ul {
            list-style: none;
        }
        /* 定位 + 浮动 */
        .myUl {
            float: left;
            position: relative;
            left: 50%;
            background-color: blueviolet;
        }

        .myUl li {
            left: -50%;
            position: relative;
        }
        /*  绝对定位方案
            .box {position: relative;}
            .myUl {position: absolute;left: 50%;background-color: blueviolet;}
            .myUl li {left: -50%;position: relative;}
        */
    </style>
</head>
<body>
<div class="box">
    <ul class="myUl">
        <li>快级元素水平垂直居中(不固定宽高)</li>
    </ul>
</div>
</body>
</html>

2、绝对定位居中(禁IE6、7)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>绝对定位水平垂直居中</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            font-size: 12px;
            font-family: Arial, Helvetica, sans-serif;
        }

        .wrap {
            width: 500px;
            height: 500px;
            background-color: chartreuse;
            position: relative;
        }
        .wrap .div1 {
            width: 50%;
            height: 50%;
            position: absolute;
            top: 0;
            right: 0;
            bottom: 0;
            left: 0;
            margin: auto;
            background-color: yellow;
        }
    </style>
</head>
<body>
<div class="wrap">
    <div class="div1">绝对定位水平垂直居中</div>
</div>
</body>
</html>

3、非快级元素水平垂直居中(vertical-align: middle禁IE6、7、8)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>非快级元素水平垂直居中</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            font-size: 12px;
            font-family: Arial, Helvetica, sans-serif;
        }

        ol, ul {
            list-style: none;
        }

        .box {
            width: 500px;
            height: 500px;
            background-color: #77BBDD;
            text-align: center;
        }

        .box img {
            /* 若为快级元素:则修改为display: inilne-block;即可 */
            width: 200px;
            height: 200px;
            background-color: red;
            vertical-align: middle;
        }
        .box::after {
            content: '';
            width: 5px;
            line-height: 500px;
            background-color: yellow;
        }
    </style>
</head>
<body>
<div class="box">
    <img src="http://img0.bdstatic.com/img/image/2016ss1.jpg" alt=""/>
</div>
</body>
</html>

5、table-cell水平垂直居中

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>绝对定位水平垂直居中</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            font-size: 12px;
            font-family: Arial, Helvetica, sans-serif;
        }

        .wrap {
            width: 500px;
            height: 500px;
            background-color: chartreuse;
            display: table;
        }
        .wrap .div1 {
            display: table-cell;
            vertical-align: middle;
        }
        .wrap .div1 p {
            width: 50%;
            height: 50%;
            margin: 0 auto;
            background-color: burlywood;
        }
    </style>
</head>
<body>
<div class="wrap">
    <div class="div1">
        <p>绝对定位水平垂直居中</p>
    </div>
</div>
</body>
</html>
    原文作者:ntscshen
    原文地址: https://segmentfault.com/a/1190000004030650
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞