Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does adding text to a span change its position?

Tags:

html

css

See this fiddle. When I take out the text, the spans line up next to each other nicely. When I add text to one, it appears lower in the page.

<div>
    <span id="first">
    </span>
    <span id="second">
        Text
    </span>
</div>

span#first {
    display:inline-block;
    width: 100px;
    height: 100px;
    background-color: red;    
}

span#second {
    display: inline-block;
    width: 100px;
    height: 100px;
    background-color: yellow;   
}
like image 377
Rose Perrone Avatar asked May 28 '12 03:05

Rose Perrone


1 Answers

The default value of vertical-align is baseline. The CSS spec says that for vertical-align: baseline

Align the baseline of the box with the baseline of the parent box. If the box doesn't have a baseline, align the bottom of the box with the parent's baseline.

An empty block has no baseline, so the bottom of the span lines up with the baseline of the parent div. The next span, since it has text (and therefore a baseline), lines up the text with the baseline of the div.

If you set vertical-align: top on both span elements, they should line up correctly.

like image 144
jimr Avatar answered Nov 03 '22 00:11

jimr