css - 不能居中的问题

查看:74
本文介绍了css - 不能居中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

为什么在子元素上加margin:0 auto不能居中 给父元素加上text-align才能居中?

#div1{height: 80px;width: 500px;background-color: blue;position: relative;}
#sp1{display: inline-block;margin:0 auto;height:30px;background-color: red;width: 100px;}
<div id="div1">
    <span id="sp1">aaaaaaaaaaa</span>
</div>

解决方案

inline-block实际还是行内元素,只是可以设置宽高,所以你这个margin:0 auto无效

这篇关于css - 不能居中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