8种垂直居中的方法

8 篇文章 0 订阅
8 篇文章 0 订阅

八种垂直居中的方法

垂直居中的需求经常遇到,通过资料实践了八种垂直居中的方法。

以下的方法都围绕着该HTML展开
HTML代码

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

CSS
方法1(常用):display:flex

.wrap{
	width:300px;
	height:300px;
    border: 1px solid red;
    display:flex;
    justify-content:center;
    align-items:center;
}
.box{
	height:100px;
	width:100px
    boder:1px solid blue;
}

方法2: table-cell

vertical-align 属性设置一个元素的垂直对齐方式。
该属性定义行内元素的基线相对于该元素所在行的基线的垂直对齐。允许指定负长度值和百分比值。这会使元素降低而不是升高。在表单元格中,这个属性会设置单元格框中的单元格内容的对齐方式。

        .wrap{
            width: 300px;
            height: 300px;
            border: 1px solid red;
            display: table-cell;
            text-align: center;
            vertical-align: middle;
        }
        .box{
            width: 100px;
            height: 100px;
            border: 1px solid blue;
            display: inline-block;
        }

方法3: margin,transform配合

.wrap{
            width: 300px;
            height: 300px;
            background-color: pink;
            /* border: 1px solid red; */
            /*防止外边距塌陷。解决外边距塌陷的方法:
            父元素加overflow:hidden或加上边框*/
            overflow: hidden;
        }
        .box{
            width: 100px;
            height: 100px;
            background-color: plum;
            margin:50% auto;
            transform: translateY(-50%); 
        }

方法4: inline-block+vertical-aligin

        .wrap {
            width: 300px;
            height: 300px;
            background-color: pink;
            text-align: center;
            line-height: 300px;
        }
        .box {
            width: 100px;
            height: 100px;
            /* 重新设置子元素的行高,否则会继承父元素的行高*/
            line-height: 100px;
            background-color: plum;
            display: inline-block;
            /* middle   把此元素放置在父元素的中部。 */
            vertical-align: middle;
        }

方法5:absolute+负margin

        .wrap{
            width: 300px;
            height: 300px;
            position: relative;
            background-color: plum;
        }
        .box{
            width: 100px;
            height: 100px;
            position: absolute;
            left: 50%;
            top: 50%;
            /*宽高的一半*/
            margin-left: -50px;
            margin-top: -50px;
            background-color: powderblue;
        }

方法6 absolute+margin:auto
和方法5类似,当宽度和高度未知时使用

        .wrap{
            width: 300px;
            height: 300px;
            position: relative;
            background-color: plum;
        }
        .box{
            width: 100px;
            height: 100px;
            position: absolute;
            left: 0;
            top: 0;
            bottom:0;
            right:0;
            margin:auto;
            background-color: powderblue;
        }

方法7:absolute+transform
与方法5类似

 .wrap {
            width: 300px;
            height: 300px;
            position: relative;
            background-color: plum;
        }

        .box {
            width: 100px;
            height: 100px;
            position: absolute;
            left: 50%;
            top: 50%;
            
            transform: translate(-50%,-50%);
            background-color: powderblue;
        }

方法8 强大的grid
阮一峰大佬Grid 网格布局教程

.wrap {
            width: 300px;
            height: 300px;
            display: grid;
            background-color: plum;
        }

        .box {
            width: 100px;
            height: 100px;
            align-self: center;
            justify-self: center;
            background-color: powderblue;
        }

总结

写到这里也结束了,在文章最后放上一个小小的福利,以下为小编自己在学习过程中整理出的一个关于 前端开发 的学习思路及方向。从事互联网开发,最主要的是要学好技术,而学习技术是一条慢长而艰苦的道路,不能靠一时激情,也不是熬几天几夜就能学好的,必须养成平时努力学习的习惯,更加需要准确的学习方向达到有效的学习效果。

由于内容较多就只放上一个大概的大纲,需要更及详细的学习思维导图的 点击我的GitHub免费获取
还有免费的 高级web全套视频教程 前端架构 H5 vue node 小程序 视频+资料+代码+面试题!

全方面的web前端进阶实践技术资料,并且还有技术大牛一起讨论交流解决问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值