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

本文详细介绍了五种在CSS中实现元素居中的方法,包括针对文字的text-align属性,单个块状元素的margin自动设置,多个块状元素的display:inline-block及flex布局,以及使用relative定位应对不确定宽度的元素。
摘要由CSDN通过智能技术生成

方法一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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值