Week 6: CSS Animations & Variables
CSS | Transitions, Keyframes, Custom Properties
Al-Aqsa University
CSS Animations & Variables
Transitions, Transforms, Keyframes & Custom Properties
Week 06
1. CSS Transitions Smooth State Changes
Transition Properties
Transitions smoothly animate property changes over a specified duration. They require a trigger (like :hover).
CSS - Transition Properties
/* Individual properties */
.box {
transition-property: background-color, transform;
transition-duration: 0.3s;
transition-timing-function: ease;
transition-delay: 0s;
}
/* Shorthand: property duration timing-function delay */
.box {
transition: all 0.3s ease;
}
/* Multiple transitions with different timings */
.box {
transition: background 0.3s ease,
transform 0.5s ease-out,
box-shadow 0.3s ease 0.1s; /* 0.1s delay */
}
/* Common pattern: define base + hover */
.button {
background: #4caf50;
transform: translateY(0);
transition: all 0.3s ease;
}
.button:hover {
background: #388e3c;
transform: translateY(-3px);
box-shadow: 0 5px 15px rgba(76,175,80,0.4);
}
Live Transition Demo (hover the button)
Timing Functions
| Function | Behavior |
ease | Slow start, fast middle, slow end (default) |
linear | Constant speed throughout |
ease-in | Slow start, accelerates |
ease-out | Fast start, decelerates |
ease-in-out | Slow start and end |
cubic-bezier(n,n,n,n) | Custom curve (use cubic-bezier.com) |
CSS - Timing Functions
.ease { transition: all 0.5s ease; }
.linear { transition: all 0.5s linear; }
.ease-in { transition: all 0.5s ease-in; }
.ease-out { transition: all 0.5s ease-out; }
.bounce { transition: all 0.5s cubic-bezier(0.68, -0.55, 0.27, 1.55); }
.spring { transition: all 0.6s cubic-bezier(0.34, 1.56, 0.64, 1); }
2. CSS Transform Move, Rotate, Scale, Skew
2D Transform Functions
CSS - Transform Functions
/* translate: move an element */
.move {
transform: translate(20px, 10px); /* x, y */
transform: translateX(50px); /* horizontal only */
transform: translateY(-20px); /* vertical only */
}
/* rotate: spin an element */
.spin {
transform: rotate(45deg); /* clockwise */
transform: rotate(-90deg); /* counter-clockwise */
}
/* scale: resize an element */
.grow {
transform: scale(1.5); /* 150% both axes */
transform: scale(2, 0.5); /* x: 200%, y: 50% */
transform: scaleX(1.5); /* horizontal only */
}
/* skew: slant an element */
.slant {
transform: skew(10deg, 5deg); /* x, y */
transform: skewX(15deg); /* horizontal only */
}
/* Combine multiple transforms */
.combined {
transform: rotate(5deg) scale(1.1) translateY(-10px);
}
/* transform-origin: pivot point */
.pivot {
transform-origin: top left; /* default: center */
transform: rotate(20deg);
}
Transform Demos (hover each box)
Translate
Rotate
Scale
Skew
Combined
3. CSS Animations @keyframes
Defining Animations
Unlike transitions, animations can run automatically, loop infinitely, and have multiple steps.
CSS - @keyframes Definition
/* Define the animation with @keyframes */
@keyframes fadeInUp {
from {
opacity: 0;
transform: translateY(30px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
/* Multiple keyframe stops */
@keyframes colorPulse {
0% { background: #4caf50; transform: scale(1); }
25% { background: #2196f3; }
50% { background: #9c27b0; transform: scale(1.1); }
75% { background: #ff9800; }
100% { background: #4caf50; transform: scale(1); }
}
/* Bounce animation */
@keyframes bounce {
0%, 100% { transform: translateY(0); }
50% { transform: translateY(-20px); }
}
Animation Properties
CSS - Animation Properties
.element {
/* Individual properties */
animation-name: fadeInUp;
animation-duration: 1s;
animation-timing-function: ease-out;
animation-delay: 0.5s;
animation-iteration-count: 1; /* number or infinite */
animation-direction: normal; /* normal|reverse|alternate|alternate-reverse */
animation-fill-mode: forwards; /* none|forwards|backwards|both */
animation-play-state: running; /* running|paused */
/* Shorthand: name duration timing delay count direction fill-mode */
animation: fadeInUp 1s ease-out 0.5s 1 normal forwards;
}
/* Common patterns */
.fade-in {
animation: fadeInUp 0.8s ease-out forwards;
}
.loop-pulse {
animation: pulse 2s ease-in-out infinite;
}
.spin-forever {
animation: spin 3s linear infinite;
}
/* Multiple animations on one element */
.multi {
animation: fadeInUp 1s ease-out,
pulse 2s ease-in-out 1s infinite;
}
/* Pause animation on hover */
.spinning:hover {
animation-play-state: paused;
}
| fill-mode | Behavior |
none | Element returns to original state after animation |
forwards | Element keeps the styles from the last keyframe |
backwards | Element gets the styles from the first keyframe during delay |
both | Both forwards and backwards behavior |
Live Animation Demos
Pulse
infinite
Spin
linear
Bounce
ease
Color
4 colors
Typing Effect (CSS Only)
Hello, I am a coder!
4. CSS Custom Properties (Variables) Reusable Values
Defining & Using Variables
CSS - Custom Properties
/* Define variables on :root (global) */
:root {
--primary-color: #4caf50;
--secondary-color: #2196f3;
--bg-dark: #0f0f1a;
--bg-card: #1a1a2e;
--text-primary: #e0e0e0;
--text-secondary: #888;
--border-radius: 12px;
--shadow: 0 5px 15px rgba(0,0,0,0.3);
--transition: all 0.3s ease;
--font-main: 'Inter', sans-serif;
--spacing-sm: 8px;
--spacing-md: 16px;
--spacing-lg: 32px;
}
/* Use variables with var() */
.card {
background: var(--bg-card);
border-radius: var(--border-radius);
box-shadow: var(--shadow);
padding: var(--spacing-lg);
transition: var(--transition);
}
.card h3 {
color: var(--primary-color);
}
/* Fallback value (if variable not defined) */
.box {
color: var(--accent-color, #ff9800); /* uses #ff9800 if --accent-color not set */
}
Variables for Theming (Dark/Light Mode)
CSS - Theme Switching
/* Dark theme (default) */
:root {
--bg: #0f0f1a;
--bg-card: #1a1a2e;
--text: #e0e0e0;
--primary: #4caf50;
--border: rgba(255,255,255,0.06);
}
/* Light theme (override with a class) */
.light-theme {
--bg: #f5f5f5;
--bg-card: #ffffff;
--text: #333333;
--primary: #2e7d32;
--border: rgba(0,0,0,0.1);
}
/* Using system preference */
@media (prefers-color-scheme: light) {
:root {
--bg: #f5f5f5;
--text: #333333;
}
}
/* Local variable override (cascading) */
.special-section {
--primary: #ff9800; /* override just here */
}
.special-section .btn {
background: var(--primary); /* uses #ff9800 */
}
Theme Demo (Hover to Switch Theme)
Variable-Powered Theme
Hover to see variables change in real-time
Why Use Variables?
CSS variables reduce repetition, make global changes easy (change one variable to update the entire site), enable theming, and can be manipulated with JavaScript. They cascade like regular CSS properties.
5. CSS Filters Image & Element Effects
Filter Functions
CSS - Filters
/* Individual filter functions */
.blur { filter: blur(5px); }
.bright { filter: brightness(1.5); } /* 1 = normal, >1 = brighter */
.dim { filter: brightness(0.5); } /* <1 = darker */
.contrast { filter: contrast(1.5); } /* increase contrast */
.gray { filter: grayscale(100%); } /* black & white */
.sepia { filter: sepia(80%); } /* vintage look */
.saturate { filter: saturate(2); } /* vivid colors */
.invert { filter: invert(100%); } /* invert colors */
.hue { filter: hue-rotate(90deg); } /* shift colors */
.shadow { filter: drop-shadow(5px 5px 10px rgba(0,0,0,0.5)); }
/* Combine multiple filters */
.vintage {
filter: sepia(50%) contrast(1.2) brightness(0.9);
}
/* Common pattern: grayscale that removes on hover */
.team-photo {
filter: grayscale(100%);
transition: filter 0.3s ease;
}
.team-photo:hover {
filter: grayscale(0%);
}
Filter Examples
Original
grayscale(100%)
sepia(80%)
blur(3px)
brightness(1.5)
hue-rotate(90deg)
6. Clip Path Clipping Shapes
clip-path Property
The clip-path property clips an element to a specific shape. Anything outside the shape is hidden.
CSS - Clip Path
/* Circle */
.circle {
clip-path: circle(50% at 50% 50%); /* radius at position */
}
/* Ellipse */
.ellipse {
clip-path: ellipse(40% 60% at 50% 50%);
}
/* Polygon (custom shapes) */
.triangle {
clip-path: polygon(50% 0%, 0% 100%, 100% 100%);
}
.hexagon {
clip-path: polygon(25% 0%, 75% 0%, 100% 50%, 75% 100%, 25% 100%, 0% 50%);
}
.diamond {
clip-path: polygon(50% 0%, 100% 50%, 50% 100%, 0% 50%);
}
/* Inset (rectangle with optional rounded corners) */
.inset {
clip-path: inset(10% 20% 10% 20% round 15px);
}
/* Animated clip-path */
.reveal {
clip-path: circle(0% at 50% 50%);
transition: clip-path 0.5s ease;
}
.reveal:hover {
clip-path: circle(100% at 50% 50%);
}
Clip Path Shapes
Circle
Triangle
Diamond
Hexagon
Star
Clip Path Generator
Use the online tool bennettfeely.com/clippy to visually create clip-path polygons and copy the CSS code. It is much easier than calculating coordinates manually.