Friday 27 July 2012

CSS3 Tutorial: Create A Sleek On/Off Button



By Filed in Web Design
Faraday Rotators from EOTwww.eotech.comEliminate Optical Feedback Compact, High Power, Great Price
Using a button is, so far, the preferred way to interact with an electronic stuff; such as the radio, TV, music player, and even a smartphone that has a voice command feature still needs at least one or two physical buttons.
Furthermore, in this digital age, the button has evolved in its digital form as well, which makes it more interactive, dynamic and real easy to make, compared to the physical button.
So, this time, we are going to create a slick and interactive button which is based on this excellent design over at Dribbble using only CSS.
Well, let’s just get started.

HTML

We will start off the button by placing the following markup on our HTML document. It’s really simple, the button would be based on an anchor tag, we also have a span next to it to create the indicator light, and then they are wrapped together within an HTML5 section tag.
  1. <section>  
  2.     <a rel="external" href="#button" id="button">&#xF011;</a>  
  3.     <span></span>  
  4. </section>  
Here is how our button initially looks like.

Basic Styling

In this section, we will start working on the Styles.
We firstly apply this dark background from Subtle Pattern on the body’s document, and center thesection. Then, we will also remove the dotted outline upon the :focus and :active link.
  1. body {  
  2.     backgroundurl('images/micro_carbon.png');  
  3. }  
  4. section {  
  5.     margin150px auto 0;  
  6.     width75px;  
  7.     height95px;  
  8.     positionrelative;  
  9.     text-aligncenter;  
  10. }  
  11. :active, :focus {  
  12.     outline: 0;  
  13. }  

Using Custom Font

