Now days, all websites are decorated with modern stylish icons and buttons. The user will more and more look into the appearance of elements or the look and feel of the web pages. To make our websites more beautiful CSS3 help us with its different properties, without having any images of buttons and icons. The full concept of Web UI has changed by CSS3; now with the help of CSS3, HTML and a few lines of jquery we can create stylish switch buttons.
For creating a Stylish Switch Button
For creating a switch button, first of all we have to create the html.
By creating a simple basic page having necessary tags head, body tags. Try this example below
HTML of Switch Button
Here we used a ‘div’ element for creating a switch and the class ‘switch-button’ will be assigned to it. And now we will give this div style to look like a switch button like border-radius, transition and some other properties for style our switch button div.
CSS for Switch Button Div
Here comes the use of Pseudo elements. The ::before pseudo-element can be used to insert some content before the content of an element and :after pseudo-element can be used to insert some content after the content of an element.
content: ‘ ‘;
-webkit-box-shadow: 0 2px 5px #999999;
box-shadow: 0 2px 5px #999999;
And now here, we are adding a circle after the content of the div using: after pseudo element and adding some animation, so that the button transition is smooth, when we click on it. Element should be positioned absolute.
So here now we will add the background color for the switch button when it is clicked.
background: #B4045F !important;
Left: 32px !important;
And here we toggle the element; ie. Adding or removing the class ‘switchOn’. So when the switchOn class added , we will position the pseudo element as defined in the css.
And now, let me add the toggle effect using few lines of jquery.