前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >8种垂直居中的方法

8种垂直居中的方法

作者头像
全栈程序员站长
发布2022-07-21 14:58:48
2040
发布2022-07-21 14:58:48
举报

大家好,又见面了,我是你们的朋友全栈君。

八种垂直居中的方法

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

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

代码语言:javascript
复制
    <div class="wrap">
        <div class="box"></div>
    </div>

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

代码语言:javascript
复制
.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 属性设置一个元素的垂直对齐方式。 该属性定义行内元素的基线相对于该元素所在行的基线的垂直对齐。允许指定负长度值和百分比值。这会使元素降低而不是升高。在表单元格中,这个属性会设置单元格框中的单元格内容的对齐方式。

代码语言:javascript
复制
        .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配合

代码语言:javascript
复制
.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

代码语言:javascript
复制
        .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

代码语言:javascript
复制
        .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类似,当宽度和高度未知时使用

代码语言:javascript
复制
        .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类似

代码语言:javascript
复制
 .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 网格布局教程

代码语言:javascript
复制
.wrap { 
   
            width: 300px;
            height: 300px;
            display: grid;
            background-color: plum;
        }

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

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/124788.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年4月4,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 八种垂直居中的方法
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档