author: Krasimir Tsonev

Krasimir is a blogger, who writes and speaks.
He loves open source and codes awesome stuff.

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.


blog comments powered by Disqus