Introduction
Active and focus states help users see which element they are clicking or typing on. This makes websites easier and safer to use.
Jump into concepts and practice - no test required
Active and focus states help users see which element they are clicking or typing on. This makes websites easier and safer to use.
selector:active { /* styles when element is clicked */ } selector:focus { /* styles when element is focused (clicked or keyboard) */ }
button:active { background-color: darkblue; color: white; }
input:focus { border-color: green; outline: 2px solid green; }
a:focus { outline: 3px dashed orange; }
This page shows a button that changes color while clicked and shows an orange outline when focused by keyboard or mouse.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Active and Focus States Example</title> <style> button { padding: 1rem 2rem; font-size: 1.25rem; background-color: lightblue; border: 2px solid blue; border-radius: 0.5rem; cursor: pointer; transition: background-color 0.3s ease; } button:active { background-color: darkblue; color: white; } button:focus { outline: 3px solid orange; outline-offset: 3px; } </style> </head> <body> <main> <h1>Active and Focus States Demo</h1> <p>Try clicking or tabbing to the button below.</p> <button>Click or Tab to me</button> </main> </body> </html>
Always provide visible focus styles for keyboard users to improve accessibility.
Active state only lasts while the mouse button is pressed down.
Use transitions to make state changes smooth and pleasant.
Active state shows when an element is being clicked.
Focus state shows when an element is selected by keyboard or mouse.
Both states improve user experience and accessibility.
:active pseudo-class in CSS represent?:active:active pseudo-class applies when the user clicks or presses an element, like a button being pressed down.:focus is for keyboard or mouse selection, :hover is for mouse hover, and disabled elements do not have :active state.:focus selector applies styles when an element is selected by keyboard or mouse, such as tabbing to a button.:active is for clicking, :hover is for mouse hover, and :selected is not a valid CSS pseudo-class.button:active { color: red; }
button:focus { color: blue; }:active and :focus.button:focus comes after button:active, so color: blue applies.:active state takes precedence during the click, so the color is red while the button is pressed.a:active { background-color: green; }
a:focus { background-color: yellow; }a:focus and a:active have same specificity. The later rule in CSS overrides earlier if both apply.a:focus is declared after a:active, its background color overrides the active style.:active after :focusa:active after a:focus ensures active style shows during click.button:focus { outline: 3px solid blue; } is correct. Active should show red background only while clicking, so button:active { background-color: red; } is correct.