Ad space (banner)
🎨HTML & CSS Lessons
Lesson 15 / 50

Adding Interaction with Pseudo-Classes (:hover)

In this lesson you'll learn how to add interaction using pseudo-classes like :hover, so you can build button motion without JavaScript. This is for people searching "CSS hover usage."

A pseudo-class is a mechanism that lets you apply a style only during a specific state, like "while the mouse is hovering over it." :hover is used while the mouse is over an element, and :focus is used while an input field is selected.

The sample code specifies a normal background color on .btn, and the background color for when the mouse hovers over it on .btn:hover, smoothing out the color change with transition. Try actually placing your mouse over the element and watch the color smoothly shift.

A common beginner stumbling block is that the transition paired with :hover needs to be specified on the normal state (.btn) side, not the :hover side. Writing transition on the :hover side instead results in an unnatural motion where only the moment you move the mouse away fails to animate smoothly.

A motion like a button's color changing when you hover the mouse over it can be achieved entirely with CSS, without JavaScript. This is a widely used technique on website buttons as visual feedback that tells the user, "this is clickable."

HTML & CSS
OUTPUT

💡 The preview runs entirely on this page — nothing is sent externally.

Ad space (banner)
Ad space (in-article)