详解实现元素的水平垂直居中的n种方式

1. flex

在父元素设置如下属性:

display: flex;

justify-content: center;

align-items: center;

2. grid

在父元素设置如下属性:

display: grid;

justify-content: center;

align-items: center;

3. table-cell

在父元素设置如下属性:

display: table-cell;

vertical-align: middle;

在子元素设置如下属性:

width: 100px;

margin: 0 auto;

4. position 偏移剩余宽高的一半;

在父元素设置如下属性:

position: relative;

在子元素设置如下属性:

position: absolute;

top: 200px;

left: 200px;

5. position + margin: auto;

在父元素设置如下属性:

position: relative;

在子元素设置如下属性:

position: absolute;

top: 0;

left: 0;

right: 0;

bottom: 0;

margin: auto;

6. position + margin:负值;

在父元素设置如下属性:

position: relative;

在子元素设置如下属性:

position: absolute;

top: 50%;

left: 50%;

margin-top: -50px;

margin-left: -50px;

7. position + translate;

在父元素设置如下属性:

position: relative;

在子元素设置如下属性:

position: absolute;

top: 50%;

left: 50%;

transform: translate(-50%, -50%);

展开阅读全文

页面更新:2024-03-13

标签:元素   负值   详解   剩余   属性   水平   方式

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号

Top