For the button’s icon, we will be using a custom font from Font Awesome rather than an image. That way the icon will be easily style-able and scale-able through the stylesheet.
Download the font, store the font files (eot, woff, ttf and svg) in the fonts folder, and then place the following code on your stylesheet to define a new font family.
  1. @font-face {  
  2.   font-family"FontAwesome";  
  3.   srcurl("fonts/fontawesome-webfont.eot");  
  4.   srcurl("fonts/fontawesome-webfont.eot?#iefix"format('eot'),  
  5.        url("fonts/fontawesome-webfont.woff"format('woff'),  
  6.        url("fonts/fontawesome-webfont.ttf"format('truetype'),  
  7.        url("fonts/fontawesome-webfont.svg#FontAwesome"format('svg');  
  8.   font-weightnormal;  
  9.   font-stylenormal;  
  10. }  
The power icon that we need for this button is represented in Unicode number F011; if you look closely at the HTML markup above, we have put this numeric character &#xF011; within the anchor tag, but since we haven’t defined the custom font-family in the button style, the icon is yet to be rendered correctly.

Styling The Button

First of all, we need to define the custom font-family for the button.
Our button will be a circle, we can achieve the circle effect using the border-radius property and set the value at, at least, half of the button’s width.
Since, we are using a font for the icon, we can easily set the color and add text-shadow for the icon in the stylesheet as well.
Next, we will also create a beveled effect for the button. This effect is quite tricky. First, we need to apply background-color: rgb(83,87,93); for the button’s color base, then we add up to four layers of box-shadows.
  1. a {  
  2.     font-family"FontAwesome";  
  3.     colorrgb(37,37,37);  
  4.     text-shadow0px 1px 1px rgba(250,250,250,0.1);  
  5.     font-size32pt;  
  6.     displayblock;  
  7.     positionrelative;  
  8.     text-decorationnone;  
  9.     background-colorrgb(83,87,93);  
  10.     box-shadow: 0px 3px 0px 0px rgb(34,34,34), /* 1st Shadow */  
  11.                 0px 7px 10px 0px rgb(17,17,17), /* 1nd Shadow */  
  12.                 inset 0px 1px 1px 0px rgba(250, 250, 250, .2), /* 3rd Shadow */  
  13.                 inset 0px -12px 35px 0px rgba(0, 0, 0, .5); /* 4th Shadow */  
  14.     width70px;  
  15.     height70px;  
  16.     border: 0;  
  17.     border-radius: 35px;  
  18.     text-aligncenter;  
  19.     line-height79px;  
  20. }  
There is also a larger circle in the outside of the button and we will be using the :before pseudo-element for it rather than adding extra markup.
  1. a:before {  
  2.     content"";  
  3.     width80px;  
  4.     height80px;  
  5.     displayblock;  
  6.     z-index: -2;  
  7.     positionabsolute;  
  8.     background-colorrgb(26,27,29);  
  9.     left: -5px;  
  10.     top: -2px;  
  11.     border-radius: 40px;  
  12.     box-shadow: 0px 1px 0px 0px rgba(250,250,250,0.1),  
  13.                 inset 0px 1px 2px rgba(0, 0, 0, 0.5);  
  14. }  
Further reading: CSS :before and :after pseudo-elements (Hongkiat.com)

Indicator Light

Under the button, there is a tiny light to designate the Power On and Off status. Below, we apply red for the light’s color because the power is initially OFF, we also add box-shadow to imitate the gleam effect of the light.
  1. a + span {  
  2.     displayblock;  
  3.     width8px;  
  4.     height8px;  
  5.     background-colorrgb(226,0,0);  
  6.     box-shadow: inset 0px 1px 0px 0px rgba(250,250,250,0.5),  
  7.                 0px 0px 3px 2px rgba(226,0,0,0.5);  
  8.     border-radius: 4px;  
  9.     clearboth;  
  10.     positionabsolute;  
  11.     bottombottom: 0;  
  12.     left: 42%;  
  13. }  

The Effect

At this point our button starts looking good and we only need to add some effects on it, for instance, when the button is ‘being’ clicked on, we want the button to look like it’s being pressed and held down.
To achieve the effect, the first box-shadow in the button will be zeroed and the position will be lowered a bit. We also need to adjust the other three shadows’ intensities a little to match the button position.
  1. a:active {  
  2.     box-shadow: 0px 0px 0px 0px rgb(34,34,34), /* 1st Shadow */  
  3.                 0px 3px 7px 0px rgb(17,17,17), /* 2nd Shadow */  
  4.                 inset 0px 1px 1px 0px rgba(250, 250, 250, .2), /* 3rd Shadow */  
  5.                 inset 0px -10px 35px 5px rgba(0, 0, 0, .5); /* 4th Shadow */  
  6.     background-colorrgb(83,87,93);  
  7.     top3px;  
  8. }  
Furthermore, once the button has been clicked, it should remain pressed down and the icon should ‘shine’ to indicate that the power is ON.
To achieve such an effect we will target the button using the :target pseudo-class, then change the icon’s color to white and add a text-shadow with white color as well.
  1. a:target {  
  2.     box-shadow: 0px 0px 0px 0px rgb(34,34,34),  
  3.                 0px 3px 7px 0px rgb(17,17,17),  
  4.                 inset 0px 1px 1px 0px rgba(250, 250, 250, .2),  
  5.                 inset 0px -10px 35px 5px rgba(0, 0, 0, .5);  
  6.     background-colorrgb(83,87,93);  
  7.     top3px;  
  8.     color#fff;  
  9.     text-shadow0px 0px 3px rgb(250,250,250);  
  10. }  
Further reading: Using :target pseudo-class
We also need to adjust the box-shadow in the circle outside the button, as follows.
  1. a:active:before, a:target:before {  
  2.     top: -5px;  
  3.     background-colorrgb(26,27,29);  
  4.     box-shadow: 0px 1px 0px 0px rgba(250,250,250,0.1),  
  5.                 inset 0px 1px 2px rgba(0, 0, 0, 0.5);  
  6. }  
The light indicator will turn from the default red to green color to emphasize that the power is ON already.
  1. a:target + span {  
  2.     box-shadow: inset 0px 1px 0px 0px rgba(250,250,250,0.5),  
  3.                 0px 0px 3px 2px rgba(135,187,83,0.5);  
  4.     background-colorrgb(135,187,83);  
  5. }  

Transition Effect

Lastly, to make the button’s effect run smoothly, we will also apply the following transition effect.
This snippet below will specifically add the transition to the color property and the text-shadowfor 350ms in the anchor element.
  1. a {  
  2. transition: color 350ms, text-shadow 350ms;  
  3.     -o-transition: color 350ms, text-shadow 350ms;  
  4.     -moz-transition: color 350ms, text-shadow 350ms;  
  5.     -webkit-transition: color 350ms, text-shadow 350ms;  
  6. }  
This second snippet below will add the transition for the background-color and box-shadowproperty in the light indicator.
  1. a:target + span {  
  2. transition: background-color 350ms, box-shadow 700ms;  
  3.     -o-transition: background-color 350ms, box-shadow 700ms;  
  4.     -moz-transition: background-color 350ms, box-shadow 700ms;  
  5.     -webkit-transition: background-color 350ms, box-shadow 700ms;  
  6. }  

Final Result

We have come through all the styles we need, now you can see the final result live as well as download the source file from the links below.

Bonus: How To Turn It Off

Here comes the bonus. If you have tried the button from the above demo, you’ve noticed that the button can only be clicked once, an that’s to turn it on, so how do we turn it off?.
Unfortunately, we have to do it with the jQuery, but it is really simple as well. Below is the all the jQuery code we need.
  1. $(document).ready(function(){  
  2.     $('#button').click(function(){  
  3.         $(this).toggleClass('on');  
  4.     });  
  5. });  
The snippet above will add the class ON in the anchor, and we used the toggleClass function from jQuery to add it. So, when the #button is clicked on, the jQuery will check whether the class ON has been added or not: when it is not, the jQuery will add the class, and if it has been added, the jQuery will remove the class.
Note: Don’t forget to include the jQuery library.
Now we have to change the Style a little bit. Simply replace all the :target pseudo-element with the .on class selector, as follows:
  1. a.on {  
  2.     box-shadow: 0px 0px 0px 0px rgb(34,34,34),  
  3.                 0px 3px 7px 0px rgb(17,17,17),  
  4.                 inset 0px 1px 1px 0px rgba(250, 250, 250, .2),  
  5.                 inset 0px -10px 35px 5px rgba(0, 0, 0, .5);  
  6.     background-colorrgb(83,87,93);  
  7.     top3px;  
  8.     color#fff;  
  9.     text-shadow0px 0px 3px rgb(250,250,250);  
  10. }  
  11. a:active:before, a.on:before {  
  12.     top: -5px;  
  13.     background-colorrgb(26,27,29);  
  14.     box-shadow: 0px 1px 0px 0px rgba(250,250,250,0.1),  
  15.                 inset 0px 1px 2px rgba(0, 0, 0, 0.5);  
  16. }  
  17. a.on + span {  
  18.     box-shadow: inset 0px 1px 0px 0px rgba(250,250,250,0.5),  
  19.                 0px 0px 3px 2px rgba(135,187,83,0.5);  
  20.     background-colorrgb(135,187,83);  
  21. }  
Finally, let’s try it in the browser.

You Might Like:

Graphic & web designer; having been about 2 years working as a freelance designer specializing in e-Commerce for retail industries. He also shares his enthusiast in web design at creatiface.com and can be contacted on Twitter @creatiface

No comments:

Post a Comment