Say, you want to make a header for your site, but not just any header. You want some sort of cool graphic and your very own custom font all stylized the way you like it. This sort of decoration is just way too complicated for CSS and thats ok. What helps me is to think of the semantic meaning of a header. In this case, it is sort of a masthead on your site. Hmmm….what tag might work for a masthead or header. Wait! A header tag!

I might markup the XHTML like the following:
<h1>Rogie's site</h1>

But, that is just too simple. I can’t very well replace all <h1> tags with a header, that would be ridiculous. I would create the XHTML like the following:

<div id="masthead">
<h1>Rogie's site</h1>
</div>

which looks like this:

Rogie’s Site

Now, we are getting where we need to be to start the CSS. First, we need to have an image for the header instead of the text, so I have one here to use. This little bad boy is 450px wide by 100px high…sweet…good to know.

Let’s whip out some rockin’ CSS now:

div#masthead{
display:block; //just in case
width:450px;
height:100px;
background:url(http://komodomedia.com/wp-content/uploads/2006/07/sample_header.gif) left top no-repeat;
}

Whoa there fella. Ease up. Let’s not go too fast now. Let’s see what it looks like now:

Rogie’s Site

Sweet, now we are getting closer, however, we still have one issue: The text is still showing up. This is where image replacement techniques come in. I personally like the text-indent method, but do your research and find which method is the best for your audience and purposes.

Alright, now I will add the new CSS:

div#masthead{
display:block; //just in case
width:450px;
height:100px;
background:url(http://komodomedia.com/wp-content/uploads/2006/07/sample_header.gif) left top no-repeat;
}
div#masthead h1{
text-indent:-9000px; //move the text 9000 pixels to the left (off the screen)
}

Let’s see what that does:

Rogie’s Site

A problem may arise when the user resizes their browser text. To simulate this, I have made the header much larger. Check this out:

Rogie’s Site

Do you notice that there is a large gap above the image? That is because the header margin, padding, and line-height has outgrown the height of the box. To correct this, we need to add one more CSS rule (changes in bold text):

div#masthead{
display:block; //just in case
width:450px;
height:100px;
background:url(http://komodomedia.com/wp-content/uploads/2006/07/sample_header.gif) left top no-repeat;
overflow:hidden;
}
div#masthead h1{
text-indent:-9000px; //move the text 9000 pixels to the left (off the screen)
}

Let’s see it now:

Rogie’s Site

Great! Our technique is finished. Now we have a stylized header, but a semantic tag to describe it. Any questions/comments?

Comments

12 Comments

  1. Ben McNelly said 2501 days ago

    Nice ;-)

    Tis logical and beautifull.

  2. Sam said 2500 days ago

    Now, as with most header images, how will you accomplish making it a link to the homepage?

  3. greengnn said 2500 days ago

    this is not a new thing! :D,but your desgin is very good!

  4. Rogie said 2500 days ago

    @greengnn: Thanks, this tutorial is not intended to be a new thing, but merely a recap of CSS tricks I use often. It is an attempt to help people new to CSS have a collection of general tricks and techniques to help them on their way :)

    @Sam: If you are using a link, you would probably want to change all of the CSS rules that target div#masthead to target a link within the masthead, like this: div#masthead a#home-link.

  5. Andrew Ingram said 2499 days ago

    I had heard that you’re only really supposed to use h1 once per page, but I imagine this is a subjective issue.

    But couldn’t you achieve the same results with the following?

    Rogie’s site

    and

    h1#masthead{
    display:block;
    width:450px;
    height:100px;
    background:url(http://komodomedia.com/wp-content/uploads/2006/07/sample_header.gif) left top no-repeat;
    overflow:hidden;
    text-indent:-9000px;
    }

  6. Andrew Ingram said 2499 days ago

    hm, it seems the tags in my comment were stripped, but basically it was the same as your markup but with the div removed and the id moved to the h1 tag.

  7. Rogie said 2499 days ago

    @Andrew: Sure you could man. I created a div for the masthead because a lot of times, people want to put more in their masthead than just a header. But, it looks like you got it nailed, so either way works.

  8. Arnor said 2479 days ago

    Now; I use this technique myself – and it was just a matter of hours after I published the new design, that someone pointed out the fact that if you (for some reason) choose to browse with CSS – but sans images – nothing at all shows up…

    I honestly doesn’t think it’s much of problem, as both text-browsers and screenreaders just turnn the whoe CSS off.. As for low-bandwith-users, I usually reccomend they do the same…

    But it’d still be nice if anyone knew of a workaround…

  9. Amit Lamba said 2328 days ago

    There is a more accessible way to do image replacement (IR). There are pros and cons for all the variations out there, but I like the Leahy/Langridge and Gilder/Levine IR techniques which both deal specifically with the CSS on, Images Off problem.

    You can find more info here:
    http://www.mezzoblue.com/tests/revised-image-replacement/

  10. maribel said 2235 days ago

    im learning html but i can not see CSS codes in Dreamweaver>? why?

  11. Bikram said 2150 days ago

    Why would I go into so much pain when I can simply use -

    –>

  12. peter said 2065 days ago

    what about to change color in mastehead to black

Sorry, the comment form is closed at this time.