1

I am using checkboxes on my page. It works fine. However I want one checkbox with light blue color and another with light yellow. Currently both checkboxes are light blue. How can I make another one light yellow?

Here is what I have.

input[type=checkbox] {
  position: relative;
  cursor: pointer;
margin-right: 10px;
}
input[type=checkbox]:before {
  content: "";
  display: block;
  position: absolute;
  width: 16px;
  height: 16px;
  top: 0;
  left: 0;
  border: 1px solid #99AFC1;
  border-radius: 3px;
  background-color: lightblue;
  padding: 1px;
}
input[type=checkbox]:checked::before {
  background-color: lightblue;
}

input[type=checkbox]:checked::after {
  content: "";
  display: block;
  width: 5px;
  height: 10px;
  border: solid #ffffff;
  border-width: 0 2px 2px 0;
  -webkit-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  transform: rotate(45deg);
  position: absolute;
  top: 2px;
  left: 6px;
}
<p><label><input type="checkbox" class="filter" value="Test1">Test1</label> <label><input type="checkbox" class="filter" value="Test2">Test2</label></p>
newuser
  • 245
  • 1
  • 1
  • 8
  • 2
    Give one of them a distinct classname, and change the color in that class. – Daniel Beck Mar 10 '23 at 16:01
  • I tried but it changes for both of them. Can you please provide an example. Thanks. – newuser Mar 10 '23 at 16:10
  • You can't apply ::before or ::after pseudo elements to input elements. See [here](https://stackoverflow.com/a/4660434/12571484) for details. It renders in chrome but not in firefox. It's not part of the spec. See the link. – Adam Mar 10 '23 at 16:16

1 Answers1

3

*This renders the same in Chrome, Edge, and Firefox.

Just give a class for the ones you want to be lightyellow.
And for a cross-browser method, I've used a <label> and <span> for the styling.

label.checkbox input[type="checkbox"] {
  display:none;
}
label.checkbox span {
  display:inline-block;
  border: 1px solid Gray;
  border-radius: 4px;
  width: 18px;
  height: 18px;
  background-color: lightblue;
  vertical-align: top;
  position: relative;
}
label.checkbox :checked + span {
  width: 18px;
  height: 18px;
}
label.checkbox :checked + span:after {
  content: '\2714';
  font-size: 100%;
  position: absolute;
  top: -12%;
  left: 12%;
  color: lightyellow;
}

/* Make the above your default checkbox, and the below can be used for special ones. */

label.checkbox.difcol span {
  background-color: lightyellow; /* New Box Color */
}
label.checkbox.difcol :checked + span:after {
  color: lightblue; /* Corresponding New Checkmark Color */
}
<p>
<label class="checkbox"><input type="checkbox"/><span></span></label> Test1
<label class="checkbox difcol"><input type="checkbox"/><span></span></label> Test2
</p>

I had to change the checkmark color too so you could see it, and I called the class "difcol" meaning "different color". I recommend you call the class "lightyellow-lightblue", i.e. make the name of the class the colors that you choose for that box / those boxes.

Robert Bradley
  • 548
  • 2
  • 21
  • 2
    Just FYI this doesn't render in firefox because input elements can't contain child elements. See [here](https://stackoverflow.com/questions/2587669/can-i-use-a-before-or-after-pseudo-element-on-an-input-field/4660434#4660434) for details. – Adam Mar 10 '23 at 16:24
  • worked on firefox as well! – newuser Mar 10 '23 at 16:25
  • @Adam, yes thank you, I'm still trying to touch it up for that reason and I'll edit this when I do. – Robert Bradley Mar 10 '23 at 16:25
  • Doens't on FF 110.0.1 which I'm running on my desktop. @RobertBradley, the usual way is to style a wrapping div (or even styling the label) and use pseudo elements on that. Cheers. – Adam Mar 10 '23 at 16:27
  • Sorry caught me mid edit :-) – Adam Mar 10 '23 at 16:30
  • 1
    And yeah, I'm going to try styling a ` – Robert Bradley Mar 10 '23 at 16:32
  • 1
    I have now made it work across browsers. (I tested it on Chrome, Edge, and Firefox; it renders the same in all) – Robert Bradley Mar 10 '23 at 17:43