A O A O - 9 months ago 39
CSS Question

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

So I have this

element, and I wanted to apply a
to it. Unfortunately, I have to specify the
attribute to be
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
element has it's dimensions automatically calculated:

enter image description here

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

enter image description here

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

Answer Source

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.