What is the difference between the :focus
and :active
pseudo-classes?
: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.
: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.
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).
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.
: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
: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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With