Difference between :focus and :active selector in HTML


:focus

:focus selector is used to applying a required style when a form element got to focus like button, link, input box. An element can get focus using mouse or using tab key. A focus remains on the element until another element gets focus.

:active

:active selector is used to indicating that an anchor tag is active or a button is active. When the mouse is down, active selector applies and remains applied till the mouse is down.

Example

Following the example, shows usage of :focus as well as :active selector on button and link.

<!DOCTYPE html>
<html>
<head>
   <title>Selector Example</title>
   <style>
      button {
         border: 2px solid black;
      }
      button:focus {
         border: 2px dotted red;
      }
      a {
         color: black;
      }
      a:active {
         color: red;
      }
   </style>
</head>
<body>
   <button type="submit">Focus Me</button>
   <a href="#">Active Me</a>
</body>
</html>
raja
Published on 13-Jan-2020 06:17:23
Advertisements