CSS – Layouts & Cool Effects
intermediate25 XP

Hover Effects & Transitions – Bringing Pages to Life!

Add smooth animations when users hover over buttons, make things grow, change colour and slide — without any JavaScript!

Hover Effects & Transitions — Wow Your Visitors! ✨

Great websites don't just sit there — they respond when you interact with them. CSS lets you add beautiful animations without writing a single line of JavaScript!

The :hover Pseudo-class

:hover styles apply when a user's mouse is over an element:

CSS
button {
  background-color: purple;
  color: white;
}

button:hover {
  background-color: rebeccapurple;  /* changes on hover */
}

Transitions — Smooth Changes

Without transitions, changes are instant and jarring. With transitions, they flow smoothly:

CSS
button {
  background-color: #7c3aed;
  color: white;
  padding: 12px 24px;
  border-radius: 8px;
  border: none;
  font-size: 16px;
  
  /* Add this to make changes smooth! */
  transition: all 0.3s ease;
}

button:hover {
  background-color: #5b21b6;
  transform: scale(1.05);      /* grows 5% bigger */
  box-shadow: 0 8px 25px rgba(124, 58, 237, 0.4);
}

transition: all 0.3s ease means:

  • all — animate all changing properties
  • 0.3s — takes 0.3 seconds
  • ease — starts fast, slows down smoothly

Transform — Move, Scale, Rotate!

CSS
.card:hover {
  transform: translateY(-8px);     /* moves UP 8 pixels */
  transform: scale(1.1);           /* grows 10% */
  transform: rotate(5deg);         /* tilts 5 degrees */
}

Box Shadow — Depth Effect

CSS
.card {
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);     /* subtle shadow */
  transition: box-shadow 0.3s ease;
}

.card:hover {
  box-shadow: 0 16px 40px rgba(0,0,0,0.2);   /* bigger on hover! */
}

🌟 Animated Navigation Link

CSS
.nav-link {
  color: white;
  text-decoration: none;
  position: relative;
  padding-bottom: 4px;
}

/* Create an underline that slides in */
.nav-link::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 0;                    /* starts at 0 width */
  height: 2px;
  background-color: #fbbf24;   /* gold underline */
  transition: width 0.3s ease;
}

.nav-link:hover::after {
  width: 100%;                 /* expands to full width! */
}

Gradient Backgrounds

CSS
.hero {
  /* Left to right gradient */
  background: linear-gradient(to right, #7c3aed, #db2777);
  
  /* Top to bottom gradient */
  background: linear-gradient(to bottom, #1e1b4b, #312e81);
  
  /* Diagonal */
  background: linear-gradient(135deg, #667eea, #764ba2);
}

These gradients look exactly like the backgrounds on Spotify, Discord, and Netflix! 🎵

Quick check

01/3

Which CSS selector applies styles when a user moves their mouse over an element?