css实现水平居中的几种方式

一、对于行内元素:

text-align:center;

 

二、对于确定宽度的块级元素:

(1)margin和width实现水平居中

常用(前提:已设置width值):margin-left:auto; margin-right:auto;

(2)绝对定位和margin-left: -(宽度值/2)实现水平居中

固定宽度块级元素水平居中,通过使用绝对定位,以及设置元素margin-left为其宽度的一半

三、对于未知宽度的块级元素:

(1)table标签配合margin左右auto实现水平居中

使用table标签(或直接将块级元素设值为display:table),再通过给该标签添加左右margin为auto

(2)inline-block实现水平居中方法

display:inline-block;(或display:inline)和text-align:center;实现水平居中

(5)CSS3的flex实现水平居中方法,法一

.contentParent{

display: flex;

flex-direction: column;

}

.content{

align-self:center;

}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值