</>HTML & CSS
AdvancedLesson 13 of 15

Transitions & Animations – HTML & CSS

State transitions, keyframes, performance and consideration for motion sensitivity.

Transitions animate the change between two states – for example on :hover or :focus. You specify property, duration, easing and optionally a delay: transition: transform .25s ease, box-shadow .25s. On the way back, the same transition applies if it is defined on the initial state.

<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
.karte { width: 180px; padding: 16px; background: #fff; border-radius: 12px; box-shadow: 0 2px 6px rgb(0 0 0 / 15%);
         transition: transform .25s ease, box-shadow .25s ease; }
.karte:hover, .karte:focus-visible { transform: translateY(-6px) scale(1.03); box-shadow: 0 12px 24px rgb(0 0 0 / 22%); }
.btn { background: #e34f26; color: #fff; border: 0; padding: 10px 16px; border-radius: 8px; transition: background .2s; cursor: pointer; }
.btn:hover { background: #b93a17; }
</style>
<div class="karte" tabindex="0"><h3>Hover me</h3><p>Gentle lift.</p></div>
<p><button class="btn">Button with a color transition</button></p>

Keyframe animations (@keyframes + animation) run without a trigger and any number of times: duration, repetition (infinite), direction (alternate), timing and animation-delay for staggered effects. Prefer animating transform and opacity – they are computed by the graphics card and do not stutter, while width, top or margin trigger expensive re-layout.

<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
.lader { display: flex; gap: 8px; }
.lader span { width: 16px; height: 16px; border-radius: 50%; background: royalblue;
              animation: huepf .8s ease-in-out infinite alternate; }
.lader span:nth-child(2) { animation-delay: .15s; }
.lader span:nth-child(3) { animation-delay: .3s; }
@keyframes huepf { from { transform: translateY(0); opacity: .4; } to { transform: translateY(-16px); opacity: 1; } }
.spinner { width: 34px; height: 34px; margin-top: 30px; border: 4px solid #ddd; border-top-color: #e34f26;
           border-radius: 50%; animation: dreh 1s linear infinite; }
@keyframes dreh { to { transform: rotate(360deg); } }
</style>
<div class="lader"><span></span><span></span><span></span></div>
<div class="spinner" role="status" aria-label="Loading"></div>

Motion can trigger nausea or dizziness in some people. Therefore respect the system setting "Reduce motion" with prefers-reduced-motion. Also useful: use will-change sparingly and keep animations short (150–400 ms for UI feedback).

<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
.box { width: 80px; height: 80px; background: seagreen; border-radius: 12px; animation: wackel 1.2s ease-in-out infinite; }
@keyframes wackel { 0%,100% { transform: rotate(-8deg); } 50% { transform: rotate(8deg) scale(1.1); } }
@media (prefers-reduced-motion: reduce) {
  * { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}
</style>
<div class="box"></div>
<p>Enable "Reduce motion" in the operating system to see the difference.</p>
WarningChanges from display: none to block and height: auto cannot be animated (without a trick). Use opacity/visibility, grid-template-rows: 0fr → 1fr or the modern @starting-style or transition-behavior: allow-discrete.