Problem: Custom Toggle Switch Control
Problem description
Given an HTML page containing a label with the class .switch that wraps a native checkbox input and a span element with the class .slider, write CSS to:
Hide the native checkbox input.
Style the
.slideras a50px × 24pxrounded rectangle with a light gray background.Use
::beforeon.sliderto create a20px × 20pxcircular knob positioned on the left.Change the background of the
.sliderto#2196F3when thecheckboxis checked.Move the knob to the right inside the slider when checked, using
transform.
Goal
Write CSS rules so that the switch track changes color and the circular knob slides from left to right when checked, simulating an on/off toggle.
Constraints
Use only CSS (no JavaScript).
.slidersize should be50pxwidthand24pxheight;border-radiusshould be24px.Knob size should be
20pxdiameter;background-colorshould be#fff;border-radiusshould be50%.Checked track background should be
#2196F3.Knob transition should be
0.2sease-in-outfor smooth sliding.
Sample visual output
Here’s what the output would look like:
Good luck trying the problem! If you’re unsure how to proceed, check the Solution.
Problem: Custom Toggle Switch Control
Problem description
Given an HTML page containing a label with the class .switch that wraps a native checkbox input and a span element with the class .slider, write CSS to:
Hide the native checkbox input.
Style the
.slideras a50px × 24pxrounded rectangle with a light gray background.Use
::beforeon.sliderto create a20px × 20pxcircular knob positioned on the left.Change the background of the
.sliderto#2196F3when thecheckboxis checked.Move the knob to the right inside the slider when checked, using
transform.
Goal
Write CSS rules so that the switch track changes color and the circular knob slides from left to right when checked, simulating an on/off toggle.
Constraints
Use only CSS (no JavaScript).
.slidersize should be50pxwidthand24pxheight;border-radiusshould be24px.Knob size should be
20pxdiameter;background-colorshould be#fff;border-radiusshould be50%.Checked track background should be
#2196F3.Knob transition should be
0.2sease-in-outfor smooth sliding.
Sample visual output
Here’s what the output would look like:
Good luck trying the problem! If you’re unsure how to proceed, check the Solution.