Why does applying `inline-block` force dimensions onto my span element?

[亡魂溺海] 提交于 2020-01-14 03:42:07

问题


So I have this <span> element, and I wanted to apply a webkit-transform to it. Unfortunately, I have to specify the display attribute to be inline-block in order for this to work.

span {
  display: inline-block;
}

But once I apply this attribute, the entire span shifts.

You'll note that before I set it, the <span> element has it's dimensions automatically calculated:

But once I do set the property, the dimensions are computed, and they are slightly different from what is automatically calculated previously:

Why does this happen? Is there a way I can apply inline-block without the <span> changing dimensions at all? I can't tell if there's padding/margins or whatever being applied but it definitely is moving


回答1:


Normally changing an element from inline to inline-block will not automatically change its size, even though the computed width and height change from auto to numbers.

One thing that's different for inline-block elements is that their margins and paddings in all directions are respected, while inline elements only have horizontal margins and paddings. So if you set vertical margins/paddings on the span, that would explain why it is moving after being changed into inline-block.

Otherwise, it's hard to know the answer without seeing the actual page.




回答2:


As inline-block makes your span to behave as block element it will have width and height

you may consider using display:flex for this

.container{
  display:flex;
  flex-direction:column;
}
.container span:first-child{
   -webkit-transform: rotate(2deg);
}
<div class="container">
<span>
  span1
</span>
<span>
  span2
</span>
</div>

Hope this helps



来源:https://stackoverflow.com/questions/40291856/why-does-applying-inline-block-force-dimensions-onto-my-span-element

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!