Check out "Do you speak JavaScript?" - my latest video course on advanced JavaScript.
Language APIs, Popular Concepts, Design Patterns, Advanced Techniques In the Browser

CSS: The background color and overlapping rows

The goal is to achieve a background of the text inside an inline element.

Alt text

I started with a simple html like:

    Lorem ipsum dolor sit amet, consectetur adipiscing elit. In tempor tincidunt erat, et facilisis metus feugiat at. 

And the following CSS:

h1 {
    text-transform: uppercase;
    font-family: Arial;
    line-height: 30px;
    font-size: 30px;
    padding: 10px;
    background: #7f7f7f;
    color: #FFF;
}

Because the h1 tag by default is a block element the background takes the whole area

Alt text

So, I made my title inline element with display: inline and the result is:

Alt text

The solution was to wrap the text inside the title in a span element.

    Lorem ipsum dolor sit amet, consectetur adipiscing elit. In tempor tincidunt erat, et facilisis metus feugiat at. 

And add one more style about the new element:

h1 span {
    position: relative;
}

Here is the final result:

Yes, I know that the result is no as good as my image made in photoshop, but you can play with line-height and font-size and will get closer.

If you enjoy this post, share it on Twitter, Facebook or LinkedIn.