Aral Code

Effects & Animation Lesson 30 of 33

CSS Transforms and Transitions

Move, rotate, scale and skew elements with transform, and make style changes happen smoothly with transition — perfect for hover effects.

When you hover over a button and it changes color instantly, it feels a bit stiff. When it changes smoothly over a fraction of a second, the page feels polished and alive. This lesson teaches two tools that work together: transform (move, turn, resize) and transition (animate the change).

transform

transform changes how an element is drawn, without affecting the layout around it. Other elements don't move out of the way.

Example The four basic transforms
<style>
  .box { display: inline-block; width: 90px; height: 90px; margin: 26px; background: #0f5c4d; color: white; border-radius: 10px; font: 13px monospace; display: inline-grid; place-items: center; }
</style>
<div class="box" style="transform: translate(20px, 10px);">translate</div>
<div class="box" style="transform: rotate(15deg);">rotate</div>
<div class="box" style="transform: scale(1.2);">scale</div>
<div class="box" style="transform: skew(10deg);">skew</div>
  • translate(x, y) — moves the element. translateY(-4px) lifts it up a little.
  • rotate(angle) — turns it, in degrees (deg). Positive is clockwise.
  • scale(n) — resizes it. 1.1 is 10% bigger, 0.9 is 10% smaller.
  • skew(angle) — slants it.

You can combine several in one line, separated by spaces: transform: translateY(-4px) scale(1.05);.

transform-origin

Rotation and scaling happen around the element's center by default. transform-origin changes that point, for example transform-origin: top left; makes it rotate like a door on its hinge.

transition

A transition makes a property change gradually instead of instantly. You add it to the element's normal state, and it plays whenever the value changes (on hover, focus, or when JavaScript changes a class).

transition: property duration timing-function delay;
transition: background-color 0.3s ease;
Example Without vs. with transition
<style>
  .btn { padding: 12px 20px; border: 0; border-radius: 10px; background: #0f5c4d; color: white; font-size: 16px; margin: 6px; cursor: pointer; }
  .btn:hover { background: #f2b33d; color: #1d2321; }
  .smooth { transition: background-color 0.4s ease, color 0.4s ease; }
</style>
<button class="btn">No transition</button>
<button class="btn smooth">With transition</button>
<p>Hover each button and compare.</p>

Duration and timing

  • Duration — 0.2s to 0.4s feels right for most hover effects. Over 0.6s feels slow.
  • Timing function — the speed curve: ease (default, starts and ends gently), linear (constant speed), ease-in, ease-out, ease-in-out.
  • Delay — waits before starting: transition: opacity 0.3s ease 0.1s;

To transition every property that changes, use transition: all 0.3s ease;. It's convenient, but listing specific properties is safer and faster.

Transform + transition: the hover lift

This is one of the most popular effects on the web: a card that rises slightly and casts a bigger shadow when hovered.

Example Cards that lift on hover
<style>
  .cards { display: flex; gap: 14px; flex-wrap: wrap; font-family: Arial, sans-serif; padding: 10px; }
  .card {
    flex: 1 1 140px;
    padding: 18px;
    border-radius: 14px;
    background: white;
    border: 1px solid #e3e0d6;
    box-shadow: 0 2px 6px rgba(0,0,0,0.06);
    transition: transform 0.25s ease, box-shadow 0.25s ease;
  }
  .card:hover {
    transform: translateY(-6px);
    box-shadow: 0 14px 28px rgba(0,0,0,0.12);
  }
</style>
<div class="cards">
  <div class="card">🧮 Math</div>
  <div class="card">🔬 Science</div>
  <div class="card">💻 ICT</div>
</div>

More hover ideas

Example Zoom, rotate and slide
<style>
  .frame { width: 150px; height: 110px; overflow: hidden; border-radius: 12px; display: inline-block; margin: 6px; background: #fdf3dc; }
  .frame img { width: 100%; height: 100%; object-fit: cover; transition: transform 0.4s ease; }
  .frame:hover img { transform: scale(1.15); }
  .icon { display: inline-block; font-size: 40px; transition: transform 0.4s ease; }
  .icon:hover { transform: rotate(360deg); }
  .arrow { font: bold 18px Arial; color: #0f5c4d; text-decoration: none; }
  .arrow span { display: inline-block; transition: transform 0.2s ease; }
  .arrow:hover span { transform: translateX(6px); }
</style>
<div class="frame"><img src="/img/halo-halo.svg" alt="Halo-halo"></div>
<span class="icon">⚙️</span>
<p><a class="arrow" href="#">Read more <span>→</span></a></p>

The zoom effect uses overflow: hidden on the frame, so the growing image is cropped neatly instead of spilling out.

What can be transitioned?

Properties with values "in between" can be animated: colors, sizes, opacity, transform, shadows, border-radius. Properties with no in-between, like display: none → block, cannot be transitioned. To fade something in, change opacity (and maybe visibility) instead.

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. Which transform makes an element 20% bigger?
2. Where should the transition property go?
3. Which property can NOT be smoothly transitioned?
4. Which two properties give the smoothest animations?

By Sir Reyel · Last updated 2026-12-04