Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Line height and background color (Span vs Div)

Tags:

html

css

Its like few days pass and again when I try to recall what I read about line-height is something misleading what I am seeing

<span>Color me</span>

span {
    line-height: 52px;
    background: red;
    font-size: 14px;
}

Why it does not color complete box (i.e complete line-height)?

But When I do the same with div it colors as required.

<div>Color me</div>

div {
    line-height: 52px;
    background: red;
    font-size: 14px;
}
like image 520
Deepak Ingole Avatar asked Jul 24 '15 04:07

Deepak Ingole


2 Answers

In this particular case you need to add the following:

span {
  display: inline-block;
  /* ... */
}

As for the reason why, see this StackOverflow answer.

like image 129
jdlm Avatar answered Sep 30 '22 04:09

jdlm


Since span is an inline element it occupies only the height of the text and it does not cover the full area whereas in div it is a block element so it can cover the full area.

The method to convert the inline element to block element is

span{display: inline-block;} 
like image 38
Sai Deepak Avatar answered Sep 30 '22 05:09

Sai Deepak