实现水平居中和垂直居中的方法

来源:互联网 发布:java两年经验工资多少 编辑:程序博客网 时间:2024/06/07 03:46

一、水平居中

1. text-align:center(行内元素)

  给其父元素设置属性 text-align:center;

2. margin: 0 auto(块级元素)

  给元素本身设置 margin: 0 auto;

3. 元素的宽度固定

.ele{    position:absolute;    width:固定;    left:50%;    margin-left:-0.5宽度;}

4.元素的宽度不固定

.ele{    position:absolute;      left:50%;      transform:translate(-50%,0);}

二、垂直居中

 1. line-height:eleparent-height

  单行文本的垂直居中可以设置属性 line-height:父元素height

2. 元素的高度固定

.ele-parent{    position:relative;}.ele{    position:absolute;    top:50%;    height:固定;    margin-top:-0.5高度;}

3.元素高度不固定

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







0 0
原创粉丝点击