Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between :focus and :active?

What is the difference between the :focus and :active pseudo-classes?

like image 320
Jitendra Vyas Avatar asked Nov 05 '09 02:11

Jitendra Vyas


People also ask

What does focused button mean?

:focus Selector: It generally applies on form elements or elements that can be focused using keyboard or mouse like input box, textarea. An element is in focus state while we use “tab” key of keyboard for that particular element. The state of focus will be same until user switch tab to another element or click.

What are focus and active pseudo classes?

:hover , :focus , and :active are pseudo-classes that are determined by a user's actions. They each correspond to a very specific point in how a user will interact with an element on a page such as a link or a button or an input field.

What is the difference between focus and hover?

Hover: by putting your cursor over it. A hovered element is ready to be activated with a mouse or any mouse-emulating technology (such as eye and motion tracking). Focus: a focused element is ready to be activated with a keyboard or any keyboard-emulating technology (such as switch devices).

What is an active button?

The :active CSS pseudo-class represents an element (such as a button) that is being activated by the user. When using a mouse, "activation" typically starts when the user presses down the primary mouse button.


2 Answers

:focus and :active are two different states.

  • :focus represents the state when the element is currently selected to receive input and
  • :active represents the state when the element is currently being activated by the user.

For example let's say we have a <button>. The <button> will not have any state to begin with. It just exists. If we use Tab to give "focus" to the <button>, it now enters its :focus state. If you then click (or press space), you then make the button enter its (:active) state.

On that note, when you click on an element, you give it focus, which also cultivates the illusion that :focus and :active are the same. They are not the same. When clicked the button is in :focus:active state.

An example:

<style type="text/css">    button { font-weight: normal; color: black; }    button:focus { color: red; }    button:active { font-weight: bold; }  </style>      <button>    When clicked, my text turns red AND bold!<br />    But not when focused only,<br />   where my text just turns red  </button>

edit: jsfiddle

like image 116
Andrew Moore Avatar answered Sep 22 '22 23:09

Andrew Moore


:active       Adds a style to an element that is activated :focus        Adds a style to an element that has keyboard input focus :hover        Adds a style to an element when you mouse over it :lang         Adds a style to an element with a specific lang attribute :link         Adds a style to an unvisited link :visited      Adds a style to a visited link 

Source: CSS Pseudo-classes

like image 42
Rubens Farias Avatar answered Sep 23 '22 23:09

Rubens Farias