android html中label居中,CSS居中的方法总结

CSS水平和垂直居中在开发中经常用到,在此加以总结。

水平居中方法

1.行内元素水平居中,设置父元素的text-align: center。

center

#box {

text-align: center;

}

常用的行内元素有a、abbr、b、br、em、input、label、select、span、strong、sub、sup、textarea等。

2.宽度固定的块级元素水平居中,设置该元素的margin: 0 auto。

center

#box {

width: 64%;

margin: 0 auto;

}

常用的块级元素有address、article、audio、blockquote、canvas、div、footer、form、h1、header、hr、ol、output、p、pre、section、table、ul、video等。

3.绝对定位元素水平居中,设置父元素position: relative,设置该元素left: 0; right: 0; margin: 0 auto;。

center

#box {

position: relative;

}

#content {

position: absolute;

left: 0;

right: 0;

margin: 0 auto;

}

4.flex布局水平居中,设置父元素display: flex; justify-content: center。

center

#box {

display: flex;

justify-content: center;

}

垂直居中方法

1.单行文本垂直居中,设置该元素line-height为父元素height高度。

center

#content {

height: 2em;

line-height: 2em;

}

2.将父元素显示为表格display: table-cell,利用vertical-align: middle设置垂直居中。

center

#box {

display: table-cell;

vertical-align: middle;

}

3.绝对定位该元素并设置父元素positoin: relative,设置该元素top:0; bottom: 0; margin: auto;

center

#box {

position: relative;

}

#content {

position: absolute;

top: 0;

bottom: 0;

marigin: auto;

}

4.绝对定位固定高度是,设置top: 50%,margin-top值为高度值的一半。

center

#box {

position: relative;

}

#content {

position: absolute;

height: 12em;

top: 50%;

margin-top: 6em;

}

5.flex布局,设置父元素display: flex; align-items: center

center

#box {

display: flex;

align-items: center;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值