CSS Playground

only cool sites

Subscribe to RSS feed

How to make sexy buttons with CSS

by Alex Griffioen

This tutorial will teach you how to create pretty looking textual buttons (with alternate pressed state) using CSS. Dynamic buttons save you heaps of time otherwise spent creating graphics and will basically make you a happier person at the end of the day. Here's what you'll get:

Bring world peace Launch nukes
<script type="text/javascript"> digg_url = 'http://www.oscaralexander.com/tutorials/how-to-make-sexy-buttons-with-css.html'; digg_skin = 'compact'; digg_title = 'How to make sexy buttons with CSS'; digg_topic = 'design'; </script> <script type="text/javascript" src="http://digg.com/tools/diggthis.js" /><iframe width="120" scrolling="no" height="18" frameborder="0" src="http://digg.com/tools/diggthis.php?u=http%3A//www.oscaralexander.com/tutorials/how-to-make-sexy-buttons-with-css.html&amp;t=How%20to%20make%20sexy%20buttons%20with%20CSS&amp;c=design&amp;s=compact" />

Sliding doors

Since we want our buttons to be über-flexible, we'll have to make the background image expand with the size of the button's text. For that, we'll use the beloved sliding doors technique; two complementing images creating the illusion of a single, stretching image.

Our button will be a basic tag with a nested , each containing a different slice of the background image. Here's what the HTML looks like:

Bring world peace

Nothing out of the ordinary, right? Next, we need to come up with a crisp design for our button in both normal and pressed state. Here's what I'm thinking:

Normal
Pressed
Button design

We will include both states in a single image. To switch between normal and pressed state, we'll simply shift the background image vertically, revealing the alternate button state. This CSS based approach allows us to perform the switch without the need for any JavaScript trickery. Let's merge these two and apply the sliding doors cut. The part of the image that will accomodate the button text will be set to a reasonable 300px. The height we'll set to 24px.

DIV
A

Styling the button

Finally, we need a fair bit of CSS to bring all this together. Since we might want to line up several buttons horizontally, I decided to float them by default. (Floating them also makes the buttons wrap nicely.)

.clear { /* generic container (i.e. div) for floating buttons */
overflow: hidden;
width: 100%;
}

a.button {
background: transparent url('bg_button_a.gif') no-repeat scroll top right;
color: #444;
display: block;
float: left;
font: normal 12px arial, sans-serif;
height: 24px;
margin-right: 6px;
padding-right: 18px; /* sliding doors padding */
text-decoration: none;
}

a.button span {
background: transparent url('bg_button_div.gif') no-repeat;
display: block;
line-height: 14px;
padding: 5px 0 5px 18px;
}

We now have a pretty looking button, but it doesn't exactly give us much visual feedback when we press it. So let's add that finishing touch of CSS:

a.button:active {
background-position: bottom right;
color: #000;
outline: none; /* hide dotted outline in Firefox */
}

a.button:active span {
background-position: bottom left;
padding: 6px 0 4px 18px; /* push text down 1px */
}

All done! Go ahead. Click 'em!

One extra measure for IE

Although hardly catastrophic, IE does not return the button to its normal state whenever it's released. To force such behaviour, we need to add a tiny bit of JavaScript to the tag:

To submit forms with this type of button, simply add more calls to the button's onclick event.


Veerle的CSS hover effect教程

,

CSS hover effect

18 comments 2007 at 01.07 pm posted by Veerle

I would like to share some insight on a piece of CSS I’ve used for the homepage for a website a while ago that I’ve built together with Roger Johansson. I’m talking about the hover effect on the 4 tabbed boxes shown on this page.

The HTML markup

Let's take a look first at the HTML markup for this. First we need to think about the structure of this piece of content. How should it be structured with CSS disabled and still make sense for the user. I think a bullet list makes sense. We sum up the 4 different ways to find your Kansas City Homes for Sale. This is the (X)HTML markup:

Prudential Kansas City homepage - CSS hover effect

  1. <h2>4 different ways to find your Kansas City Homes for Sale</h2>
  2. <ul id="differences">
  3. <li id="diff-1"><h3><em>1</em><a href="benefits.html">See all MLS homes like an insider—join us</a></h3><div><p><a href="benefits.html">Join us </a> and see <strong>2,207</strong> additional homes, e-mail updates, and free custom website.</p></div></li>
  4. <li id="diff-2"><h3><em>2</em>What is my house worth?</h3><div><p>Find the <a href="/" title="empty">value of your home</a> from any of our agents with our competitive market analysis.</p></div></li>
  5. <li id="diff-3"><h3><em>3</em>Google&trade; search Kansas City area homes in MLS</h3><div><p><a href="/" title="empty">Search virtually all MLS homes</a> in the greater Kansas City area by using Google&trade;.</p></div></li>
  6. <li id="diff-4"><h3><em>4</em>Map Kansas City real estate in Google&trade; maps </h3><div><p> <a href="cma.html" title="empty">See the location</a> of all Kansas City real estate with Google&trade; maps—map or satellite views.</p></div></li>
  7. </ul>

