Skip to content

CSS Functions Explained

The CSS functions that do the most work, grouped by job: compute sizes, read custom properties, mix and pick colors, paint gradients, apply filters, draw shapes, transform boxes, and pick values with logic. Each row pairs the function with a one-line example.

A CSS function takes arguments and returns a value the browser uses. The heavy lifters are calc() for arithmetic, var() for design tokens, clamp() for fluid sizing, and color-mix() for derived colors. Nest them freely: clamp(1rem, var(--fluid), 3rem) is a single responsive value.

Reference table · 62 entries
62 of 62 rows
Sizing & math
The result of an arithmetic expression, with mixed units.width: calc(100% - 2rem);
The smallest of the given values.width: min(100%, 60ch);
The largest of the given values.width: max(320px, 40vw);
A value bounded between a min, a preferred, and a max.font-size: clamp(1rem, 2vw, 1.5rem);
Reads an environment variable provided by the user agent.padding: env(safe-area-inset-top);
A size clamped to the content's intrinsic width.width: fit-content(200px);
Rounds one value to the nearest multiple of another.width: round(up, 133px, 50px);
The positive remainder of a division, always non-negative.margin: mod(100px, 30px);
The remainder of a division, keeps the sign of the dividend.margin: rem(-100px, 30px);
The absolute value.translateX(abs(-10px));
-1, 0, or 1, matching the sign of the value.padding: sign(-5px) * 10px;
Grid
A size range for grid tracks, between a min and a max.grid-template-columns: minmax(100px, 1fr) minmax(0, 2fr);
Repeats a track pattern a number of times or until filled.grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
Logic & conditions
Picks one of two values based on a condition.font-size: if(media(width > 600px), 1.25rem, 1rem);
True when a media query matches, for use inside if().padding: if(media(width > 40rem), 2rem, 1rem);
True when the browser supports a declaration, for use inside if().display: if(supports(color: oklch(0 0 0)), oklch(0 0 0), #333);
True when the selector matches, for use inside if().font-weight: if(selector(:hover), 700, 400);
Custom properties
The value of a custom property, with an optional fallback.color: var(--accent, #333);
Color
A color from red, green, blue (and alpha) channels.color: rgb(59 130 246 / 0.8);
A color from hue, saturation, and lightness.color: hsl(210 80% 50%);
A color in perceptual OKLCH space (consistent lightness).color: oklch(0.7 0.15 250);
A blend of two colors in a given color space.color: color-mix(in srgb, red 40%, blue);
The option from a list that contrasts best with a base.color: color-contrast(#fff vs #000, #777);
A color from hue, whiteness, and blackness.color: hwb(210 10% 20%);
A color in CIELAB space (lightness, a, b axes).color: lab(52% 40 59);
A color in CIELAB space (lightness, chroma, hue).color: lch(52% 49 24);
A color in any predefined color space by coordinates.color: color(display-p3 1 0.4 0.2);
The first color in light scheme, the second in dark.color: light-dark(#1a1a1a, #f5f5f5);
Gradients & images
A gradient along a straight line between color stops.background: linear-gradient(90deg, #4b9fff, #2ee6a6);
A gradient that radiates outward from a point.background: radial-gradient(circle, #fff, #000);
A gradient that sweeps around a center point.background: conic-gradient(red, yellow, red);
Loads an external resource: image, font, or SVG.background-image: url(hero.webp);
Picks the best image for the device's resolution.background-image: image-set("hero.webp" 1x, "hero@2x.webp" 2x);
Blends two images at a given percentage.background-image: cross-fade(30%, url(a.webp), url(b.webp));
Filter & shape
Pixel filters used inside the filter property.filter: blur(4px) brightness(1.1);
An SVG path string, used to draw a clip or shape.clip-path: path('M10 10 H 90 V 90 Z');
Reads an HTML attribute; limited use outside content.content: attr(data-label);
Adjusts the contrast of the element.filter: contrast(1.4);
Paints a shadow that follows the element's alpha shape.filter: drop-shadow(0 4px 8px rgb(0 0 0 / 0.3));
Converts the element to grayscale.filter: grayscale(100%);
Inverts the element's colors.filter: invert(1);
Applies a sepia tone.filter: sepia(0.6);
Rotates the hue of the element's colors.filter: hue-rotate(90deg);
Sets the element's opacity via filter.filter: opacity(0.5);
A circle shape for clip-path or shape-outside.clip-path: circle(40% at 50% 50%);
An ellipse shape for clip-path or shape-outside.clip-path: ellipse(50% 35% at 50% 50%);
A rounded rectangle shape for clip-path.clip-path: inset(10px 10px 10px 10px round 8px);
A polygon shape from a list of points.clip-path: polygon(50% 0, 100% 100%, 0 100%);
Transform
Moves the element along X and Y.transform: translate(10px, 20px);
Rotates the element around its center.transform: rotate(45deg);
Scales the element along X and Y.transform: scale(1.25);
Skews the element along X and Y.transform: skew(10deg, 0deg);
One 2D transform from six matrix values.transform: matrix(1, 0, 0.5, 1, 10, 0);
Sets a 3D perspective depth for the element's children.transform: perspective(600px) rotateY(20deg);
Animation & counters
An easing curve from four control points.transition-timing-function: cubic-bezier(0.65, 0, 0.35, 1);
An easing that jumps in equal steps.animation-timing-function: steps(8, end);
The current value of a CSS counter, as content.content: counter(chapter);
The values of a counter and all its ancestors, joined by a string.content: counters(item, ".");
0 to 1 as a value progresses between a start and an end.opacity: progress(scroll(), 0, 50vh);
A random number from a range, fixed per element.rotate: random(from -5deg to 5deg);
The sine of an angle, for calc expressions.translateY(calc(1rem * sin(45deg)));
The cosine of an angle, for calc expressions.translateX(calc(1rem * cos(45deg)));