Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to style a clicked button in CSS [duplicate]

Tags:

html

css

I looked at W3 schools website W3Schools which explained styling buttons with CSS. I need to specify a button style when it is clicked. What is the pseudo-class selector for this? e.g. the hover button is:

.button:hover{  } 
like image 358
user7945230 Avatar asked May 30 '17 13:05

user7945230


People also ask

How do you keep active CSS style after clicking?

The :active selector is used to select and style the active link. A link becomes active when you click on it. Tip: The :active selector can be used on all elements, not only links.

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.

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.


2 Answers

This button will appear yellow initially. On hover it will turn orange. When you click it, it will turn red. I used :hover and :focus to adapt the style. (The :active selector is usually used of links (i.e. <a> tags))

button{    background-color:yellow;  }    button:hover{background-color:orange;}    button:focus{background-color:red;}    a {    color: orange;  }    a.button{    color:green;    text-decoration: none;  }    a:visited {    color: purple;  }    a:active {    color: blue;  }
<button>  Hover and Click!  </button>  <br><br>    <a href="#">Hello</a><br><br>  <a class="button" href="#">Bye</a>
like image 181
Rachel Gallen Avatar answered Sep 17 '22 14:09

Rachel Gallen


If you just want the button to have different styling while the mouse is pressed you can use the :active pseudo class.

.button:active { } 

If on the other hand you want the style to stay after clicking you will have to use javascript.

like image 45
Alexandros Panagiotakis Avatar answered Sep 17 '22 14:09

Alexandros Panagiotakis