水平居中、水平垂直居中

水平居中

  • 行内块元素居中

    • 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>
相关文章
相关标签/搜索