CSS五种水平居中:text-align margin incline-block flex relative

方法一text-align:针对于文字

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .box {
            background-color: green;
            height: 300px;
            width: 600px;
            text-align: center;
        }

        P {
            color: red;
        }
    </style>
</head>
<body>
<div class="box">
    <p>1111</p>
</div>
</body>
</html>

方法二单个块状元素解决方案margin左右居中

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .box {
            background-color: green;
            height:300px;
            width: 600px;
            text-align:center;
        }
        .box2{
            background-color: red;
            width: 20px;
            height: 20px;
            margin:0 auto;
        }
    </style>
</head>
<body>
<div class="box">
    <div class="box2"></div>
</div>
</body>
</html>

方法三 多个块状元素 父元素设为text-align子元素设置为行内块元素:display:inline-block;

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .box {
            background-color: green;
            height: 300px;
            width: 600px;
            text-align: center;
        }

        .box2, .box3 {
            background-color: red;
            width: 20px;
            height: 20px;
            display: inline-block;
        }
    </style>
</head>
<body>
<div class="box">
    <div class="box2"></div>
    <div class="box3"></div>
</div>
</body>
</html>

方法四多个块状元素 使用flex定位

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .box {
            background-color: green;
            height: 300px;
            width: 600px;
            display: flex;
            justify-content: center;
        }

        .box2, .box3, .box4 {
            background-color: red;
            width: 20px;
            height: 20px;
            margin-left: 10px;

        }
    </style>
</head>
<body>
<div class="box">
    <div class="box2"></div>
    <div class="box3"></div>
    <div class="box4"></div>
</div>
</body>
</html>

方法五不确定宽度设置:使用relative进行定位

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .box {
            float: left;
            position: relative;
            left: 50%;
            background: #2ac7d2;
        }

        .box ul {
            position: relative;
            left: -50%;
            background: #7c4a03;
        }
    </style>
</head>
<body>
<div class="box">
    <ul>
        <li>111</li>
        <li>111</li>
        <li>111</li>
    </ul>
</div>
</body>
</html>

https://github.com/7117/frontendCollection/tree/master/CSS

猜你喜欢

转载自blog.csdn.net/fujian9544/article/details/105661845
今日推荐