Lesson 28 of 29 · Polish & Interaction
CSS Variables and Transitions: Smooth Hover Effects
Two small tools make a design feel polished and keep it easy to change. CSS custom properties, often called CSS variables, store a colour or a size once and reuse it everywhere. Transitions animate a change, so a hover effect glides smoothly instead of jumping.
CSS custom properties
A custom property is any property whose name starts with two dashes. Declare shared ones on :root, which is the <html> element, so they are available on the whole page, and read them with var():
:root {
--accent: #f5b301;
--radius: 12px;
}
.button {
background: var(--accent);
border-radius: var(--radius);
}
Change --accent in one place, and every element that uses it updates. That's how a whole site can switch between a light and a dark theme. Custom properties are inherited, so you can also override one for part of the page: .night-card { --accent: #ffd166; } changes the accent only inside night cards. var() also accepts a fallback for a missing variable: var(--accent, orange).
transform
transform moves, scales or rotates an element visually, without changing the layout around it:
.lift { transform: translateY(-6px); } /* 6px up */
.grow { transform: scale(1.05); } /* 5% bigger */
.tilt { transform: rotate(3deg); } /* turned slightly clockwise */
The surrounding elements don't move, which also makes transforms cheap for the browser to animate.
transition
A hover style normally switches on instantly. A transition animates the change instead. Give it the property to animate, a duration and, optionally, a timing function such as ease:
.card {
transition: transform 0.3s ease;
}
.card:hover {
transform: translateY(-6px);
}
Put the transition on the normal state, not only on :hover. Then the card glides up when the pointer arrives and glides back down when it leaves. To animate several properties, separate them with commas: transition: transform 0.3s, box-shadow 0.3s;.
Common mistakes
- Forgetting the dashes or
var(). A custom property must start with--, and you read it withvar(--accent). Plainaccentorvar(accent)does nothing. - Defining a variable in the wrong place. A variable set on
.headeronly exists inside the header. Put shared values on:root. - Putting the transition only in
:hover. The effect animates in, but snaps back instantly when the pointer leaves. - Leaving out the time unit.
transition: transform 300is invalid. Write300msor0.3s.
Assignment
The shop cards use hard-coded colours and sizes. Turn them into variables and add a hover effect:
- In a
:rootrule, define--accent: #f5b301and--radius: 16px. - Use the variables instead of the hard-coded values: the
.cardborder colour and the.btnbackground becomevar(--accent), and the.cardcorners becomevar(--radius). - Give
.carda0.3stransition ontransform:transition: transform 0.3s ease. - Add a
.card:hoverrule that lifts the card 6px withtransform: translateY(-6px).
Point at a card in the Preview to watch it lift.
Premium lesson
Unlock the full Learn HTML & CSS course
- All 29 lessons, bosses and quizzes — lifetime access
- One payment of $35, no subscription
- Finish within 30 days and claim $10.50 (30%) back
Want more than one course?
The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.
Keep learning
Need a quick reminder? The HTML & CSS cheat sheet has every topic of Learn HTML & CSS on one page.
Lesson last updated on .