Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST

HTML and CSS Functions & Calculations


HTML & CSS: Functions & Calculations

CSS functions let you compute values, blend colors, and respond to context without extra JavaScript.

Combine them with custom properties to build smart, responsive design systems.


Math helpers

  • calc() - add, subtract, multiply, or divide units (e.g., width: calc(100% - 2rem);).
  • min() / max() - pick the smallest or largest value.
  • clamp(min, preferred, max) - constrain a value to a responsive range.

Note: Think of calc() as a calculator in CSS, and clamp() as setting a minimum and maximum limit for a value.

If you want to read more or get an in-depth understanding, see CSS calc() and CSS clamp() in the CSS tutorial.


Example: Fluid Spacing

This example builds a responsive spacing scale with clamp() and applies it to padding, margins, and radii via custom properties.

Clamp-based scales

:root {
  --space-xs: clamp(0.5rem, 1vw, 0.75rem);
  --space-sm: clamp(0.75rem, 1.5vw, 1.125rem);
  --space-lg: clamp(1.5rem, 3vw, 2.5rem);
}
.card {
  padding: var(--space-lg);
  margin-block: var(--space-sm);
  border-radius: calc(var(--space-xs) * 2);
}
Try it Yourself »

clamp(min, preferred, max) keeps spacing within bounds: it scales with viewport width but never shrinks below the minimum or grows beyond the maximum.



Color functions

  • rgb(), hsl() accept decimals and alpha channels.
  • color-mix() blends two colors with a defined percentage.
  • color-contrast() (experimental) returns the best contrasting color.

Note: Alpha channels control transparency; a value of 0 is fully transparent and 1 is fully opaque.

If you want to read more or get an in-depth understanding, see RGB Colors and HSL Colors in the CSS tutorial.


Trigonometry & geometry

  • Modern browsers support sin(), cos(), tan(), and hypot().
  • Use them for animations, gradients, or polar positioning.
  • Pair with custom properties for interactive charts.

Environment helpers

  • env() exposes safe-area insets on mobile devices.
  • var() fetches custom property values with optional fallbacks.
  • attr() retrieves attribute values inside generated content.


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.