Start learning with our library of video tutorials taught by experts. Get started
Viewers: in countries Watching now:
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.
Occasionally, you're going to want to indicate when a link is going to redirect a user to an external site, or even download an external resource. Now, the easiest way to do this is to use attribute selectors to the filter out link types. Now, while we're focusing on a specific use case in this particular exercise, I really want you to think about all the different ways that you can use the techniques that I'm going to show you in this exercise to enhance your navigation. There may be you know some other ways that are unexpected. All right, so I have the external.htm page open from the 02_04 folder.
I just want to show you this in the browser, so I can show you what I'm talking about here. We're indicating external links. We're going to show the user that we when they're going to click on something, it's going to take them outside the site or download a file like Word document or PDF or something of that nature. So I have got four links currently built into this. I've got two of them that are going to take you off the site, so mocked to one that shows off the font that I'm going to be using Modern Pictograms, and the other one that takes you to Font Squirrel where you can find that font. Take a look at these character set.
Then I have an internal link that takes you to a link within the same site, and then I have to an external resource that downloads a Word document. So I really want to distinguish between these different types of links when I'm styling them. Now, the easiest way to do that is to use attribute selectors. So if you've never used attribute selectors before, these are awesome. I'm just going to go right up into this style tag in my code ,and below all of my active selectors, I'm going to go ahead and create a new one. So what I'm going to do is I'm going to go ahead and do an a selector, and then an attribute selector just follows whatever element you're targeting with brackets, so those sort of square brackets.
And I'm just going to type in href and then close my bracket and then go ahead and open up a curly brace, hit Enter twice, and close the curly brace just so I've got my initial selector here, and then I'm just going to change color to red. Okay, so let's talk about the syntax of this. Basically, those brackets right there are saying, hey, I'm targeting an attribute when it's applied to this element. So it's going to say go down to find any anchor element that has an href attribute. So if I save this and go back to my page and refresh that, you can see that it's targeting every single link.
It's targeting every single link because they all have an href attribute. Well, fortunately, we can actually filter that to a greater degree by looking for specific values for attributes. So not only can we look for the href attribute, we can modify this a little bit. I'm going to go ahead and do a caret, which is that upward pointing arrow that you can get by holding down a Shift and 6, and then directly after that I'm going to type in equals. You'll notice that there are no spaces there. It's just that caret-equals. Now what this does is it's saying, okay, take a look at the value of href, and that's the equals sign, but the caret says look for something that starts with.
So it's not looking for the entire value of the href attribute, it's only looking for when it starts with a certain set of characters. Now the next thing you need to do is tell it which characters to look for. So I'm going to open up a quotation mark, and then I'm going to type in http:// and then close my quotation marks. Now, did I really need the forward-slash, forward-slash? No, probably not. I could've probably gotten away with http, but I'm just being thorough. So, essentially what I'm saying now is I'm saying, hey, go out and find any link that has an a href attribute. Find the href attribute that begins with http://.
So now if I save this, go back in, and refresh my page, you can see that now it's only targeting those two links, and that's because they're going to external sites. One is going to a page that details their font Modern Pictograms, and the other one is going to Font Squirrel which is one of my favorite font resources. Well, that's pretty cool, but what we're wanting to do is indicate external links in maybe a more obvious way. Chances are your users aren't going to know that red text indicates an external link unless you tell them somewhere. So we want to really demonstrate that visually, and one of the ways to do that is to use some type of an icon.
Now there are a lot of different ways to do this technique. In lot of tutorials out there, you might see people use background images or even, you know, real images right next to them. Those are good techniques, but the problem that I have with them is they're not really scalable. We live in a time now where fixed size websites really aren't that powerful anymore because people might be viewing it on a phone or different screen size. The user might want to increase the size of the text or decrease the size of the text. And when you start putting fixed size images or icons in your pages like that, they don't scale quite as well.
So one of the things that I really like is the development of what's known as icon fonts. And I'm just going to switch over here to Font Squirrel and show you the font that we're going to be using, which is Modern Pictograms. Now this is by John Caserta, and I hope I'm pronouncing his name right. But he created this one, and it's available, it has an open-source license out here on Font Squirrel. And you can go download this and use it in your own projects. And you can see it has a series of icons here that are mapped to certain characters. So if you want this particular up arrow, for example, you just use that sort of left bracket.
It's really cool, and it's extremely easy to do. The problem is, obviously, I don't want to go through my site and find all of my external links--and by the way, the symbol we're going to be using is right here for the O. This is a pretty common symbol for external links, and that's the one I want to use. Well, I don't want to have to go through all my code and put an O next to all of that. So I'm going to combine the technique of using attribute selector with what's known as generated content. So I'm going to go back into my selector here, and I'm going to modify this selector a little bit, and right after the attribute selector, I'm going to type in :after.
Now what this does is this is generated content that basically says after the content in this selector--which in this case is the anchor element--I want you to put some other content for me, I want you to put something in there. So I'm going to go ahead and modify this a little bit. I'm going to take the color red and get rid of that, and I am going to type in content and then in quotation marks, space and then a lower case O, and that's because that's the character I want to use. And this space that I'm using here is going to give me a little bit of separation between the link and the icon that's going to show up. Now how are we going to use that font? Well, if I go further up in my code, I can see that I'm using it @font-face declaration.
This going to use that modernpics-webfont, and I've actually included the fonts right over here in this fonts directory, so it's pointing to those. So all I need to do in order to use that, now if I scroll down is now I'm going to say font-family, and I'm going to use the definition that I created up top. I'm just going to type in Modern, sans-serif, and below that I can start to modify this a little bit. I'm going to say, okay, I want its font-size to 1.1 ems.
And then I want to move it a little bit, so I'm going to set a position of relative on it, and then I'm going to do a top value of -0.2 ems. Now let me go over each of that line by line to tell you what that's going to do for us. So this content line is going to place that O character to space O after the contents of the link. The font-family is saying I want you to style that using the Modern font, which is going use icon font. The font-size is going to make a little bit larger than the text it's in, and that's okay because it's a really small icon.
And then this position: relative, top: -0.2 ems is going to move it up a little bit so that sort of superscripts that icon. So if I save this, go back at to the browser, and refresh that, you can see now the red text is replaced with that icon, sort of in that upper right-hand corner. Now one of the defects obviously, of this being a icon font and all the styling applying to it, you can see sort of that hover effect is now applying to everything. So in this case, I may want to get rid of that underline so that you don't see it there. So I can go back to my hover, get rid of that text decoration.
These are some of the trade-offs that you need to make when you're thinking that using these types of icon fonts. So you want to really plan thoroughly before you start styling these, you really want to think about them. Now, I mentioned that I have an external resource here. This technique is extremely flexible, and it can work for several different things. If I go back in to my code here and instead of typing all this again, I'm just going to copy this and paste it, and I'm going to change the attribute selector. I want to change the attribute selector from a caret equals to a dollar sign equals. So the caret equals says anything that begins with the string, the dollar sign equals says anything that ends with the string.
Now, since the link to an external document is linking to a Word document, it's pretty easy for me to say, hey, if the href ends with .doc, which would be a Word doc, I want to do something different. In this case, instead of a lowercase O, I'm going to do space and then uppercase K. I don't really need to change this setting because of the formatting, I want to keep that consistent. So, I'm just going to save that, go back out to the browser, and Refresh that. And now you'll notice that I get the little document icon there in addition to the external site document. This is a fairly complex technique.
If you're new to CSS, I could see this being a little bit overwhelming. So just slow it down and go through step by step and really come to understand what each of these things is doing for you. You know, it's important for me that you're not just sort of take away this technique, it's important for you to understand that this is not the only way to target these kind of links or even to display these icons. However, it is one of my favorite techniques, and it illustrates two things that are very important. First, attribute selectors, generated content, and icon fonts. Those three things are very powerful.
And when you combine these techniques, it can be extremely powerful. And second, you really need to think about things like accessibility, screen sizes and how flexible certain techniques are before you use them. One of the reasons why I went so complex with this technique and used icon fonts is if somebody comes in and ups the size of the text, notice that the icons scale right along with that. If I had created, say, a GIF file and used that as a background image, that wouldn't be the case. And somebody increasing and decreasing the size of the text, or even displaying it on a smaller screen like a mobile phone, there would be a really big disconnect between the text and the icon itself.
So those are some of the things you really need to think about. Often it's really just choosing the technique that's the best overall fit for the situation, rather than simply using a single technique for every situation.
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.