Ljubo Valevski
Ljubo Valevski

Reputation: 89

css styling when click on checkbox

I want to style the div#main in the following code when I click on the checkbox inside it. Can I do this using only css/css3? Thank you in advance!

<div id="main">
<div id="content">
<input type="checkbox"/>
</div>
</div> 

Upvotes: 2

Views: 12630

Answers (4)

Sirwan Afifi
Sirwan Afifi

Reputation: 10824

use this code :

#main #content input[type="checkbox"]:checked {
    /*your styles*/
}

​if you want style to #main #content you should use jquery :

$(function () {
            $("#chk").change(function () {
                if ($(this).is(":checked"))
                    $("#content").addClass("newClass");
                else {
                    $("#content").removeClass("newClass");
                }
            });
        });

CSS :

.newClass{
 width: 100px;
 height: 100px;
 background-color: red;
}

Upvotes: 0

sandeep
sandeep

Reputation: 92813

You can play with HTML & css. Write Like this:

<input type="checkbox" id="change"/>
<div id="main">
<div id="content">
<label for="change"></label>
    <h2>hello</h2>
</div>
</div> 

CSS

#main{
  width:100px;
  height:100px;
  background:red;
  text-align:center;
}
input{
    display:none;
}
input:checked ~ #main{
    border:2px solid green;
    font-size:24px;
    background:yellow;
}
input:checked ~ #main label{
    background:url(http://farm6.static.flickr.com/5182/5840005274_b3bcc52bb1_o.png);
}


label{
    background:url(http://farm3.static.flickr.com/2793/5839457953_1690178a65_o.png);
    display:block;
    width:18px;
    height:18px;
}

Check this http://jsfiddle.net/j5nTx/

Upvotes: 4

Sahil Popli
Sahil Popli

Reputation: 105

use this one

input[type="checkbox"]:checked #content {
    /*your color changing code*/
}

it applies the style code to #content when input[type="checkbox"] is cheked i.e what you want

Upvotes: 0

Man Programmer
Man Programmer

Reputation: 5356

html

<input type="checkbox" class="checkBox"/>

CSS

.checkBox:checked
{
/* apply any css */
border:solid 1px;
}

Upvotes: 0

Related Questions