Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are inline block elements misaligned?

Tags:

html

css

Problem

Inside a div with vertical-align: top; I have two elements, an img and a span (with aaa inside).

Both elements have display: inline-block; and width and height set.

No matter what I do and what I try, the elements are MISALIGNED as shown on picture below. What is wrong and how can I fix this?

This problem occurs in every browser - FF, IE, Safari ... http://jsfiddle.net/CaU59/

like image 452
Dusan Avatar asked Nov 05 '13 21:11

Dusan


People also ask

How do you align inline block elements?

To align things in the inline direction, use the properties which begin with justify- . Use justify-content to distribute space between grid tracks, and justify-items or justify-self to align items inside their grid area in the inline direction.

What is the purpose of inline block?

Compared to display: inline , the major difference is that display: inline-block allows to set a width and height on the element. Also, with display: inline-block , the top and bottom margins/paddings are respected, but with display: inline they are not.

How are inline and block elements different from each other?

By default, inline elements do not force a new line to begin in the document flow. Block elements, on the other hand, typically cause a line break to occur (although, as usual, this can be changed using CSS).

Does text-align work on inline elements?

Even though the property says “text” align, it affects all elements inside the block-level element that are either inline or inline-block elements. The property only affects the content inside the element to which it is applied, and not the element itself.


1 Answers

You need to add vertical-align to the elements themselves, not their container. They are currently aligned baseline as you can see from the text lining up with the image.

http://jsfiddle.net/CaU59/2/

like image 154
James Montagne Avatar answered Nov 15 '22 07:11

James Montagne