Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS styling checkbox when checked - not working

Tags:

html

css

checkbox

I have the following static html file, where I'm spending time building a CMS web application site for our client.

http://cms.tmadev.com.au/usergroup.html

In middle section, there's a vertical array of checkboxes (which I css-styled it) and I followed numerous online tutorials, which lead me to use this site link.

http://csscheckbox.com/

I download the source code tutorial, understood it, copied the css code, tailor my changes as per my client requirements.

Everything looks perfectly fine - except when trying to check the checkbox.

NOTHING HAPPENS!

The checkbox doesn't get checked when clicked!

I couldn't figure out why it's not working as it's supposed to be like the tutorial.

Can someone please tell me what I did wrong?

Here's my code.

input[type=checkbox].input-checkbox{
    width: 1px;
    height: 1px;
    position: absolute;
    overflow: hidden;
    clip: rect(0,0,0,0);
    margin: -1px;
    padding: 0;
    border: 0;
}


input[type=checkbox].input-checkbox + label.input-label{
    border: 2px solid #58585A;
    display: inline-block;
    width: 20px;
    height: 20px;
    line-height: 15px;
    background-repeat: no-repeat;
    font-size:15px;
    vertical-align: middle;
    cursor: pointer;
}

input[type=checkbox].input-checkbox:checked + label.input-label{
    background-position: 0 -20px;   
}

.input-label{
    background-image: url('/images/tickbox.png');
}

Thanks very much!

like image 634
awongCM Avatar asked Jan 22 '14 00:01

awongCM


People also ask

How do I style a checkbox in CSS?

Use the :checked pseudo-class, which helps to see when the checkbox is checked. Style the label with the width, height, background, margin, and border-radius properties. Set the position to "relative". Style the "checkbox-example" class by setting the display to "block" and specifying the width and height properties.

How do I change the color of a checkbox when checked CSS?

Use the accent-color property to change the checkbox color in CSS.

How do I highlight a checkbox in CSS?

It is possible to style a checkbox using Pseudo Elements like :before, :after, hover and :checked. To style the checkbox the user first needs to hide the default checkbox which can be done by setting the value of the visibility property to hidden.

How check checkbox is checked or not CSS?

The :checked CSS pseudo-class selector represents any radio ( <input type="radio"> ), checkbox ( <input type="checkbox"> ), or option ( <option> in a <select> ) element that is checked or toggled to an on state.


1 Answers

The CSS is fine. Your problem is matching the label elements with the input elements.

This method relies on the fact that clicking the label toggles the checkbox. If the label isn't attatched to the checkbox it won't work. Match the value of each label's for attribute to the id of each checkbox.

For example:

<input type="checkbox" class="input-checkbox" id="checkbox1">
<label for="checkbox1" class="input-label"></label>

LIVE EXAMPLE HERE

like image 159
Josh Crozier Avatar answered Sep 18 '22 08:09

Josh Crozier