Part 3: Backgrounds, Transforms, Transitions, Animations, Filters, Media Queries & More
background-color: #1a1a2e;
background-image: url('bg.png');
background-repeat: no-repeat | repeat | repeat-x | repeat-y | space | round;
background-position: center | top left | 50% 50% | 20px 40px;
background-size: cover | contain | 100% | 200px 300px | auto;
background-attachment: scroll | fixed | local;
background-clip: border-box | padding-box | content-box | text;
background-origin: border-box | padding-box | content-box;
/* Shorthand */
background: #1a1a2e url('bg.png') no-repeat center/cover;
/* Clip text to image */
.text-bg {
background: linear-gradient(45deg, red, blue);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
.multi-bg {
background:
url('top.png') no-repeat top center,
url('pattern.png') repeat,
linear-gradient(#000, #333);
/* First listed = on top */
}
/* Individual sizing */
background-size: 100px, 50px 50px, cover;
background-position: top, center, center;
/* Blend mode */
background-blend-mode: multiply | screen | overlay | darken |
lighten | color-dodge | color-burn | hard-light | soft-light |
difference | exclusion | hue | saturation | color | luminosity;
/* Basic: top to bottom */
background: linear-gradient(red, blue);
/* With direction */
background: linear-gradient(to right, red, blue);
background: linear-gradient(to bottom right, red, blue);
background: linear-gradient(45deg, red, blue);
/* Multiple color stops */
background: linear-gradient(red, yellow, green, blue);
/* With stop positions */
background: linear-gradient(red 0%, yellow 30%, blue 100%);
/* Hard stops (stripes!) */
background: linear-gradient(
red 0%, red 33%,
white 33%, white 66%,
blue 66%, blue 100%
);
/* Repeating */
background: repeating-linear-gradient(
45deg, #606dbc, #606dbc 10px,
#465298 10px, #465298 20px
);
/* Radial Gradient */
background: radial-gradient(red, blue);
background: radial-gradient(circle, red, blue);
background: radial-gradient(ellipse, red, blue);
background: radial-gradient(circle at top left, red, blue);
background: radial-gradient(circle 100px, red, blue);
/* Size keywords */
background: radial-gradient(closest-side, red, blue);
background: radial-gradient(farthest-corner, red, blue);
/* Repeating radial */
background: repeating-radial-gradient(
circle, red 0px, red 10px, blue 10px, blue 20px
);
/* Conic Gradient (pie chart!) */
background: conic-gradient(red, yellow, green, blue, red);
background: conic-gradient(from 45deg, red, blue);
background: conic-gradient(at 25% 50%, red, blue);
/* Pie chart */
background: conic-gradient(
red 0% 30%, blue 30% 60%, green 60% 100%
);
border-radius: 50%;
/* Checkerboard */
background: conic-gradient(
#000 25%, #fff 25% 50%, #000 50% 75%, #fff 75%
);
background-size: 40px 40px;
/* Border radius */
border-radius: 10px; /* All corners */
border-radius: 10px 20px; /* TL+BR TR+BL */
border-radius: 10px 20px 30px 40px; /* TL TR BR BL */
border-radius: 50%; /* Circle */
border-radius: 9999px; /* Pill shape */
/* Asymmetric corners (horizontal / vertical) */
border-radius: 50% / 30%;
border-top-left-radius: 50px 30px;
/* Border image */
border-image: linear-gradient(45deg, red, blue) 1;
border-image-source: url('border.png');
border-image-slice: 30;
border-image-repeat: round;
/* box-shadow: x y blur spread color */
box-shadow: 5px 5px 10px rgba(0,0,0,0.3);
/* Inset shadow (inner) */
box-shadow: inset 0 0 10px rgba(0,0,0,0.5);
/* Multiple shadows */
box-shadow:
0 2px 4px rgba(0,0,0,0.1),
0 8px 16px rgba(0,0,0,0.1),
inset 0 -2px 0 rgba(0,0,0,0.1);
/* Glow effect */
box-shadow: 0 0 20px #6c63ff, 0 0 40px #6c63ff44;
/* Hard shadow (no blur) */
box-shadow: 5px 5px 0 #000;
/* Shadow only (no visible element) */
box-shadow: 200px 0 0 50px red;
/* x:200px, y:0, blur:0, spread:50px = creates
a 100x100 circle 200px to the right */
/* Translate (move) */
transform: translateX(50px);
transform: translateY(-20px);
transform: translate(50px, -20px);
transform: translate(-50%, -50%); /* Center trick */
/* Rotate */
transform: rotate(45deg);
transform: rotate(-90deg);
transform: rotate(0.5turn);
/* Scale */
transform: scale(1.5); /* 150% both */
transform: scaleX(2); /* Stretch horizontal */
transform: scale(1.5, 0.5); /* X, Y */
transform: scale(-1); /* Mirror/flip */
/* Skew */
transform: skewX(10deg);
transform: skewY(-5deg);
transform: skew(10deg, -5deg);
/* Multiple transforms (applied right to left) */
transform: translate(50px, 0) rotate(45deg) scale(1.2);
/* Transform origin (pivot point) */
transform-origin: center; /* Default */
transform-origin: top left;
transform-origin: 50% 100%;
transform-origin: 0 0;
/* Enable 3D on parent */
.parent {
perspective: 800px; /* Lower = more dramatic */
perspective-origin: 50% 50%;
}
/* 3D rotations */
transform: rotateX(45deg); /* Tilt forward/back */
transform: rotateY(45deg); /* Turn left/right */
transform: rotateZ(45deg); /* Same as rotate() */
transform: rotate3d(1,1,0,45deg); /* Custom axis */
/* 3D translate */
transform: translateZ(100px); /* Move toward viewer */
transform: translate3d(x, y, z);
/* Preserve 3D in children */
transform-style: preserve-3d;
/* Hide back face (card flip) */
backface-visibility: hidden;
/* Card flip example */
.card-inner {
transform-style: preserve-3d;
transition: transform 0.6s;
}
.card:hover .card-inner {
transform: rotateY(180deg);
}
.card-front, .card-back {
backface-visibility: hidden;
}
.card-back {
transform: rotateY(180deg);
}
.element {
transition-property: all | none | transform | opacity | color | background | ...;
transition-duration: 0.3s | 300ms;
transition-delay: 0.1s;
/* Timing functions */
transition-timing-function: ease; /* Default: slow-fast-slow */
transition-timing-function: linear; /* Constant speed */
transition-timing-function: ease-in; /* Slow start */
transition-timing-function: ease-out; /* Slow end */
transition-timing-function: ease-in-out; /* Slow start + end */
transition-timing-function: cubic-bezier(0.68, -0.55, 0.27, 1.55); /* Bounce */
transition-timing-function: steps(4); /* Step animation */
/* Shorthand: property duration timing delay */
transition: all 0.3s ease;
transition: transform 0.3s ease, opacity 0.2s linear;
transition: background 0.3s ease 0.1s; /* with delay */
}
/* What CAN'T be transitioned: display, font-family, position */
/* What CAN be transitioned: color, bg, opacity, transform, width, height,
margin, padding, border, box-shadow, filter, etc. */
/* Define the animation */
@keyframes slideIn {
from {
transform: translateX(-100%);
opacity: 0;
}
to {
transform: translateX(0);
opacity: 1;
}
}
/* With percentage steps */
@keyframes bounce {
0%, 100% { transform: translateY(0); }
25% { transform: translateY(-30px); }
50% { transform: translateY(-15px); }
75% { transform: translateY(-5px); }
}
/* Apply the animation */
.element {
animation: slideIn 0.5s ease forwards;
animation: bounce 1s ease infinite;
}
animation-name: slideIn;
animation-duration: 0.5s;
animation-timing-function: ease;
animation-delay: 0.2s;
animation-iteration-count: 1 | 3 | infinite;
animation-direction: normal | reverse | alternate | alternate-reverse;
/* Fill mode (state before/after) */
animation-fill-mode: none; /* Reset after */
animation-fill-mode: forwards; /* Keep final state */
animation-fill-mode: backwards; /* Apply from state during delay */
animation-fill-mode: both;
/* Play state */
animation-play-state: running | paused;
/* Shorthand */
/* name duration timing delay count direction fill-mode */
animation: slideIn 0.5s ease 0.2s 1 normal forwards;
/* Multiple animations */
animation:
fadeIn 0.3s ease,
slideUp 0.5s ease 0.1s;
filter: blur(5px); /* Gaussian blur */
filter: brightness(1.5); /* 0=black, 1=normal, 2=2x bright */
filter: contrast(200%); /* 0%=gray, 100%=normal */
filter: grayscale(100%); /* 0%=color, 100%=grayscale */
filter: hue-rotate(90deg); /* Shift colors around wheel */
filter: invert(100%); /* Negative image */
filter: opacity(50%); /* Like opacity but composited */
filter: saturate(200%); /* 0%=desaturated, 100%=normal */
filter: sepia(100%); /* Warm vintage tone */
filter: drop-shadow(5px 5px 10px rgba(0,0,0,0.5)); /* Follows shape! */
/* Multiple filters */
filter: contrast(1.1) brightness(1.1) saturate(1.2);
/* Backdrop filter (affects content BEHIND element) */
.glass {
background: rgba(255,255,255,0.1);
backdrop-filter: blur(10px) saturate(180%);
-webkit-backdrop-filter: blur(10px) saturate(180%);
}
/* Difference between filter and backdrop-filter:
filter = blurs the element itself
backdrop = blurs what's behind the element (frosted glass) */
/* Circle */
clip-path: circle(50%);
clip-path: circle(100px at center);
clip-path: circle(40% at 50% 50%);
/* Ellipse */
clip-path: ellipse(50% 30% at center);
/* Inset (rectangle with optional rounding) */
clip-path: inset(10px 20px 30px 40px round 10px);
/* Polygon (custom shapes!) */
/* Triangle */
clip-path: polygon(50% 0%, 0% 100%, 100% 100%);
/* Pentagon */
clip-path: polygon(50% 0%, 100% 38%, 82% 100%, 18% 100%, 0% 38%);
/* Star */
clip-path: polygon(50% 0%, 61% 35%, 98% 35%, 68% 57%, 79% 91%, 50% 70%, 21% 91%, 32% 57%, 2% 35%, 39% 35%);
/* Arrow/Chevron */
clip-path: polygon(0 0, 75% 0, 100% 50%, 75% 100%, 0 100%, 25% 50%);
/* Rhombus */
clip-path: polygon(50% 0%, 100% 50%, 50% 100%, 0% 50%);
/* CSS Mask */
mask-image: url('mask.svg');
mask-image: linear-gradient(black, transparent);
mask-size: cover;
mask-repeat: no-repeat;
mask-position: center;
/* Fade-out effect */
.fade-bottom {
mask-image: linear-gradient(
to bottom, black 60%, transparent
);
}
/* shape-outside (text wrap around shapes) */
.circle-float {
float: left;
width: 200px;
height: 200px;
shape-outside: circle(50%);
clip-path: circle(50%);
}
shape-outside: circle() | ellipse() | polygon() | inset() | url();
shape-margin: 20px; /* Space between shape and text */
/* Mobile first (min-width) */
@media (min-width: 480px) { /* Phones landscape */ }
@media (min-width: 768px) { /* Tablets */ }
@media (min-width: 1024px) { /* Laptops */ }
@media (min-width: 1280px) { /* Desktops */ }
/* Desktop first (max-width) */
@media (max-width: 1279px) { /* Below desktop */ }
@media (max-width: 1023px) { /* Below laptop */ }
@media (max-width: 767px) { /* Below tablet */ }
/* Range (modern syntax) */
@media (768px <= width <= 1024px) { }
/* Combining */
@media (min-width: 768px) and (max-width: 1024px) { }
@media (min-width: 768px), (orientation: landscape) { }
/* User preferences */
@media (prefers-color-scheme: dark) {
:root { --bg: #000; --text: #fff; }
}
@media (prefers-color-scheme: light) { }
@media (prefers-reduced-motion: reduce) {
* { animation: none !important;
transition: none !important; }
}
/* Input capabilities */
@media (hover: hover) { /* Device with hover */ }
@media (hover: none) { /* Touch device */ }
@media (pointer: fine) { /* Mouse/trackpad */ }
@media (pointer: coarse){ /* Touch/stylus */ }
/* Orientation */
@media (orientation: portrait) { }
@media (orientation: landscape) { }
/* @supports - feature detection */
@supports (display: grid) {
/* Use grid layout */
}
@supports not (backdrop-filter: blur()) {
/* Fallback */
}
/* Define containment context */
.card-container {
container-type: inline-size; /* Track width */
container-name: card;
/* Shorthand */
container: card / inline-size;
}
/* Query the container */
@container card (min-width: 400px) {
.card-content {
display: grid;
grid-template-columns: 1fr 1fr;
}
}
@container (min-width: 700px) {
.card {
flex-direction: row;
}
}
/* Container query units */
font-size: 5cqi; /* 1% of container inline size */
width: 50cqb; /* 1% of container block size */
padding: 2cqw; /* 1% of container width */
margin: 1cqh; /* 1% of container height */
/* Math */
calc(100% - 40px) /* Arithmetic with mixed units */
min(90%, 800px) /* Smallest value */
max(300px, 50%) /* Largest value */
clamp(14px, 2.5vw, 24px) /* min, preferred, max */
abs(-10px) /* Absolute value */
round(2.7) /* Round to nearest */
mod(10, 3) /* Remainder */
/* Color */
rgb(255, 0, 0) /* RGB color */
rgba(255, 0, 0, 0.5) /* RGB + alpha */
hsl(0, 100%, 50%) /* HSL color */
color-mix(in srgb, red 50%, blue) /* Mix colors */
/* References */
var(--name, fallback) /* Custom property */
url('path/to/file') /* External resource */
attr(data-label) /* HTML attribute value */
env(safe-area-inset-top) /* Environment variable */
counter(section) /* Counter value */
/* Shapes */
circle(50%) /* Circle shape */
ellipse(50% 30%) /* Ellipse shape */
polygon(50% 0%, ...) /* Polygon shape */
inset(10px round 5px) /* Rectangular shape */
/* Grid */
repeat(3, 1fr) /* Repeat pattern */
minmax(200px, 1fr) /* Min/max size */
fit-content(300px) /* Content-based with max */
/* Physical -> Logical mapping */
/* (works with any writing direction) */
margin-left -> margin-inline-start
margin-right -> margin-inline-end
margin-top -> margin-block-start
margin-bottom -> margin-block-end
/* Shorthand */
margin-inline: auto; /* left + right */
margin-block: 20px; /* top + bottom */
padding-inline: 16px;
padding-block: 8px;
/* Size */
width -> inline-size
height -> block-size
min-width -> min-inline-size
max-height -> max-block-size
/* Border */
border-inline: 1px solid #ccc;
border-block-start: 2px solid red;
/* Inset (replaces top/right/bottom/left) */
inset: 0; /* All sides */
inset-inline: 20px; /* Left + right */
inset-block: 10px; /* Top + bottom */
/* Smooth scrolling */
html { scroll-behavior: smooth; }
/* Scroll snap (carousel-like) */
.scroll-container {
overflow-x: auto;
scroll-snap-type: x mandatory;
/* x|y|both mandatory|proximity */
scroll-padding: 20px;
}
.scroll-item {
scroll-snap-align: start | center | end;
scroll-margin: 10px;
}
/* Overscroll behavior */
overscroll-behavior: contain; /* No chaining */
overscroll-behavior: none; /* No bounce */
overscroll-behavior-y: contain;
/* Scroll-driven animations (new!) */
.element {
animation: fadeIn linear;
animation-timeline: scroll();
}
/* will-change: hint browser about future changes */
.animated {
will-change: transform; /* Promotes to own layer */
will-change: opacity;
will-change: transform, opacity;
}
/* IMPORTANT: Remove when animation is done. Don't overuse. */
/* contain: optimize rendering by isolating subtrees */
.widget {
contain: layout; /* Layout doesn't affect outside */
contain: paint; /* Painting doesn't leak out */
contain: size; /* Size doesn't depend on children */
contain: style; /* Counters/quotes scoped */
contain: content; /* = layout + paint */
contain: strict; /* = layout + paint + size */
}
/* content-visibility: skip rendering off-screen content */
.section {
content-visibility: auto;
contain-intrinsic-size: 0 500px; /* Estimated size */
}
transform and opacity. Use content-visibility: auto on long pages for massive rendering improvement.