Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
617 views
in Technique[技术] by (71.8m points)

关于display:inline-block 元素,设置margin: 0 auto; 没有居中效果问题

遇到一个inline-block元素,想让其居中显(span元素水平居中在div中)示,设置margin: 0 auto;也设置了宽度,但没有效果,如果把元素改为block,确可以居中显示,不知是什么原因?
图片描述图片描述

html结构
<div class="closearea">
<span class="close">close</span>
</div>

css样式(scss语法):
//关闭按钮

.closearea {
  width: 100px;
  height: 100px;
  position: relative;
  background-color: #e84f41;
  .close {
    font-size: 0px;
    display: inline-block;
    height: 50px;
    width: 50px;
    border: 1px solid red;
    background-color: #1be8b2;
    margin: 0 auto;//display: inline-block;时不起作用,但是block时起作用 ???
  }
}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

inline-block是将元素转换为行内元素,所以你在设置margin: 0 auto 也是无用。
现在布局用的比较多的是flex,
推荐一篇文章:http://www.ruanyifeng.com/blo...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...