- Home
- /
- Tutorials
- /
- CSS Tutorial
- /
- CSS Functions
CSS Reference
CSS Functions
Recognize common CSS functions for values, colors, images, transforms, filters, shapes, counters, and environment data. It maps functions for variables, math, colors, images, transforms, filters, shapes, Grid sizing, counters, attributes, and environment values.
CSS functions are the bits of a value that end in brackets: calc(), var(), rgb(), url(). Each one works out part of a value for you, from a calculation to a color mix. This page is a map of the ones you will meet.
Functions can be combined: a custom property can be used inside a calculation, and a calculation inside a color. The details of each family are in their own lessons, such as CSS Math Functions and Modern Colors.
The main families
- Variables and environment:
var(),env(),attr() - Math:
calc(),min(),max(),clamp(),round() - Color:
rgb(),hsl(),oklch(),color(),color-mix(),light-dark() - Images:
url(),image-set(), gradient functions - Transforms:
translate(),rotate(),scale(),matrix(),perspective() - Filters:
blur(),brightness(),contrast(),drop-shadow() - Shapes:
circle(),ellipse(),inset(),polygon(),path() - Grid and sizing:
repeat(),minmax(),fit-content() - Generated values:
counter(),counters()
Example
css
.hero {
padding: max(1rem, env(safe-area-inset-left));
width: min(100% - 2rem, 72rem);
background-image: image-set(
url("hero.webp") type("image/webp") 1x,
url("hero@2x.webp") type("image/webp") 2x
);
}In one rule: padding that also respects a phone's notch (env()), a width that is never wider than 72rem and never touches the screen edges (min()), and a background image that picks the right file format (image-set()).
Valid name, wrong place
A function only works where its property accepts that kind of value. rotate(45deg) is fine inside transform and meaningless inside width. When a value is invalid, the browser drops the whole declaration silently, so the element simply looks unstyled. If a rule with a function seems to do nothing, check it in DevTools: it will be crossed out with a warning icon.
Common CSS Functions
| Function | Category | Example |
|---|---|---|
| var() | Custom properties | var(--accent, blue) |
| clamp() | Math | clamp(1rem, 2vw, 2rem) |
| color-mix() | Color | color-mix(in oklch, blue, white) |
| linear-gradient() | Image | linear-gradient(to right, red, blue) |
| translate() | Transform | translate(1rem, 0) |
Complete Example
Complete runnable example
html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>CSS Functions example</title>
<style>
* { box-sizing: border-box; }
body {
margin: 0;
padding: 2rem;
font-family: system-ui, sans-serif;
line-height: 1.5;
}
main { max-width: 70rem; margin-inline: auto; }
article, section, aside, form, .card, .panel {
padding: 1rem;
border: 1px solid #cbd5e1;
border-radius: 0.6rem;
}
.hero {
padding: max(1rem, env(safe-area-inset-left));
width: min(100% - 2rem, 72rem);
background-image: image-set(
url("https://ik.imagekit.io/html5andcss3/frontend-media/html_example.webp") type("image/webp") 1x,
url("https://ik.imagekit.io/html5andcss3/frontend-media/html_example.webp?tr=w-1600") type("image/webp") 2x
);
}
</style>
</head>
<body>
<main class="hero"><h1>CSS functions</h1><p>This container uses min(), max(), env(), and image-set().</p></main>
</body>
</html>Browser Support
Feature | Chrome | Edge | Firefox | Safari |
|---|---|---|---|---|
| calc() | 26 | 12 | 16 | 7 |
| color-mix() | 111 | 111 | 113 | 16.2 |
Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8. A partial-support note is included where it changes how the example behaves.
Good to know
- Spaces matter inside
calc():calc(100% - 2rem)works,calc(100%-2rem)does not. - Newer functions like
color-mix()andlight-dark()are widely supported now, but if your layout depends on one, give a plain value first as a fallback.
CSS Functions exercises
calc()
Make the card calc(100px + 20px) wide.
The HTML your code works on
<style>
.wrap { width: 600px; }
</style>
<div class="wrap">
<div class="card">Card</div>
</div>Result
