Home >Web Front-end >CSS Tutorial >How to Replace Default Checkbox Images with Custom On/Off Images Using Pure CSS?
Pure CSS Checkbox Image Replacement
For your checkbox list, you aim to replace the default checkbox images with custom on/off images using CSS.
To achieve this, follow these steps:
Hide the Original Checkbox:
Create a Custom Label:
Style the Label with Background Images:
Position the Images Correctly:
Complete CSS Code Example:
input[type=checkbox] { display: none; /* Hide the checkbox */ } input[type=checkbox] + label { display: inline-block; /* Position the label next to the checkbox */ width: 16px; /* Width of the label = Width of the checkboxes */ height: 16px; /* Height of the label = Height of the checkboxes */ background-image: url('/images/off.png'); /* Default to "off" image */ background-position: 0 0; } input[type=checkbox]:checked + label { background-image: url('/images/on.png'); /* Change image to "on" image when checked */ }
Note: Ensure that the paths to your custom images are correct. Refer to the provided JavaScript Fiddle and Gist for a complete working example.
The above is the detailed content of How to Replace Default Checkbox Images with Custom On/Off Images Using Pure CSS?. For more information, please follow other related articles on the PHP Chinese website!