1

I've managed to style checkboxes with a background color. Now I want to have one different color per checkbox without rewriting a whole bunch of code of classes for each checkbox.

Fiddle here

.checkbox {
    display: inline-block;
    cursor: pointer;
    font-size: 13px;
    margin-right:10px;
    line-height:18px;
}

input[type=checkbox] {
    display:none;
}
.checkbox:before {
    content:"";
    display: inline-block;
    width: 18px;
    height: 18px;
    vertical-align:middle;
    background-color: red;
    color: #f3f3f3;
    text-align: center;
    box-shadow: inset 0px 2px 3px 0px rgba(0, 0, 0, .3), 0px 1px 0px 0px rgba(255, 255, 255, .8);
    border-radius: 3px;
}
.checkbox .pl {

    background-color: blue;

}

input[type=checkbox]:checked + .checkbox:before {
    content:"\2713";
    text-shadow: 1px 1px 1px rgba(0, 0, 0, .2);
    font-size: 15px;
}
<input id="Option" type="checkbox">
<label class="checkbox pl" for="Option">Option 1</label>
<input id="option2" type="checkbox">
<label class="checkbox" for="option2">Option 2</label>
<input id="option3" type="checkbox">
<label class="checkbox" for="option3">Option 3</label>
<input id="option4" type="checkbox">
<label class="checkbox" for="option4">Option 4</label>

You can see I tried to make the first checkbox blue but without success. I know it can be achieved by replicating the code for each of the checkboxes but I'm looking for an optimized solution

4 Answers 4

3

without adding classes in the markup you could use an attribute selector, e.g.

/* common style for all checkboxes */
.checkbox:before {
   content:"";
   display: inline-block;
   ...
}

/* specific background color */
[for="option"]:before { background: blue; }
[for="option2"]:before { background: red; }
[for="option3"]:before { background: yellow; }
[for="option4"]:before { background: green; }

The attribute selector is supported since IE7

Sign up to request clarification or add additional context in comments.

Comments

2

How about something like this:

.checkbox:nth-of-type(1):before {
    background-color: yellow;
}
.checkbox:nth-of-type(2):before {
    background-color: pink;
}
.checkbox:nth-of-type(3):before {
    background-color: green;
}
.checkbox:nth-of-type(4):before {
    background-color: orange;
}

The nth-of-type pseduo selector is quite powerful!

For example, you might want to go for alternating colours:

.checkbox:nth-of-type(2n-1):before {
    background-color: yellow;
}
.checkbox:nth-of-type(2n):before {
    background-color: pink;
}

Cool, huh?

1 Comment

nth-of-type doesn't work in IE7, just be aware of that
2

This is what you need:

.checkbox.pl:before {

    background-color: blue;

}

Here is the full example:

.checkbox {
    display: inline-block;
    cursor: pointer;
    font-size: 13px;
    margin-right:10px;
    line-height:18px;
}

input[type=checkbox] {
    display:none;
}
.checkbox:before {
    content:"";
    display: inline-block;
    width: 18px;
    height: 18px;
    vertical-align:middle;
    background-color: red;
    color: #f3f3f3;
    text-align: center;
    box-shadow: inset 0px 2px 3px 0px rgba(0, 0, 0, .3), 0px 1px 0px 0px rgba(255, 255, 255, .8);
    border-radius: 3px;
}
.checkbox.pl:before {

    background-color: blue;

}

input[type=checkbox]:checked + .checkbox:before {
    content:"\2713";
    text-shadow: 1px 1px 1px rgba(0, 0, 0, .2);
    font-size: 15px;
}
<input id="Option" type="checkbox">
<label class="checkbox pl" for="Option">Option 1</label>
<input id="option2" type="checkbox">
<label class="checkbox" for="option2">Option 2</label>
<input id="option3" type="checkbox">
<label class="checkbox" for="option3">Option 3</label>
<input id="option4" type="checkbox">
<label class="checkbox" for="option4">Option 4</label>

Comments

2

Your CSS is wrong

.checkbox .pl {

    background-color: blue;

}

should be

.checkbox.pl:before { // no space!

    background-color: blue;

}

2 Comments

In this case it doesn't need to be - only the first checkbox has a .pl class
It does, the background is set to the pseudo before element, not the element itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.