chiapa
chiapa

Reputation: 4412

Styling HTML checkboxes issue

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

Upvotes: 1

Views: 813

Answers (4)

roryok
roryok

Reputation: 9645

Your CSS is wrong

.checkbox .pl {

    background-color: blue;

}

should be

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

    background-color: blue;

}

Upvotes: 2

Fabrizio Calderan
Fabrizio Calderan

Reputation: 123397

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

Upvotes: 3

Shomz
Shomz

Reputation: 37711

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>

Upvotes: 2

gvee
gvee

Reputation: 17171

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?

Upvotes: 2

Related Questions