html margincalc居中,超全面的CSS居中方法总结

行内元素的居中水平居中text-align:center;

content

.parent{

background-color: red;

text-align: center;

}

bVcLgx5fit-content;

content

.parent{

background-color: red;

width: fit-content;/*父元素适应子元素的宽度*/

margin: auto;

}

bVcLgyy垂直居中line-height;(只针对单行文本)

content

.parent{

background-color: red;

height: 200px;

line-height: 200px;

}

bVcLgzL

块级元素的居中水平居中

*margin:0 auto;

content

.parent{

background-color: red;

}

.child{

background-color: blue;

width: 100px;

margin: 0 auto;/*左右自适应*/

}

水平垂直居中定位

.parent{

background-color: red;

position: relative;

height: 200px;

}

.child{

background-color: blue;

width: 100px;

height: 100px;

position: absolute;

left: 50%;

top: 50%;

margin-top: -50px;

margin-left: -50px;

}

2.定位+transform

.parent{

background-color: red;

position: relative;

height: 200px;

}

.child{

background-color: blue;

position: absolute;

left: 50%;

top: 50%;

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

/*子元素大小未知*/

}

3.定位+margin

.parent{

background-color: red;

position: relative;

height: 200px;

}

.child{

background-color: blue;

width: 100px;

height: 100px;

position: absolute;

margin: auto;

left: 0;

right: 0;

top: 0;

bottom: 0;

}

4.padding

.parent{

padding: 20px;

background-color: red;

}

.child{

height: 200px;

background-color: blue;

}

5.flex

.parent{

display: flex;

height: 200px;

align-items: center;

justify-content: center;

background-color: red;

}

.child{

height: 100px;

width: 100px;

background-color: blue;

}

6.伪元素

7.函数calc

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值