CSS Buttons Tutorial / Example

This is a Web page to demonstrate CSS Buttons. Further down you can view an example.

If new to CSS you might wanna do some reading first. This assignment from the Internet programming 3 course will get you started: CSS2 - Design your Website with style

What is a CSS Button?

CSS buttons are buttons created with HTML and CSS code.

Why use CSS Buttons?

While Image Buttons doesn't add that much size to a Web page, the bandwidth requirements do add up, resulting in a slower page load.

Faster Web page load

To be sure Web pages you create load as fast as possible, CSS buttons can be used as a strong alternative to Image Buttons since they usually don't add as much to the total file size. Smaller file size means faster page load which of course is desirable for most sane Web developers.

There are, however, other benefits as well. For example - if coded correctly - resizable button text for Web site visitors, and easier site maintenance for Web developers.

Resizable button text for visitors

If the CSS code for a Web page define the em or percent unit for the font-size value, the text will be resizable even for users that use Internet Explorer version 6(and below) as their Web browser.

Unlike Image Buttons CSS Buttons will - along with the rest of the text - be resizable since the buttons are made out of pure text.

Of course the width and hight of Image Buttons can use the em or percent units to make them resizable, but it's more difficult to get the same look for the buttons in different Web browsers, and - since the file size of the buttons are/should be reduced as much as possible - the quality of the Image Buttons will decrease in an ugly way when they are enlarged.

Easier site maintenance

As for easier site maintenance you might, for example, wan't to change the visual size of a CSS button you use site-wide. This could easily be done by changing the width and height values for the CSS button, or have a larger value for the font-size of the button text to make the text stretch out the button's width and height automatically.

Or you might wanna change the background and/or the button text color of the button. This too is easily done by changing the values of the background or color property in the Style Sheet.

CSS Buttons Example - Previous and Next Buttons

Below is an example of how Previous and Next buttons can look like when created with CSS. Note that, if you change the text size of this Web page, they too will change their size.

To change the text size you can, in most browsers, press down the CTRL button while using the scroll wheel on your mouse, or - if you use Internet Explorer or Firefox - change the text size under the View menu(View -> Text size).

«  Previous Next  »




HTML Code for the Example above:

<span class="button"> <a title="Example Button" href="prev-link-here.htm"> <span class="arrows"> &#171;&nbsp; </span> Previous </a> </span> <span class="button"> <a title="Example Button" href="next-link-here.htm"> Next <span class="arrows"> &nbsp;&#187; </span> </a> </span>

CSS Code for the Example above

This is the CSS code(Style Sheet) I use in the external CSS file linked to in the head section of this Web page.

/*Button Style*/ .button { float:left; height:auto; font:76%/150% "Lucida Grande", Geneva, Verdana, Arial, Helvetica, sans-serif; width:10em; text-align:center; white-space:nowrap; } /*Button Arrow Styles*/ .arrows { font-size:90%; margin:0.2em; } /*Button link styles*/ .button a:link, .button a:visited { color: #eee; background-color:#1e5184; font-size:1em; font-weight:bolder; text-decoration: none; border-bottom:0.1em solid #555; border-right:0.1em solid #555; border-top:0.1em solid #ccc; border-left:0.1em solid #ccc; margin: 0.2em; padding:0.2em; display:block; } .button a:hover { background-color:#003366; color:#999; border-top:0.1em solid #777; border-left:0.1em solid #777; border-bottom:0.1em solid #aaa; border-right:0.1em solid #aaa; padding:0.2em; margin: 0.2em; }

I link to the CSS file in the head section of this HTML page like this:

<link media="all" href="css-buttons-style.css" type="text/css" rel="stylesheet" />