Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change div background color on click using only css

Tags:

html

css

So I have a div I want to change the color of when clicked. I have three divs over all and I want to denote which one is the active div when clicking on it

Basically I want to use the CSS active property but not have the particular div change back when the mouse up occurs. Sort of like a focus. I am also using bootstrap if that is helpful

Here is a example of the html

<div>
    Section 1
</div>
<div>
    Section 2
</div>
<div>
    Section 3
</div>

Could anyone tell me how i could accomplish this without using javascript?

like image 931
Philip Loyer Avatar asked Nov 21 '14 19:11

Philip Loyer


People also ask

How do I change the background color in click CSS?

To change the background color of the button, use the CSS background-color property and give it a value of a color of your taste. In the . button selector, you use background-color:#0a0a23; to change the background color of the button.

Can div have a background color?

To set a background color for a div or related element in CSS, you use the background-color property.


2 Answers

Make your DIVs focusable, by adding tabIndex:

<div tabindex="1">
Section 1
</div>

<div tabindex="2">
Section 2
</div>

<div tabindex="3">
Section 3
</div>

Then you can simple use :focus pseudo-class

div:focus {
    background-color:red;
}

Demo: http://jsfiddle.net/mwbbcyja/

like image 128
Yuriy Galanter Avatar answered Oct 22 '22 03:10

Yuriy Galanter


Try this, it worked for me:

div:active{  

    background-color:white;

}
like image 3
Dhruv Naik Avatar answered Oct 22 '22 03:10

Dhruv Naik