Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS change button style after click

I was wondering if there was a way to change a button's style, in css, after it's been clicked, so not a element:active.

like image 974
JohnyNich Avatar asked Feb 09 '17 10:02

JohnyNich


People also ask

How do I change the color of a clicked button in 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.

How do you keep active CSS style after clicking a button?

:active denotes the interaction state (so for a button will be applied during press), :focus may be a better choice here. However, the styling will be lost once another element gains focus.

How do you style a click button?

Unfortunately, there is no :click pseudo selector. If you want to change styling on click, you should use Jquery/Javascript. It certainly is better than the "hack" for pure HTML / CSS.


2 Answers

If you're looking for a pure css option, try using the :focus pseudo class.

#style  {     background-color: red; }  #style:focus {          background-color:yellow;     } 
like image 76
Rich Avatar answered Sep 18 '22 22:09

Rich


Each link has five different states: link, hover, active, focus and visited.

Link is the normal appearance, hover is when you mouse over, active is the state when it's clicked, focus follows active and visited is the state you end up when you unfocus the recently clicked link.

I'm guessing you want to achieve a different style on either focus or visited, then you can add the following CSS:

a { color: #00c; } a:visited { #ccc; } a:focus { #cc0; } 

A recommended order in your CSS to not cause any trouble is the following:

a a:visited { ... } a:focus { ... } a:hover { ... } a:active { ... } 

You can use your web browser's developer tools to force the states of the element like this (Chrome->Developer Tools/Inspect Element->Style->Filter :hov): Force state in Chrome Developer Tools

like image 22
MrD Avatar answered Sep 17 '22 22:09

MrD