If you look ate the page with CSS disabled, I think this markup is well structured and it's perfect to build the necessary CSS to achieve the style I have in mind.

The background images

If you look at the final page and the effect I want to achieve with the tabs, you know that I need to create background images for this that are able to grow if the user enlarges the text. Here is how they look:

You'll notice that both the hover background and the default background are saved as 1 gif file. This way the hover effect will go smooth without interuption. The entire image is loaded and the browser doesn't have to load the hover images on the moment the user hovers the box. To be sure the box can grow we save the bottom part of the box and the top part of the box, making it long enough to be sure there will be no gaps (till a certain enlargement).

The CSS styles

The unordered list "differences"

This is the CSS for the list starting with the unordered list of the 4 boxes. To prevent the margins to collapse I've added float:left.

  1. #differences {
  2. margin:0.5em 0 1.5em 0;
  3. padding:0;
  4. list-style:none;
  5. width:100%;
  6. float:left;
  7. }

The li element styles look like this:

  1. #differences li {
  2. margin:0 18px 0 0;
  3. padding:0;
  4. float:left;
  5. background:url(../images/diff-tabs-top-wide.gif) no-repeat 0 0;
  6. }

I add a right-margin of 18px to create a gutter between each box. The last box in the row doesn't need this margin. Each li element has an id. The id for the last one is #diff-4. So I add this CSS style:

  1. #differences li#diff-4 {
  2. margin-right:0;
  3. }
Making sure the tabbed boxes can grow

As you've noticed, we've linked the top part of the background image (the tab shape) to the li element via CSS. To make sure the tabbed boxes can grow if the user enlarges his text, we need to add an extra div in each li element. This way we can link the bottom part of the image to this div via CSS:

  1. #differences div {
  2. font-size:0.94em;
  3. padding:6px;
  4. background:url(../images/diff-tabs-bottom-wide.gif) no-repeat 0 100%;
  5. }

With CSS3 we don't have to go through this hassle of adding extra divs, because CSS3 allows for multiple background images on one element. This is my top favorite CSS3 feature smile

Creating the hover effect

I've used background positioning to create the hover effect on the li element using the :hover pseudo-class. The CSS looks like this:

  1. #differences li:hover {
  2. background-position:-156px 0;
  3. }
  4. #differences li:hover div {
  5. background-position:-156px 100%;
  6. }

The x coordinate is set to -156px. This way the pink orange part of the background image will be shown. The first style represents the top part of the tabbed box and the 2nd one the bottom part. Remember I had to add an extra div to make sure the box can grow (till a certain enlargement).

The page uses a dynamic resolution dependent layout

Not sure if you've noticed or not, but if you resize your browser window to less then 750 pixels wide, the page will adjust its layout. This is done using a javascript created by Kevin Hale of Particle Tree in combination with CSS. I'm using an extra CSS stylesheet that is applied if the browser window is resized to less then 750 pixels. By default the page/site uses the wider version, which is aimed to a screen resolution of 1024 x 768. About 90% of the visitors of the Prudential Kansas City visitors are using a screen resolution of at least 1024 x 768 pixels.

Hopefully some of you have learned from this article and are inspired to use CSS in a creative way without overlooking the accessibility part of it and keeping the right structure in mind for the (X)HTML. I'm still creating new templates for this website on an occasional basis. This website is rather big and new features are constantly added by the client and the developer.

Oh and let's not forget to mention that it is always great to work together with Roger. He's picky in what he does, but that's what I love about working with him, and of course I always challenge him with my designs smile

百人街头整蛊陌生路人

CSS-Based Forms: Modern Solutions

必须访问的60个CSS大师级de站点

With new technologies flooding the web,different tools are available with which a web designer can showcase his creativity and craft. But despite putting in the effort and sweat, numerous websites with fantastic creativity fail to get noticed. Also, the surge in designing is driving designers to dig for creativity, color schemes and layouts.

Keeping these aspects in mind, we came up with a compendium of best CSS directories, CSS showcase, CSS galleries that rank / grade websites (using CSS) on the basis of ‘alexa ranking’, colors, categories, tags, ratings and RSS.

If you feel that a particular site is worth to get showcased, then drop the URL’s in your comments so that they become a part of our next list.

If you found this article useful, digg it to ensure that a designer’s effort doesn’t go unnoticed.

