CSS Tutorial menu

CSS Transitions and Animations

CSS can change styles smoothly over time, which makes a page feel alive and easier to use.

Transitions

A transition makes a change happen slowly instead of instantly. You say which property should change and how long it should take.

Example

html
<!DOCTYPE html>
<html>
<head>
<style>
.btn {
  background-color: #264de4;
  color: white;
  padding: 12px 24px;
  border: none;
  border-radius: 6px;
  transition: background-color 0.4s ease;
}
.btn:hover {
  background-color: #f97316;
}
</style>
</head>
<body>

<button class="btn">Hover over me</button>

</body>
</html>
Try it Yourself »

The transition shorthand takes up to four values:

PartExampleMeaning
Propertybackground-colorWhat changes (or all)
Duration0.4sHow long it takes
Timing functionease, linear, ease-in-outThe speed curve
Delay0.1sWait before starting

Transform

transform moves, resizes or rotates an element without affecting the layout around it. It works very well with transitions.

FunctionExampleEffect
translate()translate(10px, -5px)Moves it
scale()scale(1.1)Makes it bigger or smaller
rotate()rotate(45deg)Turns it

Example

html
<!DOCTYPE html>
<html>
<head>
<style>
.card {
  width: 180px;
  padding: 20px;
  background-color: white;
  border: 1px solid #dddddd;
  border-radius: 10px;
  transition: transform 0.3s, box-shadow 0.3s;
}
.card:hover {
  transform: translateY(-8px) scale(1.03);
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.15);
}
</style>
</head>
<body>

<div class="card">
  <h3>Laptop Bag</h3>
  <p>Rs 2,499</p>
</div>

</body>
</html>
Try it Yourself »

Keyframe Animations

A transition needs a trigger, like hover. An animation can play by itself and repeat. First describe the steps with @keyframes, then attach it with animation.

Example

html
<!DOCTYPE html>
<html>
<head>
<style>
@keyframes pulse {
  0%   { transform: scale(1); }
  50%  { transform: scale(1.15); }
  100% { transform: scale(1); }
}
@keyframes spin {
  from { transform: rotate(0deg); }
  to   { transform: rotate(360deg); }
}
.offer {
  display: inline-block;
  background-color: crimson;
  color: white;
  padding: 10px 16px;
  border-radius: 20px;
  animation: pulse 1.5s ease-in-out infinite;
}
.loader {
  width: 40px;
  height: 40px;
  margin-top: 20px;
  border: 5px solid #dddddd;
  border-top-color: #264de4;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}
</style>
</head>
<body>

<span class="offer">50% OFF today</span>
<div class="loader"></div>

</body>
</html>
Try it Yourself »

Animation Properties

PropertyExampleMeaning
animation-namepulseThe @keyframes name
animation-duration1.5sLength of one cycle
animation-timing-functionease-in-outSpeed curve
animation-delay0.5sWait before starting
animation-iteration-count3 or infiniteHow many times it plays
animation-directionalternatePlay backwards on every other cycle

Use Movement Carefully

  • Keep most transitions short: 0.2s to 0.5s
  • Animate transform and opacity; they are the smoothest for the browser
  • Do not make important text move all the time

Some people feel sick from moving screens. Respect their setting with @media (prefers-reduced-motion: reduce) { * { animation: none; transition: none; } }.

Exercise

Exercise

Which CSS rule defines the steps of an animation?

Learn CSS with a teacher

Join the Web Development course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp