Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
A common element in websites is an image header. By that, I mean the use of a large image or logo image at the top of every page, like you see right here, this large image header. This ensures a visual identity and can be impactful. However, the logical way of doing it, simply adding an image to the top of the page, it's not really the correct way of doing it. If you add an image, you're not providing a header with text content, just a link to an image. There's a better way of doing this. With some clever CSS, you can get a header that has the correct text elements and still displays as if it was an image inside browsers, and you can make that header image link back to the home page just as if it was an image.
That sounds confusing until you see how it's done. So let's first take a look at what we have right now and why this isn't the right way of doing it. So what I have here at the top is this big image, and if you click on it you go back to the home page, so it seems like it works the way it's supposed to. Yes? Well, if we turn Styles off and then disable his image by breaking the link, you immediately see the problem. Because it was an image, there is no text information here.
So for a person who's not seeing the image, what they see is the first piece of text information, namely Main Menu, which is the menu header for the Main Menu, hardly what you want to display as a title of your site. A better option would be to add actual text as your header, but if you do that, then you don't have the image. Well, you can't have the image, but you have to know how to do it. So first I'm going to add just the text I want in the header and then we'll add the image afterwards. So let's go to the mark up in our project file and take a look at what it looks like right now.
So when we scroll down, you'll see here on line 18 we have this div id="header-image" and then there's an anchor and then the image itself. So what I want to do is replace this with some text. So first, I'm going to a new HTML5 element called hgroup. This is not age group, as in the age of a person; it's hgroup as in group of headings. So I'll end that type first so that there's no confusion, and then inside here I want to add both an h1 and an h2 header, because I want the h1 to say Hansel & Petal Flower Company and I want the h2 to say, from our garden to your home, which coincidentally matches what the graphic says right here.
So I'll add an h1 first. Say Hansel & Petal Flower Company, and then I'll add the h2. It says, From our garden to your home. And then because I want this to be a link, I'll add a link around Hansel & Petal Flower Company. So I'll go down here and I'll simply borrow the link code from the image we had before and I'll wrap it around the text, Hansel & Petal Flower Company.
Then because this link is a link, it needs a title tag, so I will say title=Hansel & Petal Flower Company home page. Now I can delete the code that calls the image and save the page, and when we go back to the page now, you'll see, instead of the image, we have this Hansel & Petal Flower Company link, and then the subtitle. The next step is to add the image back in. So here I want to do two things.
I want to add the image as a background element, and then I also want to hide the text. This is all done in CSS, but first we need to create some IDs to attach CSS to. So I'll go back into my markup, and then for the h1, I'll add the id="site-title" and for the h2 I'll add the id="site-description". Now I can create CSS styles that hook up to these IDs. I'll go and open my style sheet and then under Header I can add some new styles.
So I'll go here and I'll create a new style called site-title, because that's the new ID, and then I'll say a, because I want to target the anchor inside, meaning the link. The reason why I do this is because I want the image I place there to become clickable so that I can go back to the home page. Now, I need to add the background image. So I will say background: url and then I'll point to the image. In this case it's under images/headerBanner.png.
I don't want this image to repeat, so I'll just set it to no-repeat and then I can now save it. So I've saved the style sheet and index.html page and I'll reload it in my browser. Now you see we have the background image, but the background image only spans the length of the text itself. I want the background image to be the same size it was before. So what I want to do is match the size of this box to the size of the image itself. I already know that the image is 183 pixels tall and 900 pixels wide, so I can go in and add that to my styles.
So I go and add a new line and say height: 183px and then width: 900px, and because I want it to display as a block element so that it wraps around, I have to set display to block. Now when I reload the page, you'll see the image is the size I want, and you notice that wherever I hover inside the image, it's clickable. So I'm almost where I want to be. The only thing that's missing is that I can still see the text Hansel & Petal Flower Company.
So I need to get rid of that. And I'm going to do this the same way we got rid of the section header. So I'll go in and finally say text-indent:-9999px. Reload one final time, and you see now the text is gone because it's been indented to the left so far that you can never see it. That leaves us with just one problem, the subtitle From our garden to your home. We need to get rid of that as well. So I'll go back in and create a new style for the site-description, and I'll use the same text indent trick we did with the section-headers.
So I'll first position it absolutely, and then I'll do text-indent. Reload the page and we now have that header image clickable, containing the text information we need, and it doesn't display the text. So if now go and Disable Styles, you'll see that the header of the page now is Hansel & Petal Flower Company, From our garden to your home, rather than nothing.
Of course, this still doesn't look exactly the way we want it to. I need to work a bit on my margins here and get rid of that hover effect, but that can be done later on. This is the general principle of adding a proper page header and you can also use this to create any other image buttons, so that the image buttons always contain real text, in case the image doesn't work. By adding and hiding header text at the top of your site and adding an image background to the header, you get all the benefits with none of the pain.
The page has a proper text header readable by browsers, people, and search engines alike, and for those using a visual browser the image and logo will display as intended. The same technique can and should be used any time you want to replace text with an image. That way you preserve the text semantics, and still get the graphic display.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 97968 Viewers
80 Video lessons · 141244 Viewers
59 Video lessons · 59606 Viewers
52 Video lessons · 72865 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.