水平居中、水平垂直居中

水平居中

  • 行內塊元素居中

    • text-align  可使塊級元素內部的行內元素水平居中,若是內部是塊級元素,先將其轉成行內塊元素。
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>行內塊元素水平居中</title>
    <style>
        .father{
            text-align: center;
            width: 200px;
            height: 200px;
            border: 1px solid black;
        }
        .son{
            display: inline-block;
            width: 50px;
            height: 50px;
            background: red;
        }
    </style>
</head>
<body>
    <div class="father">
        <div class="son"></div>
    </div>
</body>
</html>
  • 塊級元素水平居中

    • 將該塊級元素的左右邊距設置成auto
<style>
        .father{
            width: 200px;
            height: 200px;
            border: 1px solid black;
        }
        .son{
            margin: 0 auto;
            width: 50px;
            height: 50px;
            background: red;
        }
    </style>
    •  使用transform:translateX
<style>
        .father{
            width: 200px;
            height: 200px;
            border: 1px solid black;
            position: relative;
        }
        .son{
            width: 50px;
            height: 50px;
            background: red;
            position: absolute;
            left: 50%;
            transform: translateX(-50%);
        }
</style>
    • 使用flex+justify-content  將其父元素設置成彈性盒子
<style>
        .father{
            width: 200px;
            height: 200px;
            border: 1px solid black;
            display: flex;
            justify-content: center;
        }
        .son{
            width: 50px;
            height: 50px;
            background: red;
        }
    </style>

 水平垂直居中

  • 絕對定位與負邊距實現(已知寬高)

<style>
        .father{
            width: 200px;
            height: 200px;
            border: 1px solid black;
            position: relative;
        }
        .son{
            width: 50px;
            height: 50px;
            background: red;
            position: absolute;
            top: 50%;
            left: 50%;
            margin: -25px 0 0 -25px;
        }
    </style>
  • 絕對定位與margin:auto(不兼容低版本IE)

<style>
        .father{
            width: 200px;
            height: 200px;
            border: 1px solid black;
            position: relative;
        }
        .son{
            width: 50px;
            height: 50px;
            background: red;
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            margin: auto;
        }
    </style>
  • 絕對定位+CSS3(兼容問題)

 <style>
        .father{
            width: 200px;
            height: 200px;
            border: 1px solid black;
            position: relative;
        }
        .son{
            width: 50px;
            height: 50px;
            background: red;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%,-50%);
        }
    </style>
  • flex佈局(不兼容低版本IE)

<style>
        .father{
            width: 200px;
            height: 200px;
            border: 1px solid black;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .son{
            width: 50px;
            height: 50px;
            background: red;
        }
    </style>