Site
Alexa Rank
Colors
Categories
Tags
Ratings
RSS
www.cssbeauty.com 10,432 No Yes Yes No Url
www.cssdrive.com 14,303 No Yes Yes Yes Thumbnail
www.stylegala.com 15,819 No No No Yes Thumbnail
www.cssmania.com 22,686 Yes Yes No Yes Thumbnail
www.cssremix.com 23,294 No No No Yes Full Image
www.alvit.de/css-showcase/ 26,557 No Yes No No Url
www.cssvault.com 27,285 No No No Yes Thumbnail
www.w3csites.com 28,078 No No No No Url
www.bestwebgallery.com 30,382 No Yes No No Thumbnail
www.csselite.com 33,948 No Yes No No Thumbnail
www.screenalicious.com 36,114 Yes No No Yes Thumbnail
www.unmatchedstyle.com 42,269 No Yes Yes Yes Full Image
www.designlinkdatabase.net 48,086 Yes Yes Yes Yes Thumbnail
www.screenfluent.com 51,014 No No No No Thumbnail
www.designsnack.com 51,904 No Yes No Yes Url
www.cssheaven.com 53,491 No Yes No Yes Thumbnail
www.cssimport.com 57,960 No No No Yes Thumbnail
www.cssglobe.com 65,660 No No No No Url
www.cssreboot.com 67,768 No No Yes Yes Url
www.mostinspired.com 68,025 No No No No Thumbnail
www.thebestdesigns.com 68,614 No Yes Yes No No
http://thesis.veracon.net 76,844 No No No No Thumbnail
www.cssbloom.com 79,672 Yes Yes Yes Yes Thumbnail
www.csscollection.com 92,579
www.csstux.com 98,129 No No No No No
www.cssbased.com 101,834 No Yes Yes Yes Url
www.css-website.com 102,923 Yes Yes Yes Yes Thumbnail
www.designshack.co.uk 109,656 No No No No Thumbnail
www.ceeses.com 124,001 No No No Yes Url
http://anjo.dekiteharu.jp 135,146 No Yes No No No
www.cssclip.com 135,730 Yes Yes Yes Yes Url
www.my3w.org 151,235 No Yes Yes Yes Thumbnail
www.csshazard.com 167,525 No Yes Yes No Full Image
www.artnetz.de 173,234 No Yes Yes No Thumbnail
www.css-design-yorkshire.com 177,077 No No No No No
www.css11.com 182,520 No No Yes Yes No
www.cssimpress.com 188,219 No Yes Yes Yes Thumbnail
www.e-motionaldesign.com 202,826 No Yes No Yes Thumbnail
www.cssprincess.com 210,811 No Yes Yes Yes No
www.cssgaleri.com 220,224 No Yes No No Thumbnail
www.cssblast.ru 224,048 No No No No Url
www.creative-pakistan.com 233,247 No Yes No Yes Thumbnail
www.netzfruehling.de 237,645 No No No No No
www.najdizajn.com 256,885 No Yes No Yes Thumbnail
www.edustyle.net 274,750 No No No Yes No
www.csssmoothoperator.com 290,827 No No No No Thumbnail
www.coolsitecollection.com 313,655 No Yes No No Thumbnail
www.cssgalaxy.com 348,352 No Yes No No Thumbnail
www.per.fectio.net 357,030 No No No Yes No
www.cssflavor.com 364,755 No No No No Thumbnail
www.onepixelarmy.com 369,312 No Yes No No Url
www.piepmatzel.de 418,803 No No No No Url
www.cssbrain.hu 438,074 No Yes No No Thumbnail
www.w3c-compliance.com 461,674 No No No No No
www.stylegrind.com 484,645 No No No No Url
www.submitcss.com 523,147
http://inspirace.dobrestranky.com 549,529 No Yes Yes Yes Full Image
www.cssgallery.ro 553,463 No Yes No Yes Url
www.cssgreen.com 600,815 No Yes No No No
Source

CSS Formatter and Optimiser





Hey~very glad to find two new messages (both from rove)and a new top banner navigation of the Opera blog is cool! Pages loads faster than ever before, too.

I'm doing my c3 project now and hopefully this site is going to be official launched on/before 1st May.

it gonna be an eye-catching one, i promise.

设计师一生当中必须掌握的53个CSS技巧

今天在Smashing Magazine上看到这个唬人的标题:没有这53个CSS技巧你没法活。必须去看看,自己掌握了几个。

Veerle 的CSS教程:图形化横向导航二级菜单设计

the internet is a big container

Read more...

20+ CSS Menus

50 Beautiful CSS-Based Web-Designs in 2006

February 2012
S M T W T F S
January 2012March 2012
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29