Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
7
rated 0 times [  13] [ 6]  / answers: 1 / hits: 67406  / 11 Years ago, tue, october 8, 2013, 12:00:00

Is it possible to change the background color on button click; of the entire document, using only CSS and HTML5?



(e.g.: it's trivial in JavaScript)


More From » jquery

 Answers
27

Based on the fiddle I posted in the comments I've modified it very slightly to use the Bootstrap button CSS.



Just include Bootstrap's CSS file then use the code below.



HTML



<label for=check class=btn btn-default>Toggle background colour</label>
<input type=checkbox id=check />
<div></div>


CSS



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;
}
input[type=checkbox]{
display: none;
}
input[type=checkbox]:checked + div{
background: #5BC0DE;
}


This uses the adjacent sibling selector.



Here it is working: http://jsfiddle.net/eYgdm/ (I've added *-user-select: none; to prevent selection of the label text but it's not required for the label to work)



Browser support



Chrome, Firefox [Desktop & Mobile] (Gecko) ≥ 1.0, Internet Explorer ≥ 7, Opera ≥ 9 and Safari ≥ 3
References: Attribute selectors and Adjacent sibling selectors


[#75135] Monday, October 7, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cruz

Total Points: 415
Total Questions: 98
Total Answers: 109

Location: France
Member since Thu, May 6, 2021
3 Years ago
cruz questions
Tue, Sep 14, 21, 00:00, 3 Years ago
Fri, Mar 26, 21, 00:00, 3 Years ago
Sat, Oct 26, 19, 00:00, 5 Years ago
;