html里div水平居中,html+css div水平居中的几种方法

本文介绍了三种使用CSS实现元素居中的方法:第一种是通过设置width、margin:0 auto和text-align:center;第二种利用display:flex和margin:auto;第三种是利用display:table和margin:0 auto。这些方法适用于不同场景,帮助开发者灵活地实现页面元素的居中对齐。
摘要由CSDN通过智能技术生成

方法一:

为元素设置width宽度和margin:0 auto以及text-align:center; 实例如下:

width: 90%;

margin: 0 auto;

text-align:center;

}

Foo foo

方法二:

使用display: flex;和margin: auto, 实例如下:

.outer {

display: -webkit-flex;

display: flex;

//-webkit-justify-content: center;

//justify-content: center;

//align-items: center;

width: 100%;

height: 100px;

background-color: lightgrey;

}

.inner {

background-color: cornflowerblue;

padding: 2rem;

margin: auto;

//align-self: center;

}

Foo foo

方法三:

使用display: table;和margin: 0 auto;属性。 实例如下:

#inner {

display: table;

margin: 0 auto;

}

Foo foo
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值