Skip to main content

Affordance is when the appearance of an object describes it's purpose. Buttons are a great example, they should look like they're press-able.

Shine
Yer
Buttons
Guv'nor?
Nice 'n' shiny likes

Shiny buttons with affordance

Designed and developed by James Kyle and shamelessly pilfered from: cssdeck.com.

Any background colour may be used with this button method, but check the contrast ratio meets WCAG AA.

The effect is controlled by a multi-box-shadow which overlays the button, one for each state.

All state changes are animated via a transition.

Example buttons (well <div>s actually)

Muted tones

A tad dull perhaps but they remain my person preference.

#555
#3A617E
#477343
#723131
#4B3F5E
#FF6600

Black to white

Black
Grey #333
Grey #666
White

Primary

The brightest values acceptable for WCAG AA with white text and text-shadow.

Red #F00
Green #009700
Blue #00F

HTML

Using an anchor as an action trigger is considered poor semantics. If it does something it's a button, if it goes somewhere it's a link. Simples.

Language HTML
<button class="button gray">Do something</button>
<a href="someplace.html" class="button gray">Go somewhere</a>

CSS

Button colours are stated as simply as this.

Language CSS
.button.gray {
  background-color: #555;
}

Generic button class.

Language CSS
.button {
  position: relative;
  display: inline-block;
  padding: 0.618rem 1.618rem;
  cursor: pointer;
  color: #FFF;
  letter-spacing: 1px;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.9);
  background: #434343 none repeat scroll 0% 0%;
  border: 1px solid #242424;
  border-radius: 4px;
  box-shadow:
    0 1px 0 rgba(255, 255, 255, 0.25),
    0 1px 0 rgba(255, 255, 255, 0.25) inset,
    0 0 0 rgba(0, 0, 0, 0.5) inset,
    0 1.25rem 0 rgba(255, 255, 255, 0.08) inset,
    0 -1.25rem 1.25rem rgba(0, 0, 0, 0.3) inset,
    0 1.25rem 1.25rem rgba(255, 255, 255, 0.1) inset;

  transition: all 0.2s linear 0s;

  text-align: center;
  text-decoration: none;
  margin: 0.618rem;
}

Hover, focus and active states only change the box shadow.

Language CSS
.button:hover,
.button:focus {
  text-decoration: none;
  box-shadow:
    0 2px 5px rgba(0, 0, 0, 0.5),
    0 1px 0 rgba(255, 255, 255, 0.25) inset,
    0 0 0 rgba(0, 0, 0, 0.25) inset,
    0 20px 0 rgba(255, 255, 255, 0.03) inset,
    0 -20px 20px rgba(0, 0, 0, 0.15) inset,
    0 20px 20px rgba(255, 255, 255, 0.05) inset;
}
.button:active {
  box-shadow:
    0 1px 0 rgba(255, 255, 255, 0.25),
    0 1px 0 rgba(255, 255, 255, 0) inset,
    0 0 5px rgba(0, 0, 0, 0.5) inset,
    0 20px 0 rgba(255, 255, 255, 0.03) inset,
    0 -20px 20px rgba(0, 0, 0, 0.15) inset,
    0 20px 20px rgba(255, 255, 255, 0.05) inset;
}

A little shine at top of the button helps.

Language CSS
.button::before {
  content: "";
  display: block;
  position: absolute;
  background:-moz-linear-gradient(left, rgba(255,255,255,0) 0%, rgba(255,255,255,1) 50%, rgba(255,255,255,0) 100%);
  background:-webkit-gradient(linear, left top, right top, color-stop(0%,rgba(255,255,255,0)), color-stop(50%,rgba(255,255,255,1)), color-stop(100%,rgba(255,255,255,0)));
  background:-webkit-linear-gradient(left, rgba(255,255,255,0) 0%,rgba(255,255,255,1) 50%,rgba(255,255,255,0) 100%);
  background:linear-gradient(left, rgba(255,255,255,0) 0%,rgba(255,255,255,1) 50%,rgba(255,255,255,0) 100%);
  padding: x 12px;
  top: 0;
  left: 15%;
  height: 1px;
  width: 40%;
  box-shadow: 0 1px 5px rgba(255, 255, 255, 0.2);
  transition: all 0.3s ease-in-out 0s;
}
.button:active::before {
  opacity: 0;
}
.button:hover::before {
  left: 45%;
}

The shadows, without Sir Cliff

Shadows on white, without interactions, so you can see the effect applied on each state clearly.

Normal - soft internal gradient:

Hover - faint internal gradient, plus external shadow:

Focus - same as hover:

Active - harsher edge internal gradient:

WCAG AA color contrast and the drop shadow

Obviously white text on white background fails, but what if you use a 1 px black outline? According to the WCAG that is acceptable.

For example white on red (#F00) fails but would pass if the red used was #F00000. A pretty close alternative.

With #F00 the button drop-shadow darkens immediately around the text to #D73D37, which passes AA. Adding the default text-shadow darkens it further to #C53933.

In this specific circumstance white text with #F00 background (including shadowing) easily passes WCAG AA.

Let's consider lime-green #F00. Without shadowing it would need to be #008B00 to pass AA. A considerable difference. With the default shadowing the text is next to #64D52C also a failure. Increasing the text-shadow improves it little. From my tests there appears to be nothing short of using black text or a #009700 background (with shadowing).

Create your own (A work in progress)

Test button

Socialise: