绝对定位水平垂直居中
1.绝对定位水平垂直居中
<style> .box {
position: absolute;
/* 1. left 走 50% 父容器宽度的一半 */
left: 50%;
/* 2. margin 负值 往左边走 自己盒子宽度的一半 */
margin-left: -100px;
top: 50%;
margin-top: -100px;
width: 200px;
height: 200px;
background-color: pink;
/* margin: auto; */
}
</style>
2.图片在盒子中垂直水平居中
<style>
body {
background-image: url(images/bg.jpg);
background-repeat: no-repeat;
//水平居中
/* background-position: center top; */
background-position: center top;
//垂直水平居中
background-position: center ;
}
</style>
3.块级盒子水平居
外边距可以让块级盒子水平居中,但需要满足两个条件:
- 盒子必须指定了宽度
盒子左右的外边距都设置为auto
4.行内元素或者行内块元素水平居中
给其父元素添加 text-align:center 即可
里面的文字
还没有评论,来说两句吧...