I researched a lot, and I found this JSFiddle looks like what I need, but, when I click the button, the page background is changing. I want the button’s color to be changed when clicked, instead.
div{
width: 100%;
height: 100%;
background: #5CB85C;
position: absolute;
top: 0;
left: 0;
z-index:1;
}
label.btn {
position: absolute;
top:10px;
left: 10px;
z-index:2;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
input[type="checkbox"]{
display: none;
}
input[type="checkbox"]:checked + div{
background: #5BC0DE;
}
label + input[type="checkbox"]:checked{
background: #000;
}
<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet"/>
<label for="check" class="btn btn-default">Toggle background colour</label>
<input type="checkbox" id="check" />
<div></div>
6
Answers
This is fairly trivial. Simply move the “button”
label
element to after the checkbox so that you can select it with the+
selector (this is called the adjacent sibling selector; there is no previous sibling selector, unfortunately), and change the CSS to match. JSFiddleTo achieve what you want, in your css use
background-color
when the button is clicked.“:active” property is used for displaying clicked event style…
HTML:
CSS:
TEST
In css you can not go backward you should go forward like following.
You need to put label after input box.
+
is for the next sibling. There is no “previous sibling” selector.Check Updated Fiddle
Try using css pseudo code:
change
to