【HLML/CSS学习】元素的水平垂直居中

1. 水平居中

元素类型实现水平居中的方式
inline元素text-align: center
block元素margin: auto
absolute元素left: 50% + margin-left: 负值

2. 垂直居中

元素类型实现垂直居中的方式
inline元素line-height 的值等于 height 值
absolute元素top: 50% + margin-top: 负值
absolute元素left: 50% + top: 50% + transform: translate(-50%, -50%)
absolute元素top, left, bottom, right = 0, margin: auto

3. 具体实现

3.1 水平居中

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>水平对齐</title>
    <style>
        .container{
            border: 1px solid #ccc;
            margin: 10px;
            padding: 10px;
        }
        .item{
            background-color: #ccc;
        }
        /*1. 文字的居中对齐 */
        .container-1{
            text-align: center;
        }
        
        /*2. block元素的水平居中 */
        .container-2 .item{
            width: 500px;
            margin: auto;
        }
        
        /*3. absolute元素的水平居中 */
        .container-3{
            position: relative;
            height: 100px;
        }
        .container-3 .item{
            width: 300px;
            height: 100px;
            position: absolute;
            left: 50%;
            margin-left: -150px;
        }
    </style>
</head>
<body>
    <div class="container container-1">
        <span>一段文字</span>
    </div>
    <div class="container container-2">
        <div class="item">
            this is block item
        </div>
    </div>
    <div class="container container-3">
        <div class="item">
            this is absolute item
        </div>
    </div>
</body>
</html>

效果
在这里插入图片描述

3.2 垂直居中

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>垂直居中</title>
    <style>
        .container{
            border: 1px solid #ccc;
            margin: 10px;
            padding: 10px;
            height: 100px;
        }
        .item{
            background-color: #ccc;
        }
        /* 1. 将line-height和height设置相同的数值,可以让文字垂直居中 */
        .container-1{
            text-align: center;
            line-height: 100px;
            height: 100px;
        }
        /* 2. 利用top: 50% 和 margin-top: 负值,可以让block元素垂直居中 */
        .container-2{
            position: relative;
        }
        .container-2 .item{
            width: 300px;
            height: 40px;
            position: absolute;
            left: 50%;
            margin-left: -150px;
            top: 50%;
            margin-top: -20px;
        }
        /* 3. transform: translate(-50%, -50%)*/
        .container-3{
            position: relative;
        }
        .container-3 .item{
            width: 200px;
            height: 40px;
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
        }
        /* 4. top: 0;right: 0;bottom: 0; left: 0;margin: auto*/
        .container-4{
            position: relative;
        }
        .container-4 .item{
            width: 100px;
            height: 40px;
            position: absolute;
            top: 0;
            right: 0;
            bottom: 0; 
            left: 0;
            margin: auto
        }
    </style>
</head>
<body>
    <div class="container container-1">
        <span>一段文字</span>
    </div>

    <div class="container container-2">
        <div class="item">
            this is item
        </div>
    </div>

    <div class="container container-3">
        <div class="item">
            this is item
        </div>
    </div>

    <div class="container container-4">
        <div class="item">
            this is item
        </div>
    </div>

</body>
</html>

效果
在这里插入图片描述

3.2 使用flex布局

html

<div class="container">
	<div class="box"></div>
</div>

css

.box {
    width: 50px;
    height: 50px;
    background-color: #fff
}

.container {
    width: 150px;
    height: 150px;
    background-color: #000;
    display: flex;              // 开启flex布局
    justify-content: center;    // 水平居中
    align-items: center;		// 垂直居中
}

效果
在这里插入图片描述

3.3 绝对定位 + margin: auto

.container {
    width: 150px;
    height: 150px;
    background-color: #000;
    position: relative;
}

.box {
    width: 50px;
    height: 50px;
    background-color: #fff;
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    margin: auto;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

周兴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值