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

CSS +Div水平垂直居中实现

2017-03-11 00:00 477 查看
1.固定宽度和高度
(1)父元素相对定位
position:relative;
(2)子元素相对于父元素绝对定位
position:absolute;
width:100px;
height:80px;
top:50%;
left:50%;
margin-left:50px;
margin-top:40px;
(可以替代后两行margin:-50px 0 0 -40px;)
这里使用了position的绝对定位和left和top,设置外边距的margin-left(值为宽度的一半),
margin-top(值为高的一半)
2.元素宽度和高度未知
(1)父元素相对定位
position:relative;
(2)子元素相对于父元素绝对定位
position:absolute;
top:50%;
left:50%;
margin-left:50px;
margin-top:40px;
transform:translate(-50%,-50%);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息