Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing background color without JS

I'm a beginner to css and js. But I have a basic question I can't find an answer for on google: can I change a background color on a div on click without using javascript and jquery, just with css?

like image 890
Hued Bafuli Avatar asked Sep 12 '26 07:09

Hued Bafuli


1 Answers

Yes that's possible with the :targetselector and ids.

https://developer.mozilla.org/en-US/docs/Using_the_:target_selector

HTML:

<div id="demo">Demo</div>
<a href="#demo">Click me</a>

CSS:

:target {
    background: green;
}

Demo: http://jsfiddle.net/Qptjq/2/

Or even cooler: http://jsfiddle.net/Qptjq/3/

:target requires IE9+ http://caniuse.com/#search=%3Atarget

like image 79
jantimon Avatar answered Sep 14 '26 20:09

jantimon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!