Hire a web Developer and Designer to upgrade and boost your online presence with cutting edge Technologies

Wednesday, January 18, 2012

Faster button styling with Sass

Faster button styling with Sass

Everybody loves a nicely styled CSS button but here it is explained how to use Sass and Compass to create a mixin to implement a variety of buttons in no time and also covers browser support fallbacks

Another button tutorial?!? I hear you cry. Well yes, but this one’s a bit different. There’s plenty of tutorials out there on how to style buttons but this is a tutorial less about that and more about implementation.

As a frontend developer I work on lots of different user interfaces for web apps, services and sites and obviously one of the most common elements for all of these are buttons, or more specifically for this tutorial, buttons that allow for different icons and coloured backgrounds. So with the use of Sass and Compass I’ve created the following mixin for quickly implementing different buttons in my projects.

Base variables

We start off by declaring some base variables. Firstly the default button colour:
  1. $background-default: #D5D5D5;
We’re going to apply a gradient to the buttons using a shader. This is layered over the top of the default colour using CSS multiple backgrounds. We also declare an inverted shader which we’ll use on the active state for the buttons and for browsers that don’t support CSS gradients we’ll declare a fallback shader image:
  • View source
  • Copy code
  1. $background-shader: linear-gradient(top, rgba(0,0,0,0), rgba(0,0,0,0.25));
  2. $background-shader-i: linear-gradient(top, rgba(0,0,0,0.25), rgba(0,0,0,0));
  3. $background-shader-f: url(../images/site/button-shader.png) left center repeat-x;
There are two other images we’ll be using to style our buttons. A striped texture to give the button some depth and a divider line which is positioned between the icons and the text:
  1. $background-divider: url(../images/site/button-divider.png) 44px top repeat-y;
  2. $background-stripe: url(../images/site/button-stripe.png) left top repeat;
Finally, we’ll be using a sprite for all the icons. This sprite is divided into 40px segments so we declare this in a variable as well:
  • View source
  • Copy code
  1. $sprite-division: 40px;

Default button styles

We want to have a default state for regular buttons that don’t have an icon so let's declare those styles first:
  1. .button {
  2.         @include background($background-stripe,
  3.                                   $background-shader,
  4.                                   $background-default);
  5.         @include border-radius();
  6.         @include single-box-shadow(rgba(0,0,0,0.4), 0, 1px, 2px);
  7.         @include single-text-shadow(rgba(0,0,0,0.2), 0, -1px, 1px);
  8.        
  9.         color: #FFF;
  10.         display: inline-block;
  11.         font-size: 15px;
  12.         line-height: 15px;
  13.         margin-bottom: 24px;
  14.         padding: 12px 24px;
  15.         text-align: center;
  16.  
  17.         //more styles to go here

Fallback styles

We need to account for browsers that don’t support CSS gradients but do support multiple backgrounds (for example, Internet Explorer 9). We can use the Sass Parent Selector along with Modernizr to apply this easily:
  1.         .no-cssgradients & {
  2.                 @include background($background-stripe,
  3.                                           $background-shader-f,
  4.                                           $background-default);
  5.         }
Here, instead of applying the shader gradient we apply the shader image to achieve the same effect.

The final fallback is for browsers that don’t support CSS multiple backgrounds (for example, Internet Explorer 8 and 7):
  • View source
  • Copy code
  1.         .no-multiplebgs & {
  2.                 background-color: $background-default;
  3.         }
Here we’re just applying the default colour of the button.

Pseudo styles

For this example we want the buttons to invert their gradient shader and for the box shadow to disappear on click (:active) to create the illusion of the button being pressed. Here are the styles to achieve this including the fallback styles for no gradients and no mulitple background support:
  1.         &:hover,
  2.         &:focus  {
  3.                 color: #FFF;
  4.         }
  5.         &:active {
  6.                 @include background($background-stripe,
  7.                                           $background-shader-i,
  8.                                           $background-default);
  9.                 @include single-box-shadow(none);
  10.                
  11.                 .no-cssgradients & {
  12.                         @include background($background-stripe,
  13.                                                   $background-shader-f,
  14.                                                   darken($background-default, 5%));
  15.                 }
  16.                 .no-multiplebgs & {
  17.                         background-color: darken($background-default, 5%);
  18.                         border-color: #FFF;
  19.                 }
  20.         }
  21. }
Just a note about these styles. On the .no-cssgradients style we’re not inverting the gradient and instead using Sass’s darken function to make the background colour five per cent darker. I opted for this because to invert the gradient we’d need to load in a new background image for the inverted shader which would result in a split second delay in the image loading the first time you click a button. There are ways around this of course but this tutorial is aimed to focus on how Sass can make your life easier so we’re darkening the background colour instead.

With our basic styles we can now turn any anchor into a button by adding a .button class to it.

Buttons with icons

We’re now going to extend these button styles by adding the facility to give them icons and change their colour. Here’s some examples:
Firstly we create a sprite containing the icons. The sprite is split into 40px segments as we declared earlier in our variables. This sprite only has eight icons but we can easily add more by appending them to the bottom:
Here’s an example of the markup for a button with an icon:
  • View source
  • Copy code
  1. <a class="button twitter" href="http://twitter.com/ryanhavoc">Follow on Twitter</a>
And the specific style for this button will be:
  1. .twitter { @include button(2, #82BFC5); }

The sprite function

We can create a function in Sass to calculate the position of the sprite and return the necessary background layer for our multiple background. We work out the coordinates by multiplying the sprite division of 40px by the position parameter passed which we then offset by one.
  • View source
  • Copy code
  1. @function sprite($position) {
  2.         $coordinates: $sprite-division * ($position - 1);
  3.         @return url(../images/site/button-sprite.png) 6px (0 - $coordinates) no-repeat;

The mixin

The mixin we’re about to add simply overrides some of the styles we’ve previously set:
  1. @mixin button($position: false, $color: $background-default) {
  2.  
  3.         @if $position != false {
  4.                
  5.                 @include background(sprite($position),
  6.                                           $background-divider,
  7.                                           $background-stripe,
  8.                                           $background-shader,
  9.                                           $color);
  10.                
  11.                 padding-left: 60px;
  12.  
  13.  
  14.                 //more styles to go here
The mixin accepts two parameters, the position of the icon in the sprite, eg two for the second icon and an overriding colour, eg #82BFC5. If no colour parameter is passed, the default colour is used (#D5D5D5). This allows us to have regular grey buttons with icons.

An overriding multiple background is applied to our button which includes the sprite and an additional image for the divider line you can see in the examples. We also add some additional left padding to the button.

As with the styles for the regular button we need to override the fallback styles for no CSS gradients and no multiple background support:
  1.                 .no-cssgradients & {
  2.                         @include background(sprite($position),
  3.                                                   $background-divider,
  4.                                                   $background-stripe,
  5.                                                   $background-shader-f,
  6.                                                   $color);
  7.                 }
  8.                 .no-multiplebgs & {
  9.                         background: $color sprite($position);
  10.                         padding-left: 45px;
  11.                 }
  12.                
  13.                 &:active {
  14.                         @include background(sprite($position),
  15.                                                   $background-divider,
  16.                                                   $background-stripe,
  17.                                                   $background-shader-i,
  18.                                                   $color);
  19.                        
  20.                         .no-cssgradients & {
  21.                                 @include background(sprite($position),
  22.                                                           $background-divider,
  23.                                                           $background-stripe,
  24.                                                           $background-shader-f,
  25.                                                           darken($color, 5%));
  26.                         }
  27.                         .no-multiplebgs & {
  28.                                 background: darken($color, 10%) sprite($position);
  29.                                 padding-left: 45px;
  30.                         }
  31.                 }      
  32.         }
  33. }

Conclusion

And thats it. Lots of multiple background styles but with it all bundled into a mixin styling new buttons is easy:
  1. .facebook    { @include button(1, #507291); }
  2. .twitter     { @include button(2, #82BFC5); }
  3. .flickr      { @include button(3, #4287DB); }
  4. .dribbble    { @include button(4, #F16197); }
  5. .favourite   { @include button(8, #EDE126); }
  6. .add-user    { @include button(5); }
  7. .upload-file { @include button(6); }
  8. .delete      { @include button(7); }

No comments:

Post a Comment