加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 创业 > C语言 > 正文

垂直居中

发布时间:2020-12-15 00:44:36 所属栏目:C语言 来源:网络整理
导读:1.绝对定位——margin:auto 原理:元素在过度受限情况下,将margin设置为auto,浏览器会重算margin的值,过度受限指的是同时设置top/bottom与height或者left/right与width。.parent{position: relative;width: 500px;height: 500px;background-color: #ccc;}

1.绝对定位——margin:auto

原理:元素在过度受限情况下,将margin设置为auto,浏览器会重算margin的值,过度受限指的是同时设置top/bottom与height或者left/right与width。
.parent{position: relative;width: 500px;height: 500px;background-color: #ccc;}
.pos{position: absolute;background-color: #f0f;top: 0;bottom: 0;left: 0;right: 0;width: 200px;height: 200px;margin: auto;}

2.绝对定位——top:50%

.pos{position: absolute;background-color: #f0f;top: 50%;left: 0;right: 0;width: 200px;height: 200px;margin: auto;}
然后设置margin-top:-1/2自身高度的一半;已知高度时,如上,直接设置:margin-top:-100px;
或者用css3: {top:50%;left:50%;transform: translate(-50%,-50%);}宽高可以不设置,不设置的时候,宽度为parent的1/2

3.table布局

.parent{display:table;}
.pos{display:table-cell;vertical-align: middle;}

4.flex布局

.parent{display:flex;align-items: center;justify-content: center;}

5.单行文字

{height:30px;line-height:30px;}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读