Function

Term side of the Card.
Topic: Basics
Unit title: Function
Categories: Concept, Syntax
Explanation side of the Card

Explanation: The special commands that take one or more arguments and return a value used in a style rule. Examples include `calc()` for calculations, `url()` for linking resources and `rgb()` for color definitions. Functions provide dynamic and flexible ways to define styles.

Example

Wrongcss
.main {
  width: calc(100%-2rem);
}
Rightcss
.main {
  width: calc(100% - 2rem);
}

Without spaces -2rem is read as a negative number, not a subtraction, so the value is invalid.

Common mistakes

  • Writing calc(100%-20px) without spaces, so the whole value is invalid.
  • Adding a space between the function name and the parenthesis.

Key points

  • Operators in calc() have spaces around + and -.
  • Mixed units are combined with calc(), not written as plain math.
  • Fluid sizes use clamp() with a rem part, so they respect zoom.

Interview questions

Can I use CSS variables inside calc()?

Yes, calc(var(--gap) * 2) works. The variable must hold a value that makes sense in the calculation.

Is rgba() still needed?

No, rgb() accepts an alpha value, like rgb(0 0 0 / 50%), so rgba() is a legacy alias.

Analytics cookiesWe'd like to use analytics cookies to see which pages are useful. They're set only if you accept. Privacy Policy