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.
In our last chapter we're going to focus on ways that we can enhance our navigation through multiple CSS techniques. With the recent focus on designing for multiple screens and devices, it's becoming increasingly important for designers to look for ways that CSS can make their sites more lightweight and flexible. Now, thankfully some of the more recent CSS capabilities greatly expand our ability to create artwork with just CSS. Take this button, for example. Now, in the past we'd have to rely on background images and lots of non-semantic markup to create it.
Well, now we can create it through pure CSS. So over the next few exercises we're going to create this button and explore some of CSS' newest features. So I've opened up the buttons.htm file from the 06_01 folder, and I wanted to show you the two elements on the page that we're going to be styling. We have an input submit, which is just the submit button you see on forms, and they typically are going to display a browser's default styling. And then we also have just a regular link on the page, for whatever reason, maybe it's a call to action link.
What I have done in both of these cases is I have taken these very, very different elements, both the link and the submit button, and I've given it the class of button. One of the real values in working this way is you can sort of create these button styles generically as classes and then just add them to whatever element on the page that needs that particular button formatting, in this case we're using both the submit button and a link. So I am going to scroll up, and you can see that we have some selectors waiting on it, so I am trying to save us a little bit of time here. We have the first class selector button, then we have a hover and an active and a focus.
So obviously, because this is a button, we're going to want to style all of the states of the button as well. So let's just go ahead and dive into the formatting. So I am going to go into the button selector. The first thing I am going to do is set a background for it. This button is going to be blue, but if you'd like to use a different color scheme, feel free. I am going to use rgb(2,99,174). Now, the next thing I want to do is color. So I am going to set the foreground color of the buttons to white, so we're going to have white text. I do like using the keywords a lot if I am using white or black just because they're so easy to read and very easy to update and maintain and they just sort of stand out.
Next thing I am going to do is I am going to do some padding. This is going to give me a little extra space all the way around the button, and really all I am doing for padding is right and left, so I am going to do 0 for top and bottom and then 1 em for right and left, so I am just going to use some shorthand notation there. You're also going to notice that as I create this button, I am going to be using relative units of measurement. The reason I'm doing that is because I don't want to lock my buttons into a fixed pixel size. That way if somebody that's reading the page increases the size of the text, the button itself will increase as well, or decrease based on how good somebody's eyes are.
I do want to set a specific height here though. So I am going to do a height of 2.4 ems, and just below that I am going to set the display of this to table. Now, why am I doing that? Well, the reason I am doing this is because all these buttons are going to be block-level elements. And what I mean by block-level elements is in order to style certain aspects of them, such as the box model properties, they need to display as a block-level element. But links by default are inline level elements.
So the problem with going ahead and just changing the display to block, which would be probably the easiest thing to do to get that to work is the fact that block-level elements by default stretch to fit the width of their container element. So they would stretch all the way out to the edge of the page if I wasn't controlling this. Now, you can control that by setting an explicit width on them. But the problem with that is I really want the text of the buttons to control how wide it is. I don't want all of my buttons to be exactly the same width. So I need to find a display method that's going to allow me to set all of these different box model properties, like padding and margins and height and things like that, but it's also going to shrink wrap to the content, because block wouldn't do it.
I really have two choices, I have inline-block, and I have table. And I like table because it's a little bit more stable, and inline-block has some support issues in older versions of Internet Explorer. That's not to say table doesn't, it does, but I like the way table works, because it is block-level, it's not an inline level element that you can also apply box model properties to, and there is a distinction there. So this is going to cause it to stay on its own line, it's going to behave just like a block-level element, but it's also going to shrink wrap to the width. The one other choice you might have there is floating.
You can certainly float the button and then clear everything after it, but to me that causes too much of a headache for me to follow that along, so I just keep the display as table. The next thing I am going to do is do a border, a border property of 1 pixel, solid all the way around. We need to do another blue, but I am going to do a blue that's a little bit more of a medium blue, so 32, 124, 202. You will also notice that sometimes I put white space between the value and sometimes I don't, it really doesn't matter. All it means is that I'm a fairly sloppy coder. So I am going to save this, go out to my browser, and refresh the page.
You can see there's a big difference in the text, and that really has everything to do with some of the default formatting for buttons, which you are seeing in the Form button above it, and then the Call to action link is taking its styling hints from the global styles above it. So we need to make those match. So really quickly, let's go down and do a little bit of font formatting here. Below border I am going to type in font-family, so font-family. And I am going to do Helvetica, sans-serif. Now, if you want to use a different font, go right ahead.
I like Helvetica a lot in buttons because you can make the text weight really, really light, and that's what we're going to do next. We're going to take the font-weight, and I am going to take the font-weight to 100, which is the lightest possible rendering for that particular font-weight. Now, if you have a condensed font or a light version of font, you can certainly use that. Helvetica actually tends to work a little bit better than Arial when you use lighter font-weight. So below that type in font-size, and I am going to make the font-size 1 em, remember the height is 2.4 ems, so font-size of 1 em is going to be a little bit smaller than that.
I am also going to do text-transform, and I am going to put the text-transform to lowercase, so this is going to make all the text in lowercase, regardless of what it starts out as. That's just a personal preference for my button. You can change it or not put it in there, depending upon the way you want to do it. I am also going to do some letter-spacing. I am going to do letter-spacing of .045 ems. Now, I'd love to tell you that I calculated that, but I'll be honest with you, I just did that through trial and error. That's typically what I always do when I am doing things. I just look at them visually and kind of change those values to get what I'm looking for out of it.
So if you don't like that value, certainly change it, it's not setting anything in stone. Next thing I want to do is I want to vertically center the text inside my button. I am going to set the line-height to 2.4 ems. I am also going to set the text-align to center. In just two lines I have centered it vertically and horizontally inside the button. For text-decoration, I am going to turn that off. Any default underlines or anything like that, I am just going to type in none, so they'll go away. And then one more thing, I am going to set white-space to nowrap.
You may have noticed earlier that I set the line height to 2.4 ems, which is the same as the height. Well, that's going to vertically center the text as long as it's a single line of text. Once the text breaks, it's going to sort of destroy that illusion. So I am putting a white-space of nowrap on there to not allow it to break. The key to this obviously is to not have a button that has paragraphs worth of text in it. Most buttons are going to be two words, three, four words at the most, so that's not a big deal. So I am going to save this, go back here to my browser, and refresh that.
So that is the basic formatting of our button. When you're creating your own button, you can certainly style them any way that you want. Now that we've got the basics of this out of the way, we're going to take a look at enhancing our buttons through the use of some special CSS effects, and we're going to do that in the next exercise.
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.