position: relative;
position: absolute; top: 0; right: 0; bottom: 0; left: 0; margin: auto;
设置该图片容器的css样式为: display: table-cell;text-align:center;vertical-align:middle;
.img{ width: 100px; height: 100px; position:absolute; left:50%; top:50%; margin-left:-50px; margin-top:-50px; }
我上面的三位大神分别用了css3的居中,table-cell特有的属性和盒子模型的margin来解决,小弟不才,推荐不那么兼容ie的方法:
display:flex; justify-content:center; align-items:center }
当然了,元素要有高度才行
这道题你会答吗?花几分钟告诉大家答案吧!
扫描二维码,关注牛客网
下载牛客APP,随时随地刷题