Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Creating CSS-only buttons

From: CSS: Styling Navigation

Video: Creating CSS-only buttons

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.

Creating CSS-only buttons

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.

That's looking a lot better, at least the default style of the button looks consistent. I am overriding the other default styling so that's looking pretty good. One last thing I do want to do here at the very, very bottom of this, I am going to go ahead and type in cursor, and I am going to make the cursor a pointer. Now, remember, this is a class of a button, and it can be applied to anything you want. So a submit button by default and a link by default change the cursor to a pointer, but what if you wanted to put it on something like a table cell or a table row, for example, and you wanted that to be clickable, maybe you're going to attach some JavaScript or something like that to it? By applying a cursor value of pointer, it means that regardless of what element you apply this to, the pointer symbol is going to show up indicating that this button is indeed clickable.

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.

Show transcript

This video is part of

Image for CSS: Styling Navigation
CSS: Styling Navigation

53 video lessons · 17012 viewers

James Williamson
Author

 
Expand all | Collapse all
  1. 3m 8s
    1. Welcome
      42s
    2. What you should know before watching this course
      1m 12s
    3. Using the exercise files
      1m 14s
  2. 35m 25s
    1. Organizing menus with lists
      4m 26s
    2. Ensuring accessibility
      9m 3s
    3. Using the nav element
      7m 30s
    4. Creating block-level links
      3m 8s
    5. Lab: Structuring navigation
      4m 11s
    6. Solution: Structuring navigation
      7m 7s
  3. 48m 42s
    1. Exploring link style considerations
      9m 2s
    2. Using global link styles
      9m 56s
    3. Styling link states
      10m 57s
    4. Indicating external links
      10m 4s
    5. Styling image links
      8m 43s
  4. 52m 5s
    1. Stripping default list styling
      4m 34s
    2. Defining link dimensions
      6m 0s
    3. Setting link styling
      3m 36s
    4. Aligning links vertically
      4m 11s
    5. Controlling link spacing
      2m 30s
    6. Styling menus with borders
      2m 32s
    7. Creating rollovers
      4m 45s
    8. Restricting link styling
      3m 31s
    9. Lab: Creating a vertical menu
      11m 44s
    10. Solution: Creating a vertical menu
      8m 42s
  5. 54m 58s
    1. Stripping list styling
      3m 35s
    2. Displaying links horizontally
      6m 14s
    3. Clearing floats
      6m 12s
    4. Controlling link sizing and spacing
      3m 11s
    5. Styling links
      7m 16s
    6. Creating rollovers
      5m 52s
    7. Indicating current pages
      4m 43s
    8. Controlling cursor states
      2m 46s
    9. Lab: Creating horizontal menus
      6m 45s
    10. Solution: Creating horizontal menus
      8m 24s
  6. 55m 35s
    1. Overview of dropdown menus
      1m 17s
    2. Structuring submenus
      5m 56s
    3. Styling submenus
      6m 4s
    4. Creating submenu rollovers
      3m 28s
    5. Positioning submenus
      5m 43s
    6. Controlling submenu display
      5m 5s
    7. Creating persistent hover states
      5m 53s
    8. Animating menus with CSS transitions
      6m 29s
    9. Lab: Dropdown menus
      6m 51s
    10. Solution: Dropdown menus
      8m 49s
  7. 58m 7s
    1. Creating CSS-only buttons
      8m 39s
    2. Creating special effects for buttons
      4m 2s
    3. Enhancing buttons with gradients
      7m 40s
    4. Overview of CSS sprites
      3m 30s
    5. Using CSS sprites for icons
      14m 30s
    6. Styling block-level links
      8m 38s
    7. Lab: Enhancing navigation with CSS
      5m 26s
    8. Solution: Enhancing navigation with CSS
      5m 42s
  8. 6m 29s
    1. Additional resources
      6m 29s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now "Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed CSS: Styling Navigation.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.