How to Style Checkboxes with Different Colors Using CSS?

Barbara Streisand
Release: 2024-11-06 09:52:02
Original
678 people have browsed it

How to Style Checkboxes with Different Colors Using CSS?

Enhanced CSS Styling for Checkboxes

Many solutions exist online for styling checkboxes using CSS. However, if you desire greater flexibility and the ability to apply different colors to individual checkboxes, consider the following approach:

Requirement:
Customize checkboxes with various colors without creating numerous images.

Solution:
Utilize a transparent PNG image with a white exterior and partially transparent checkbox. Apply a CSS-specified background color to the HTML element, resulting in a color overlay on the checkbox.

Code:
JavaScript:

// Check for checkbox and apply CSS classes
if (inputs[a].type == "checkbox" || inputs[a].type == "radio" && inputs[a].className.search(/^styled/) != -1) {
  span[a] = document.createElement("span");
  span[a].className = inputs[a].type + inputs[a].className.replace(/^styled/, "");
}
Copy after login

CSS:

.checkbox, .radio {
  width: 19px;
  height: 25px;
  padding: 0px;
  background: url(checkbox2.png) no-repeat;
  display: block;
  clear: left;
  float: left;
}
Copy after login

Additional CSS
Define different color classes and associate them with background colors.

HTML:

<p><input type="checkbox" name="1">
Copy after login

This method leverages PNG transparency to achieve the desired effect, assuming PNG support. If necessary, alternative methods can be employed, such as using CSS layers overlaid with GIF masks.

Example (jQuery):
https://jsfiddle.net/jtbowden/xP2Ns/

The above is the detailed content of How to Style Checkboxes with Different Colors Using CSS?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!