Skip to content
Codewilds

Lesson 28 of 29 · Polish & Interaction

CSS Variables and Transitions: Smooth Hover Effects

medium20 XP2 min read

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 with var(--accent). Plain accent or var(accent) does nothing.
  • Defining a variable in the wrong place. A variable set on .header only 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 300 is invalid. Write 300ms or 0.3s.

Assignment

The shop cards use hard-coded colours and sizes. Turn them into variables and add a hover effect:

  1. In a :root rule, define --accent: #f5b301 and --radius: 16px.
  2. Use the variables instead of the hard-coded values: the .card border colour and the .btn background become var(--accent), and the .card corners become var(--radius).
  3. Give .card a 0.3s transition on transform: transition: transform 0.3s ease.
  4. Add a .card:hover rule that lifts the card 6px with transform: 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
Play the 7 free lessons

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 .

Write code ↓
HTML & CSS+20 XP

Lesson 28 is part of the full course. The first 7 lessons are free.

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
Play the 7 free lessons

Want more than one course?

The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.