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 are going to need an image that's also a clickable link. Although most link styling considerations also apply to images as well, there are a couple of things that you are going to want to be aware of as you're styling image links. So to show off some of those things, I'm going to work on the image.htm file that you can find in the 02_05 folder. I have got that opened up currently in a browser, so you can sort of see what this page is all about. We have a larger image up top that is a clickable link, and then we have some thumbnails down here in the bottom that are also all individually clickable. Now, you can see that this browser--which is Opera-- doesn't really have any default image link styling.
So, when I make an image also a link by wrapping it an anchor element, there really isn't any default styling. The only indication I get is the little hand that shows up, the cursor changes to let me know that it's clickable. In older browsers, you would often see sort of a default blue border show up on the image which a lot of designers found undesirable. So, for the most part, modern browsers don't really have any default styling cues that an image is also a link. But if somebody is viewing this in an older browser, you might have a really obnoxious blue border around the image right now.
So, one of the first things that most designers do is they strip out any of that sort of default styling. So, I'm going to go into my code editor where I have the image file opened as well. I am going to scroll down to the bottom of my styles. I have a little indicator there for image styles. I am just going to go ahead and create a default selector here for any images that are also links. So, I am going to do a img, and it's going to say any image that's inside of an anchor element. The only property I am going to set in there right now is border: none. Now, that's going to make absolutely no difference to the page that I have opened in the browser right now.
The only thing that, that would effect is older browsers that sort of show that default blue link. But what if I really did want some way to indicate to the user that this is clickable and something maybe a little bit more robust than just a cursor changing only? In order to do that, I can take advantage of all the selectors that we've been using in previous exercises. So if I go down right below that and create the selector, that's a:hover, so anytime anchor is hovered over, and then I am going to do space img, which is going to say any image that's inside of an anchor which is being hovered.
So inside that, I'm going to go ahead and set another border, but this time I am going to be a little bit more specific about the border itself. I am going to do 4 pixels solid, and then I am going to set the color to be D5973C. Now, that's actually the same exact color that I am using for links on the page as well for hovering, and that's an important consideration. You want to make sure that you don't deviate from your site's default link styling when you start working with images. Being consistent like that is going to send a much clearer picture to the users of your page.
So, I am going to refresh this, and now as I hover over the image, I can see I get a sort of orange border around it. But there's an unintended side effect here. You'll notice that as I hover over it, everything is shifting just a little bit to the right, including the text, and this can cause real problems in layouts. Now, the reason that this is happening is that borders do add to the overall width and height of an element. So if I suddenly put a border on an element that didn't have one before, all the other content is going to have to shift to make room for it, and that can really cause layouts to break.
So, that's one of the things that you need to think about, and of course, we can sort of modify this a little bit. If I go back into my page, and I change the border: none here to 4 pixels solid and white, save that. Now, what that does for me is it's going to put sort of a default white frame around any images that are also links. And when I highlight that, I skip that nice orange color, but then I have this white border around it, that sort of gives me that nice default frame that's going to cause everything to stay exactly the same and the layouts aren't going to shift.
Now, that's a really nice effect. But if you didn't want that white frame around it, one of the things that you could do is take that border and make it the exact same color as the background. Doing that would basically cause the border to be invisible until somebody hovered over it. Now, at the very bottom of this, I have got some thumbnails down there, and right now they're just basically using the same styling that my image link has up here, and that's fine for the most part. There is nothing wrong with that at all. But there are a lot of things that people do when they style thumbnails. They make them bigger, they rotate them, they animate them, there is all sorts of stuff that you can do.
So, what I want to do is I just want to show you some really basic thumbnail styling so that you can get a feel for it, and then you can take it and expand it and create your own thumbnail styling. All right, so I am going to go back into my code, and one of the things I want to point out, if I scroll down, I can see that each one of these thumbnail images has the class of thumb applied to that, and that's going to allow me to target each one of them. I also want to point out that their width, and height is set to 100 pixels, but the images themselves are actually larger than that. So, what we are doing right now is these thumbnails are being scaled down. The original size of the image is 150x150, but we are scaling them down to 100x100.
Now, the reason I did that is because what I want to have happen is when I mouse over these thumbnails, I want them to get bigger, I want it to get larger. If you do that with an image that you're basically making larger than its original size, it's going to blur the image and really damage the image quality. So, the technique that I am using here is to shrink them down originally, a thumbnail, and then bring them back to their original size as they get larger. Okay, so I am going to go right up to my image styles and just create another selector up here. And what I am going to do is I am just going to create the class thumb selector, and that's going to help me target these thumbnail images.
So, the first thing I want to do is turn off the border that was applied via the a img selector. So I am going to go ahead and turn that border off so that it doesn't have that sort of default white frame. Then I am going to put a little bit of padding up top. I am going to do padding-top: 58 px. Now, the reason I am doing that is because as these get larger, I want them to move kind of up instead of down, and having that padding on top is going to force the thumbnails down originally so that when I remove the padding when they get bigger, they will move up instead of moving them down. Now obviously, if this were in some type of a filmstrip or in a defined region of the page, the region itself would sort of be dictating how I needed to accomplish that.
But since they're sort of off on their own, it's really easy for me to just add a little bit of padding up top. So if I save this, go back into my browser, and refresh that, you can see a couple of things happen here. You can see first, the frame goes away, and the second thing that you can see is that they were pushed down a little bit. So there is a little bit of empty space above them now to allow them to grow. Now, currently, the hover is causing a very unpleasant effect of a large border that goes all the way around, including the padding. And of course, they have to shift a little bit because they don't have a border applied to them by default. All right, so we need to control the hovering of these.
And to do that, I am going to create another selector, I am going to say a .thumb, which of course is the thumb class: hover. So I am saying, hey, when any image or any element for that matter with a class of thumb applied to it, when that's hovered over, and it's inside of a link, let's go ahead and do something else. So, you'll notice that hover can be applied to things other than anchor elements. You can apply it to really any element on the page, and almost all modern browsers will recognize that. All right, so first thing I am going to do is I am going to set padding to 4 pixels. It's going to offset the top padding of 58 pixels, and it's going to create equal padding all the way around that.
Now, the reason for that is I am also going to apply a background color to it, and I am going to do background color of #D5973C. Now, you might recognize that color from up here, it's the same color that we applied to the border. You might also notice that I'm using 4 pixels for the border and 4 pixels for the padding. So, it's going to be visually the same effect, but instead of applying a border, I am going to use padding to give me enough room so that I can see some of the background color sort of poke through, if you will. Now just below that, I am going to set the width to 150 pixels.
Now again, that's going to set it to the original or the actual size of image, and I need to follow through and do the same thing to the height, so now the width and height, they are going to be set to the actual size of the image, which is 150 rather than 100. Then finally for border, I am also going to go ahead and apply none, and that's going to turn off that orange border that you were seeing just a minute ago. So if I save this and go back into my thumbnails and refresh these, now as I hover over them, you can see they get larger. They get what looks to be an orange border around it, but we know it's not, we know it's the background color that's being displayed because of the padding around it, and it's occupying that sort of empty space that we took above it.
So instead of pushing them down, they are allowed to go up and occupy that space above it. That's not the only way to style thumbnails. And for the most part, how you style any of your linked images or control these types of thumbnails is really just a matter of personal preference. Just remember which properties you can control, and if you use any type of a reset in your CSS, you are going to want to remove any default borders for linked images that might exist in some of those older browsers.
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.