Frontend Weekly

It's really hard to keep up with all the front-end development news out there. Let us help you. We…

Follow publication

Creating a toggle switch in CSS.

Awesome people build awesome 😎 things using css.

Let’s create an input checkbox with label, so that we can change the label as switch and turn on the switch when the checkbox is checked and turn off when it is unchecked.

<input type="checkbox" id="toggle" class="checkbox" /><label for="toggle" class="switch"></label>

The label is placed below the input so that we can select the label using the adjacent selector(+) .

Now the checkbox is created.

Now we need to change the label position as relative , so that we can make use of label::after and style that to appear like a switch.Also set the display : inline-block; so that we can apply width and height for the label.

.switch {    position : relative ;   display : inline-block;   width : 40px;   height : 20px;   background-color: #eee;   border-radius: 20px; }

Now the toggle switch looks like ,

Let’s add the circle ⭕️ to the toggle switch using ::after pseudo class.

.switch::after {  content: '';  position: absolute;  width: 18px;  height: 18px;  border-radius: 50%;  background-color: white;  top: 1px; // TO GIVE AN EFFECT OF CIRCLE INSIDE SWITCH.  left: 1px;  transition: all 0.3s;}

Now the switch looks like

Now we need to change the background color and position of circle when the checkbox is selected , for that let’s use :checked property on input. if the input box is checked then change the background color of label and change the position of the circle.

.checkbox:checked + .switch::after {  left : 20px; }.checkbox:checked + .switch {  background-color: #7983ff;}

Now when the checkbox is checked the input looks like

Final output.

Let’s hide the checkbox

.checkbox {    display : none;}
Final awesome 😎 toggle switch.

Follow Javascript Jeep🚙🥶

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Frontend Weekly
Frontend Weekly

Published in Frontend Weekly

It's really hard to keep up with all the front-end development news out there. Let us help you. We hand-pick interesting articles related to front-end development. You can also subscribe to our weekly newsletter at http://frontendweekly.co

Javascript Jeep🚙💨
Javascript Jeep🚙💨

Responses (1)

Write a response