父元素:position:relative;
子元素:position:absolute;top:50%;left:50%;margin-top:-子元素高度一半;margin-left:-子元素宽度一半;css
.first{ width:300px; height:200px; position:relative; } .item1{ width:200px; height:80px; position:absolute; top:50%; left:50%; margin-top:-40px; margin-left:-100px; }
父元素:position:relative;
子元素:position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);布局
.second{ width:300px; height:200px; position:relative; } .item-2{ width:60%; height:40%; position:absolute; top:50%; left:50%; transform:translate(-50%,-50%); }
父元素添加如下内容:
display:flex; //弹性布局
align-items: center; //水平居中
justify-content:center; //垂直居中学习
.third{ width:300px; height:200px; display:flex; justify-content:center; align-items: center; }