Join James Williamson for an in-depth discussion in this video Creating block-level links, part of CSS: Styling Navigation.
- View Offline
One of the really cool changes in HTML5 that often gets overlooked is the ability to create what we call block-level links. This means that instead of having to use separate links for multiple elements that all link to the same page, we can now wrap all of those elements in one single anchor element. Now to show you guys that, I have the block-level.htm file open here, and if I look at this in a browser, you can see it's a little Products page that has one single product on it, a Ultra Glazed Blue Vase.
It has the title of the product, it has an image below that, and then one line of text and a price. Now what if we had a more detailed product detail page that you wanted people to be able to go to if they wanted more information about this particular item? It would be nice if all of those elements, the heading, the image, the price, if they were all clickable so that you could just click on any of them, and it would take you to the product detail page, that would be kind of nice. Well, again, using block-level links, we can do that. Now in the past, if we wanted that type of functionality, we would have to wrap every single one of those elements in an anchor tag.
So for example, I would have to come up to the heading 2, go ahead and wrap that in an anchor element, going to the Detail page, and then maybe I'll just give it a title, and I'll title it "our blue vase", there we go. So you know, I save this, and I will preview that again. You can see that now we have a link here that takes us to the Detail page, which is kind of nice. But if I wanted every single element within the summary to be clickable, I now have to go in and do that three more times. I have to wrap every single one of those elements.
That means if the link changes, I have to update all of them. If, you know, I have 100 products, you can imagine how tedious that's going to be. So what's really nice, again, about the sort of block-level link capability in HTML5 is I can simply take this closing anchor tag, and I can now wrap all of those elements. If I save this, go back into my page and preview this, now you'll notice that every single one of these guys is clickable, and it takes into exactly the same page. Now one word of warning when using this technique, older versions of Firefox had a slight bug when you used it with HTML5 elements being inside the block-level link, so you want to be careful about that.
It is HTML5 only, but I do want to point out that really this is HTML5 supporting a behavior that browsers have long supported. So even if you're not using the HTML5, and you want to do this, there's a good chance that it'll still work just fine, no problems out there. Most browsers support this and have supported it in HTML for quite some time. Now the biggest problem is what you're seeing right here on the screen. Once you trigger the link, all of the linked elements share in the link state because it's just one gigantic link.
So you'll notice that right now they all have the focus because they were the link that I clicked on. If I click and hold the mouse down, notice that they all become the active link. If I hover over them, the hover state applies to all of them. As you can imagine, this can present some really interesting styling challenges for styling block-level links. It's so interesting. In fact, we were actually going to take a closer look at styling block-level links a little later on in the course.
- Organizing menus with lists
- Creating block-level links
- Styling links, link states, and image links
- Defining link dimensions
- Controlling link spacing in a menu
- Creating rollovers
- Clearing floats
- Indicating current pages
- Controlling cursor states
- Building dropdown menus
- Creating CSS-only buttons