What is active class in css?


Example

Select and style the active link:

a:active {
  background-color: yellow;
}

Try it Yourself »

More "Try it Yourself" examples below.


Definition and Usage

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.

Tip: Use the :link selector to style links to unvisited pages, the :visited selector to style links to visited pages, and the :hover selector to style links when you mouse over them.

Note: :active MUST come after :hover (if present) in the CSS definition in order to be effective!

Version:CSS1

Browser Support

The numbers in the table specifies the first browser version that fully supports the selector.

Selector
:active 4.0 7.0 2.0 3.1 9.6


CSS Syntax

:active {
  css declarations;
}



More Examples

Example

Select and style a

,

and element when you click on it:

p:active, h2:active, a:active {
  background-color: yellow;
}

Try it Yourself »

Example

Select and style unvisited, visited, hover, and active links:

/* unvisited link */
a:link {
  color: green;
}

/* visited link */
a:visited {
  color: green;
}

/* mouse over link */
a:hover {
  color: red;
}

/* selected link */
a:active {
  color: yellow;
}

Try it Yourself »

Example

Style links with different styles:

a.ex1:hover, a.ex1:active {
  color: red;
}

a.ex2:hover, a.ex2:active {
  font-size: 150%;
}

Try it Yourself »


CSS tutorial: CSS Links

CSS tutorial: CSS Pseudo classes




Learn how to add an active class to the current element with JavaScript.


Highlight the active/current (pressed) button:

Try it Yourself »


Active Element

Step 1) Add HTML:

Example


 
 
 
 
 


Step 2) Add CSS:

Example

/* Style the buttons */
.btn {
  border: none;
  outline: none;
  padding: 10px 16px;
  background-color: #f1f1f1;
  cursor: pointer;
}

/* Style the active class (and buttons on mouse-over) */
.active, .btn:hover {
  background-color: #666;
  color: white;
}



Step 3) Add JavaScript:

Example

// Get the container element
var btnContainer = document.getElementById("myDIV");

// Get all buttons with class="btn" inside the container
var btns = btnContainer.getElementsByClassName("btn");

// Loop through the buttons and add the active class to the current/clicked button
for (var i = 0; i < btns.length; i++) {
  btns[i].addEventListener("click", function() {
    var current = document.getElementsByClassName("active");
    current[0].className = current[0].className.replace(" active", "");
    this.className += " active";
  });
}

Try it Yourself »

If you do not have an active class set on the button element to start with, use the following code:

Example

// Get the container element
var btnContainer = document.getElementById("myDIV");

// Get all buttons with class="btn" inside the container
var btns = btnContainer.getElementsByClassName("btn");

// Loop through the buttons and add the active class to the current/clicked button
for (var i = 0; i < btns.length; i++) {
  btns[i].addEventListener("click", function() {
    var current = document.getElementsByClassName("active");

    // If there's no active class
    if (current.length > 0) {
      current[0].className = current[0].className.replace(" active", "");
    }

    // Add the active class to the current/clicked button
    this.className += " active";
  });
}

Try it Yourself »



What is a active class?

An Active Class indicates that, when instantiated, the Class controls its own execution. Rather than being invoked or activated by other objects, it can operate standalone and define its own thread of behavior.

What is is active and active in CSS?

An element is :active when it is clicked on or gains focus through other means. . active refers to a class name. In this case, the CSS rule applies to an element that has the active class within the #toolbar parent element.

Why do we use active in CSS?

:active is a CSS pseudo-class. It specifies and selects an element based on a state—the active state—and is used to apply styles to an element when it matches that state. The :active pseudo-class is a dynamic class which applies when an element is being activated by the user.

What is hover and active in CSS?

The :hover pseudoclass allows you to define the styles of an element the mouse hovers over. :active means: an element that the user holds the mouse button depressed on. In very old browsers they only worked for links, but nowadays they have been ported to all other elements.