您的位置:首页 > Web前端 > CSS

关于css基础内容居中的思考与总结

2017-11-12 18:02 393 查看
水平居中

1.行内元素局中(只讨论一个父元素和子元素)
方法一:
css代码
body{
text-align: center;
}
p{
display: inline;
background:#ff3;
}
2.块状元素局中
方法一(把块状元素转化为行内元素看待):body{
text-align: center;
}
.container{
width:100px;
height:100px;
background:#ff3;
display: inline-block;

}方法二(常规方法):
.container{
width:100px;
height:100px;
background:#ff3;
margin:auto;

}
方法三:
.container{
float:left;
position:relative;
left:50%;
}
.children:{
width:100px;
height:100px;
position:relative;
left:-50%;
(right:50%)
}
方法四(flex模型,适用于一个或多个子元素):
body{
display: flex;
justify-content: center;
}
.container{
width:100px;
height:100px;
background:#ff3;

}水平垂直居中
方法一:#container{
position:relative;
}
#center{
width:100px;
height:100px;
position:absolute;
top:50%;
left:50%;
transform: translate(-50%,-50%);
}方法二:#container{
position:relative;
}
#center{
width:100px;
height:100px;
position:absolute;
top:50%;
left:50%;
margin:-50px 0 0 -50px;
}方法三:#container{
position:relative;
}
#center{
position:absolute;
margin:auto;
top:0;
bottom:0;
left:0;
right:0;
}方法四:#container{
display:flex;
justify-content:center;
align-items: center;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: