Start learning with our library of video tutorials taught by experts. Get started
Viewed by members. in countries. members currently watching.
Join James Williamson, as he shows you how to create elegant menus, links, and buttons that help visitors navigate your site faster and more intuitively. The course covers creating structured navigation that is accessible and clean, styling links, and building horizontal and vertical menus with rollover effects. The last chapter reveals how to create stylish buttons with special effects and CSS sprites.
While the addition of block-level links has given us more flexibility when structuring our pages, it does present us with certain styling challenges. In this exercise I want to dig a little deeper into styling these block-level linked regions. And we talked about these earlier way back in Chapter 1, I am going to start talking about some of those styling issues that you need to consider if you want to use block level link regions in your own files. So I have a block-level.htm file open in the 06_06 folder, and the first thing I am going to do is just preview this in a browser so you can see what's was going on just sort of a way of refreshing your memory.
So we have this product detail region, and there is a link tag wrapped around the h2, the image, the paragraph down here, and you see when I hover over one item all of the other items get its styling, and that's the biggest problem that you're going to run into with these block-level links. Everything inside of it shares the link state, so if I click, for example, everything gets its active state. If I focus on it, everything gets the focus state of it. So it's one of those things where you have to be very, very careful about how you style it, and you have to be very, very careful about how styles are inherited.
So I am going to go back into the block-level, and just to refresh your memory on the structure of this, notice that we have a link element, it has a class of detail, it also has a class of, and if you remember from earlier when we are applying a clear fix to it, that should give you some ideas what's going to happen inside of this anyway. Then we have a couple paragraphs, an image and an h2, all surrounded by that link. Okay, let's go up and start styling this. So I am just going to start in my block-level styles, and since that anchor element is sort of a parent tag the same way that sometimes div tags or section tags are parent tags, I'd really like to sort of style that into a very distinct region.
So I am going to take the class that we've applied to that link, which is detail, I am just going to give it some styling. So the first thing I am going to do is I am going to say, okay, I would like the width of that to be 600 pixels, so again, I am going to make it a very distinct region. I am going to give it some padding, and I'll give it a padding of 20 pixels all the way around. I am going to set the line-height for all the text inside of it to 1.4. I am going to give it a background, and I am just going to give it a background color. So I am going to do rgb (184, 205, 205), which is a light blue--and no, I don't have those memorized-- and then a border of 4 pixels, so a big border around this, solid, and then another RGB color there, here we'll do a little bit of darker blue, 91, 123, 164.
So if we were styling a div tag right now--I need to go ahead and close my curly bracket there--but if we were styling a div tag right now or a section, these are box model properties that are going to clearly define that region. But since this is a block-level link, let's see how this works. So I am going to save this, go back into my browser, and refresh it, and well, that didn't work at all. I can see some of the styling there, but it's not what I was looking for at all. Now the reason for that is that even though you've wrapped block-level elements with an anchor element to the browser, the anchor element is still an inline element.
So what it's doing is it's creating these inline boxes sort of wrapping them around each one of the block- level elements inside of it. So in order to get around that, it's actually a pretty simple fix. We just come into the detail selector that we just created, and we tell the link that we want it to display as a block-level element. So saving that, previewing it, and hey cool, now we are getting that sort of defined region that we want. Okay, so now we can go inside that region to start styling some of those interior elements.
So .detail h2, and inside that I'm going to do a font-size of 1.6 ems, and I am just going to do margin-top of 0, and that's just going to strip out any default margin there. The next thing I am going to do I am going to tackle the image. So I am going to say .detail img, and don't forget the period in front of detail. Remember, it is a class selector, and for that I'm going to do a float to the left, and that's going to wrap the text around an image, and I am going to do a margin right of 1 em, and it's going to hold the text off that image.
Now, that's the reason that I have that clear fix on class applied to that link. That way, it will contain this image and not let the image overflows the contents. All right, I am going to go ahead and save that, go back and refresh that, and that's starting to look a little bit better. Now, there are still some things that I want to do with this, maybe I don't like the color of the paragraph text, maybe I would like the price to be styled a little bit different, and also I notice that when I hover over anywhere in the link everything still gets the hover styling applied to them, so we re still going to need to deal with that too. Underneath .detail image, I am going to do .detail p, and with that I am going to do a color.
So I am going to change the color of this. I am going to do rgb and the color that I am going to give this is 54, 64, 75. So it's kind of darker blue, still blue though. Underneath that I am going to change the font size, I am going to make the font size a little bit bigger, it's a separate defined region, so I am going to make the font size a little bit bigger of 1.2 ems. I am going to follow that with the price. Now I didn't point this out to you earlier, but that one paragraph down there, that price is inside of I actually applied a class of price to it as well.
So I am going to say .detail .price to target that piece of text, and inside that I want to change its font-size to 1.6 ems. So I am going to make it larger, I am going to change its font-weight to bold, I want to apply a text-align to the right so it aligns to the right of our defined region, and finally, I am going to turn the margin-bottom off so that it doesn't stretch our detail region down. So I am going to save that, preview it, and that's looking a lot better.
But I still have this hover effect that is applying to my default links, but I don't really want it to apply here. So the very next thing I am going to do is go down and strip out that, and I can do that at sort of a top level, if you will. I am going to say .detail hover. I am going to use the hover pseudo class selector, and I am going to attach it to the class of detail, no space in between them obviously. Here I am going to turn the text-declaration underline, I am going to turn that off by saying none. I am going to set a background color. So remember this is the whole background of the entire region. I am going to change it to a lighter blue, #C885F0.
So here I am mixing HEX values and RGB, three values together again, and I am also going to do that with the border. So I am going to do border-color, not the entire border property, but just the border-color property. I am going to do RGB, and inside that I am going to do 36, 54, 70, so it's a much darker blue, and it's going to make the border really stand out when somebody hovers over that. All right, so I am going to go ahead and save that, go back, refresh, and now instead of getting an underline, I get that nice rollover effect of that lighter blue in the border.
And what's really nice about that again is it tells in a very subtle way that, hey, this entire region is clickable, so you've got that the whole region that's a clickable region, and I don't have anything going on with any of the text. Now that's not to say that you can't, if you wanted the text--either the heading text or the paragraph text--to change or have a rollover effect on that as well, you could certainly do that. Let me show you really quickly how to do that. Let's say for example I come in and write one more selector. I could do .detail h2:hover and then another comma, and I could say .detail p:hover.
So remember, the hover pseudo-class selector can be used on things other links. So I am going to target the h2 and the p directly, and I will just change their color as well, and this time I am going to use the #916226, so sort of that brown color. I will go ahead and save that, preview the page, there's our finished region. Not only does the whole thing light up whenever I mouse over, but if I mouse over the text, it gives even another little signal that, hey, this is all clickable by changing the color of the text as rollovers as well.
Now, you want to be careful about mixing too many things like that, because you might send mixed messages. A person might assume that this is clickable, but they might also assume that this goes to a totally separate page. So you really want to think in terms of usability and user experience of that. Really, most important thing that you want to remember if you're dealing with these block- level links, the link itself is still going to behave like an inline element unless you specify it otherwise, like we did with the block-level display. And you also need to remember to overwrite all of the global link styles to ensure that any unwanted styling doesn't appear somewhere within the link, and sometimes it's awful hard to read out those individual properties.
There are currently no FAQs about CSS: Styling Navigation.
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.