Skip to content
Created: May 12, 2020

Introduction ​

"CSS function" refers to the built-in functions in CSS (such as calc(), var(), rgb(), url(), etc.), which are commonly used for dynamic calculations, color processing, unit conversion, gradients, deformations, filtering, selector control, etc..

Below I will help you systematically summarize and classify all common CSS functions so that you can quickly grasp the key points and high-frequency interview points.

1. Numerical and unit calculation functions (calculation class) ​

1️⃣ calc() ​

  • Function: Allows dynamic calculations in CSS.

  • Common syntax:

    css
    width: calc(100% - 50px);
    padding: calc(var(--gap) / 2);
  • ✅ Supports + - * / four arithmetic operations

  • ✅ Can mix different units (such as %, px, rem)

2️⃣ min() / max() / clamp() ​

Dynamic range function in CSS, a responsive design artifact!

FunctionMeaningExample
min(a, b)Take the minimum valuewidth: min(100%, 600px);
max(a, b)Get the maximum valuewidth: max(50%, 300px);
clamp(min, preferred, max)Limit range (recommended value)font-size: clamp(14px, 2vw, 20px);

✅ clamp() is a common technique for responsive fonts and container resizing.

2. Color and gradient functions ​

1️⃣ rgb() / rgba() ​

  • Color definition (red, green, blue, transparency)

    css
    color: rgba(255, 0, 0, 0.8);

2️⃣ hsl() / hsla() ​

  • Define colors by hue, saturation, and lightness.

    css
    color: hsl(120, 50%, 50%);

3️⃣ color-mix() ​

  • Mix two colors (supported by modern browsers)

    css
    color: color-mix(in srgb, red 30%, blue);

4️⃣ Gradient function ​

FunctionDescriptionExample
linear-gradient()Linear gradientbackground: linear-gradient(45deg, red, blue);
radial-gradient()Radial gradientbackground: radial-gradient(circle, yellow, red);
conic-gradient()Conical gradientbackground: conic-gradient(from 0deg, red, yellow, green);
repeating-linear-gradient()Repeating linear gradientbackground: repeating-linear-gradient(90deg, red 0, red 10px, blue 10px, blue 20px);

3. Transformation and animation functions (Transform class) ​

These functions usually appear in the transform: attribute and are used for 2D/3D transformations.

FunctionEffectExample
translate(x, y)Translationtransform: translate(100px, 50px);
translateX() / translateY() / translateZ()Single direction translationtransform: translateX(20px);
scale(x, y)Scaletransform: scale(1.2, 1.2);
rotate(angle)Rotationtransform: rotate(45deg);
skew(x-angle, y-angle)skewtransform: skew(10deg, 5deg);
matrix(a, b, c, d, tx, ty)2D matrix transformationtransform: matrix(1, 0, 0, 1, 50, 50);
perspective(n)Set perspective depthtransform: perspective(600px) rotateY(30deg);

✅ These functions can be used in combination:

css
transform: translateX(100px) rotate(45deg) scale(1.2);

4. Filter function (Filter class) ​

Visual effects for images, backgrounds, elements.

FunctionDescriptionExample
blur(px)blurfilter: blur(5px);
brightness(%)Brightnessfilter: brightness(120%);
contrast(%)Contrastfilter: contrast(150%);
grayscale(%)Grayscalefilter: grayscale(100%);
hue-rotate(deg)Hue rotationfilter: hue-rotate(90deg);
invert(%)Invert colorfilter: invert(100%);
saturate(%)Saturationfilter: saturate(200%);
sepia(%)sepiafilter: sepia(80%);
drop-shadow(x y blur color)Shadowfilter: drop-shadow(2px 2px 5px gray);

5. Image and resource functions ​

FunctionPurposeExample
url()Introduce resources such as images and fontsbackground: url('./bg.png');
image-set()Set images with different resolutionsbackground: image-set(url(img1.png) 1x, url(img2.png) 2x);
cross-fade()Image blending (some browsers)background: cross-fade(url(a.png), url(b.png), 50%);

6. Custom attributes and variable functions ​

FunctionDescriptionExample
var(--name, fallback)Call custom variablescolor: var(--main-color, black);
env()System environment variables (such as safe area)padding-bottom: env(safe-area-inset-bottom);

7. String and counting functions ​

FunctionPurposeExample
attr()Get element attribute valuecontent: attr(data-label);
counter() / counters()Countercontent: counter(item) ". ";

8. Shape and clipping functions (Shape/Clip-path) ​

FunctionDescriptionExample
circle()Circle clippingclip-path: circle(50% at 50% 50%);
ellipse()Ellipse clippingclip-path: ellipse(40% 60% at 50% 50%);
polygon()Polygon clippingclip-path: polygon(0 0, 100% 0, 100% 100%);
inset()Padding clippingclip-path: inset(10% 10% 10% 10%);
path()Custom path (svg)clip-path: path('M10 10 H 90 V 90 H 10 Z');

9. Background and mask functions ​

FunctionFunctionExample
repeating-radial-gradient()Repeating radial gradientbackground: repeating-radial-gradient(circle, red, yellow 10px);
mask-image()Mask imagemask-image: linear-gradient(to right, transparent, black);

10. Time and step function (animation function) ​

FunctionDescriptionExample
cubic-bezier(x1, y1, x2, y2)Custom easing functiontransition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
`steps(n, startend)`step by step animationanimation-timing-function: steps(5, end);

🔍 Summary of frequently asked questions in interviews ​

TypeHigh Frequency FunctionDescription
Dynamic layoutcalc(), clamp(), min()Responsive calculation
Animation optimizationtransform(), opacity + cubic-bezier()High-performance animation
Color controlrgba(), hsl(), color-mix()Flexible color matching
Filter effectsfilter() seriesImage optimization
Variable controlvar(), env()Theme and adaptation
Clip shapeclip-path()Personalized UI