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>
Change your
div
into a flex container:Now there are two methods to center the alignments for all the content:
Method 1:
DEMO
Method 2:
DEMO
Try different width and height values on the
img
and different font size values on thespan
and you'll see they always remain in the middle of the container.Firstly inline CSS is not recommended at all, it really mess up your HTML.
For aligning image and span, you can simply do
vertical-align:middle
.Multiline solution:
http://jsfiddle.net/zH58L/6/
Works in all browers and ie9+
For the record, alignment "commands" shouldn't work on a SPAN, because it is an in-line tag, not a block-level tag. Things like alignment, margin, padding, etc won't work on an in-line tag because the point of inline is not to disrupt the text flow.
CSS divides HTML tags up into two groups: in-line and block-level. Search "css block vs inline" and a great article shows up...
http://www.webdesignfromscratch.com/html-css/css-block-and-inline/
(Understanding core CSS principles is a key to it not being quite so annoying)
Because you have to set the
line-height
to the height of the div for this to workBasically, you'll have to get down to CSS3.