Master CSS From Scratch

Clear, interactive, and structured CSS lessons designed for beginners.

CSS Functions

CSS functions allow you to perform calculations, create flexible layouts, control responsive values, and dynamically combine CSS values.


What are CSS Functions?

CSS functions are special expressions that return a value which can be used by CSS properties.

They are useful when a value needs to be calculated, limited within a range, reused, or generated dynamically.

Interactive CSS Functions Visual

Hover over the visual cards below. The layout uses CSS functions such as clamp() and calc() to create flexible sizing.

+
calc() Calculate values
↓
min() Choose the smaller value
↑
max() Choose the larger value
↔
clamp() Keep a value within a range
Creative colorful workspace representing flexible CSS design
Flexible CSS Design

CSS functions make layouts more flexible and responsive.

Basic CSS Function Syntax

property: function(value);

Simple Explanation

A CSS function receives one or more values and returns a result that can be used by a CSS property.

For example, calc() can calculate a size, while clamp() can keep a value between a minimum and maximum.

How CSS Functions Work

CSS Property
width
→
Function
calc()
→
Calculated Value
760px

1. calc()

The calc() function allows you to perform calculations using CSS values.

.container {
    width: calc(100% - 40px);
}

How it works

The width becomes the available width minus 40px.

Output

calc(100% - 40px)

Resize the browser to see the container remain flexible.

2. min()

The min() function returns the smallest value from the values provided.

.box {
    width: min(90%, 600px);
}

Meaning

The width will use whichever is smaller: 90% or 600px.

Output

min(90%, 600px)

3. max()

The max() function returns the largest value from the values provided.

.box {
    width: max(50%, 300px);
}

Meaning

The width will use whichever is larger: 50% or 300px.

Output

max(50%, 300px)

4. clamp()

The clamp() function keeps a value between a minimum, preferred, and maximum value.

.heading {
    font-size: clamp(28px, 5vw, 52px);
}

clamp() Structure

clamp(minimum, preferred, maximum)

The value should not go below the minimum or above the maximum while the preferred value adapts to the viewport.

Output

Responsive Heading

Resize the browser window to see the heading adapt.

5. var()

The var() function is used to access a CSS custom property.

:root {
    --primary-color: #2563eb;
}

.button {
    background-color: var(--primary-color);
}

Why use var()?

It allows you to reuse values stored in CSS variables.

Output

6. Combining CSS Functions

CSS functions can also be combined to create more flexible layouts.

How it works

First, calc() performs the calculation. Then min() compares the calculated value with 1000px and uses the smaller result.

7. Color Functions

CSS also provides functions for creating and modifying colors. Modern CSS supports functions such as rgb(), hsl(), rgba(), and hsla().

.box {
    background-color: rgb(37, 99, 235);
}

.card {
    background-color: rgba(37, 99, 235, 0.15);
}

Output

rgb()
rgba()
hsl()

Common CSS Functions

Function Purpose Example
calc() Performs calculations calc(100% - 40px)
min() Returns the smaller value min(90%, 600px)
max() Returns the larger value max(50%, 300px)
clamp() Limits a value within a range clamp(20px, 5vw, 50px)
var() Accesses a CSS variable var(--primary)
rgb() Creates a color using RGB values rgb(37, 99, 235)
hsl() Creates a color using HSL values hsl(217, 83%, 53%)

Practical Example: Responsive Card

.responsive-card {
    width: min(90%, 650px);
    padding: clamp(16px, 4vw, 30px);
    font-size: clamp(16px, 2vw, 22px);
}

Output

CSS

Flexible Course Card

This card uses CSS functions to adapt its width, padding, and typography.

Benefits of CSS Functions

Flexible Adapt values to available space
Responsive Create layouts for different screens
Reusable Combine with CSS variables
Maintainable Reduce unnecessary media queries

Best Practices

  • Use calc() when values need mathematical calculations.
  • Use min() and max() when values need upper or lower constraints.
  • Use clamp() for fluid responsive values.
  • Combine functions with CSS variables when creating reusable design systems.
  • Keep calculations understandable and easy to maintain.

Important Note

CSS functions are especially useful for responsive design because they can reduce the need for many breakpoint-specific values.

Summary

CSS functions allow developers to calculate, compare, limit, and reuse CSS values. Important functions include calc(), min(), max(), clamp(), var(), rgb(), and hsl(). They are especially useful for creating flexible and responsive designs.