Why won't vertical-align: middle
work? And yet, vertical-align: top
does work.
<div>
<img style="width:30px;height:30px">
<span style="vertical-align:middle">Doesn't work.</span>
</div>
Why won't vertical-align: middle
work? And yet, vertical-align: top
does work.
<div>
<img style="width:30px;height:30px">
<span style="vertical-align:middle">Doesn't work.</span>
</div>
You can set image as
inline element
usingdisplay
propertyYou probably want this:
As others have suggested, try
vertical-align
on the image:CSS isn't annoying. You just don't read the documentation. ;P
You have to apply
vertical-align: middle
to both elements to have it been centered perfectly.The accepted answer does center the icon around half of the x-height of the text next to it (as defined in the CSS specs). Which might be good enough but can look a little bit off, if the text has ascenders or descenders standing out just at top or bottom:
On the left, the text is not aligned, on the right it is as shown above. A live demo can be found in this article about vertical-align.
Has anyone talked about why
vertical-align: top
works in the scenario? The image in the question is probably taller than the text and thus defines the top edge of the line box.vertical-align: top
on the span element then just positions it at the top of the line box.The main difference in behavior between
vertical-align: middle
andtop
is that the first moves elements relative to the box's baseline (which is placed wherever needed to fulfill all vertical alignments and thus feels rather unpredictable) and the second relative to the outer bounds of the line box (which is more tangible).Write these span properties
Use
display:inline-block;
When you usevertical-align
property.Those are assosiated propertiesI generally use 3px in place of
top
. By increasing/decreasing that value, the image can be changed to the required height.Actually, in this case it's quite simple: apply the vertical align to the image. Since it's all in one line, it's really the image you want aligned, not the text.
Tested in FF3.
Now you can use flexbox for this type of layout.