The hover state lets you change how something looks when you move your mouse over it. It helps users know what they can click or interact with.
Hover state in CSS
Start learning this pattern below
Jump into concepts and practice - no test required
selector:hover { property: value; }
The :hover is a CSS pseudo-class that applies styles when the mouse is over the element.
You can use it on buttons, links, images, or any visible element.
button:hover { background-color: lightblue; }
a:hover { text-decoration: underline; color: red; }
.card:hover { box-shadow: 0 4px 8px rgba(0,0,0,0.3); }
This page shows a green button that changes to a darker green when you move your mouse over it. The change is smooth because of the transition.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Hover State Example</title> <style> button { background-color: #4CAF50; color: white; padding: 1rem 2rem; border: none; border-radius: 0.5rem; font-size: 1.25rem; cursor: pointer; transition: background-color 0.3s ease; } button:hover { background-color: #45a049; } </style> </head> <body> <main> <button aria-label="Submit form">Submit</button> </main> </body> </html>
Hover effects do not work on touch screens the same way because there is no mouse pointer.
Use transition to make hover changes smooth and pleasant.
Always ensure good color contrast for accessibility when changing colors on hover.
The :hover pseudo-class changes styles when the mouse is over an element.
It helps users see what is clickable or interactive.
Use smooth transitions and good contrast for better user experience.
Practice
:hover pseudo-class do?Solution
Step 1: Understand the purpose of
The:hover:hoverpseudo-class activates when the mouse pointer is over an element, changing its style temporarily.Step 2: Compare options with this behavior
Only the option "It changes the style of an element when the mouse pointer is over it." describes this temporary style change on mouse hover correctly.Final Answer:
It changes the style of an element when the mouse pointer is over it. -> Option CQuick Check:
:hoverchanges style on mouse over [OK]
- Confusing :hover with :focus or :active
- Thinking :hover applies after clicking
- Assuming :hover hides elements
button elements?Solution
Step 1: Identify correct pseudo-class syntax
The:hoverpseudo-class is used without spaces or parentheses after the selector.Step 2: Check each option
button:hover { background-color: red; } uses correct syntax:button:hover { ... }. Options A, C, and D have invalid spaces or characters.Final Answer:
button:hover { background-color: red; } -> Option BQuick Check:
Correct pseudo-class syntax = button:hover { background-color: red; } [OK]
- Adding space between selector and :hover
- Using parentheses after :hover
- Using brackets [] with :hover
<div> when hovered in this CSS?div { background-color: blue; transition: background-color 0.5s; } div:hover { background-color: green; }Solution
Step 1: Understand the transition property
Thetransition: background-color 0.5s;means background color changes smoothly over half a second.Step 2: Check hover background color
Thediv:hoverchanges background color to green, so on hover it transitions from blue to green smoothly.Final Answer:
Green smoothly over 0.5 seconds on hover -> Option AQuick Check:
Transition + :hover changes color smoothly [OK]
- Ignoring transition effect
- Expecting no color change
- Confusing color names
a:hover { color: red background-color: yellow }Solution
Step 1: Check CSS syntax for each rule
The rulea:hover { color: red background-color: yellow }is missing a semicolon afterred, which is required to separate declarations.Step 2: Understand CSS parsing behavior
Without the semicolon between declarations, the browser ignores invalid properties or the entire rule, breaking the hover effect.Final Answer:
Missing semicolon aftercolor: redstops hover from working. -> Option AQuick Check:
Always end CSS declarations with semicolon [OK]
- Thinking multiple :hover selectors cause error
- Believing background-color can't change on hover
- Assuming hover only works on buttons
button to smoothly change text color to white and background to blue on hover, but only if the button is enabled (not disabled). Which CSS selector correctly targets this?Solution
Step 1: Understand the order of pseudo-classes
The:enabledpseudo-class filters enabled buttons, and:hoverapplies when mouse is over the element.Step 2: Check correct selector syntax
button:enabled:hover { color: white; background-color: blue; transition: 0.3s; } usesbutton:enabled:hover, which is valid and applies styles only when button is enabled and hovered.Step 3: Identify errors in other options
button:hover :enabled { color: white; background-color: blue; transition: 0.3s; } includes an invalid space after:hover, selecting:enableddescendants of hovered buttons rather than the button itself. Options C and D use invalid attribute selectors for enabled state.Final Answer:
button:enabled:hover { color: white; background-color: blue; transition: 0.3s; } -> Option DQuick Check:
Use :enabled before :hover for correct targeting [OK]
- Using attribute selectors instead of :enabled
- Adding space between :hover and :enabled
- Forgetting transition for smooth effect
