Reputation: 141
I'm a beginner in html (and javascript) and I'm making a project to practice what I've learned. So I'm trying to make a custom checkbox by styling it. Here's the CSS for it:
input[type=checkbox]{
-webkit-appearance: none;
outline: none;
height: 20px;
width: 20px;
border: 1px solid black;
color: black;
background-color: rgb(168, 168, 75);
box-shadow: 0 1px 2px rgba(0,0,0,0.05), inset 0px -15px 10px -12px rgba(0,0,0,0.05);
display: inline-block;
position: relative;
}
And this is the result I get:
If I click it, nothing happens. I would be grateful if someone provided any suggestions on how I could make it work!
Note: I haven't found anything specific to my problem or any recent posts related to it on Stack Overflow.
Upvotes: 0
Views: 4795
Reputation: 586
input[type=checkbox]{
-webkit-appearance: none;
outline: none;
height: 20px;
width: 20px;
border: 1px solid black;
color: black;
background-color: rgb(168, 168, 75);
box-shadow: 0 1px 2px rgba(0,0,0,0.05), inset 0px -15px 10px -12px rgba(0,0,0,0.05);
display: inline-block;
position: relative;
}
input[type=checkbox].type1:checked:after {
content: '✓'; /* or '\2713', or '\2714' */
text-align: center;
vertical-align: middle;
position: absolute;
height: 1em;
width: 1em;
line-height: 1;
margin: auto;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
input[type=checkbox].type2:checked:after {
content: '';
position: absolute;
height: 60%;
width: 30%;
margin: auto;
top: 0;
bottom: 0;
left: 0;
right: 0;
-webkit-transform: translateY(-22%) rotate(45deg);
transform: translateY(-22%) rotate(45deg);
border-right: 2px solid red;
border-bottom: 2px solid red;
}
<label><input type="checkbox" class="type1" /> Test</label>
<label><input type="checkbox" class="type2" /> Test</label>
Upvotes: 0
Reputation: 167172
Since you haven't given the :checked
state, it doesn't work. You have to explicitly specify it, something like this:
input[type=checkbox]:checked {
background-color: #000;
}
Full working snippet for you:
* {
font-family: 'Segoe UI';
}
label {
cursor: pointer;
}
input[type=checkbox] {
-webkit-appearance: none;
outline: none;
height: 20px;
width: 20px;
border: 1px solid black;
color: black;
background-color: rgb(168, 168, 75);
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05), inset 0px -15px 10px -12px rgba(0, 0, 0, 0.05);
display: inline-block;
position: relative;
vertical-align: middle;
}
input[type=checkbox]:checked {
background-color: #000;
}
<label><input type="checkbox" /> Check me!</label>
Note: I have added vertical-align: middle
so that it displays well with the text. ;)
Upvotes: 1