HTML中设置元素水平居中的方案

1.给父元素设置padding,padding:(父height-子height)/2(上下) (父width-子width)/2(左右),给父元素设置为border-box

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .parent{
            width: 220px;
            height: 260px;
            background-color: red;
            margin: 0 auto;
            /* 1.2 */
            border: 1px solid red;
        }
        .child{
            width: 100px;
            height: 100px;
            background-color: blue;
            /* 1.1通过margin挤压   左右外边距 父元素宽-子元素宽/2        上下外边距 父元素高-子元素高/2 */
            margin: 80px 60px;
    </style>
</head>
<body>
    <div class="parent">
        <div class="child"></div>
    </div>
</body>
</html>

 浏览器运行结果如下:


2.通过margin挤压,给子元素设置margin:(父height-子height)/2(上下) (父width-子width)/2(左右) 给父元素设置一个边框

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .parent{
            width: 220px;
            height: 260px;
            background-color: red;
            margin: 0 auto;
            /* 2.1给父元素设置padding */
            padding: 80px 60px; 
            /* 2.2将父元素设置为border-box */
            box-sizing: border-box; 
        }
        .child{
            width: 100px;
            height: 100px;
            background-color: blue;
        }
    </style>
</head>
<body>
    <div class="parent">
        <div class="child"></div>
    </div>
</body>
</html>

 浏览器运行结果如下: 

3.子绝父相,子元素设置top,bottom,left,right全部为0 margin:auto

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .parent{
            width: 220px;
            height: 260px;
            background-color: red;
            margin: 0 auto;
            /* 3.1给父元素设置相对定位 */
            position: relative; 
        }
        .child{
            width: 100px;
            height: 100px;
            background-color: blue;
            /* 3.2子元素绝对定位  配合属性全部为0 margin:auto */
            position: absolute;
            top: 0;
            bottom: 0;
            left: 0;
            right: 0;
            margin: auto; 
        }
    </style>
</head>
<body>
    <div class="parent">
        <div class="child"></div>
    </div>
</body>
</html>

 浏览器运行结果如下: 

 

4.子绝父相,子元素设置top:50%,left:50%,margin-left:-子元素宽度/2 margin-top:-子元素高度/2

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .parent{
            width: 220px;
            height: 260px;
            background-color: red;
            margin: 0 auto;
            /* 4.1给父元素设置相对定位 */
            position: relative;
        }
        .child{
            width: 100px;
            height: 100px;
            background-color: blue;

            /* 4.2给子元素绝对定位 top:50%  left:50%  margin-left:-width/2  margin-top:-height/2 */
            position: absolute;
            top: 50%;
            left: 50%;
            margin-left: -50px;
            margin-top: -50px;
        }
    </style>
</head>
<body>
    <div class="parent">
        <div class="child"></div>
    </div>
</body>
</html>

 浏览器运行结果如下:

5.给父元素设置display:flex 给子元素设置 margin:auto

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .parent{
            width: 200px;
            height: 200px;
            background-color: red;
            /* margin: 0 auto; */
            /* 1.1给父元素设置为伸缩盒 */
            display: flex;
        }
        .child{
            width: 100px;
            height: 100px;
            background-color: blue;
            /* 1.2给子元素设置margin:auto */
            margin: auto; 
        }
    </style>
</head>
<body>
    <div class="parent">
        <div class="child"></div>
    </div>
</body>
</html>

  浏览器运行结果如下:

6.给父元素设置display:flex;justify-content:center;align-items:cente

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .parent{
            width: 200px;
            height: 200px;
            background-color: red;
            /* 2.给父元素设置伸缩盒 设置主轴和交叉轴弹性元素排列方式都为center */
            display: flex;
            /* 设置弹性元素在主轴居中对齐 */
            justify-content: center;
            /* 设置弹性元素在交叉轴居中对齐 */
            align-items: center;
        }
        .child{
            width: 100px;
            height: 100px;
            background-color: blue;
        }
    </style>
</head>
<body>
    <div class="parent">
        <div class="child"></div>
    </div>
</body>
</html>

 浏览器运行结果如下:

 

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值