Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO 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 CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST TOOLS

CSS Tutorial

CSS HOME CSS Introduction CSS Syntax CSS Selectors CSS How To CSS Comments CSS Errors CSS Colors CSS Backgrounds CSS Borders CSS Margins CSS Padding CSS Height / Width CSS Box Model CSS Outline CSS Text CSS Fonts CSS Icons CSS Links CSS Lists CSS Tables CSS Display CSS Max-width CSS Position CSS Position Offsets CSS Z-index CSS Overflow CSS Float CSS Inline-block CSS Align CSS Combinators CSS Pseudo-classes CSS Pseudo-elements CSS Opacity CSS Navigation Bars CSS Dropdowns CSS Image Gallery CSS Image Sprites CSS Attribute Selectors CSS Forms CSS Counters CSS Units CSS Inheritance CSS Specificity CSS !important CSS Math Functions CSS Optimization CSS Accessibility CSS Website Layout

CSS Advanced

CSS Rounded Corners CSS Border Images CSS Backgrounds CSS Colors CSS Gradients CSS Shadows CSS Text Effects CSS Custom Fonts CSS 2D Transforms CSS 3D Transforms CSS Transitions CSS Animations CSS Tooltips CSS Image Styling CSS Image Modal CSS Image Centering CSS Image Filters CSS Image Shapes CSS object-fit CSS object-position CSS Masking CSS Buttons CSS Pagination CSS Multiple Columns CSS User Interface CSS Variables CSS @property CSS Box Sizing CSS Functions CSS Media Queries

CSS Flexbox

Flexbox Intro Flex Container Flex Items Flex Responsive

CSS Grid

Grid Intro Grid Container Grid Items Grid 12-column Layout CSS @supports

CSS Responsive

RWD Intro RWD Viewport RWD Grid View RWD Media Queries RWD Images RWD Videos RWD Frameworks RWD Templates

CSS Cert

CSS Certificate

CSS SASS

SASS Tutorial

CSS Examples

CSS Templates CSS Examples CSS Editor CSS Snippets CSS Quiz CSS Exercises CSS Code Challenges CSS Website CSS Syllabus CSS Study Plan CSS Interview Prep

CSS References

CSS Reference CSS Selectors CSS Combinators CSS Pseudo-classes CSS Pseudo-elements CSS At-rules CSS Functions CSS Reference Aural CSS Web Safe Fonts CSS Animatable CSS Units CSS PX-EM Converter CSS Colors CSS Color Values CSS Default Values CSS Browser Support

CSS Functions


CSS Functions

CSS functions let you perform calculations, insert values dynamically, or apply transformations. They are used inside property values to make styles more flexible and powerful.

Here are some of the most commonly used CSS functions:

  • calc() Performs calculations
  • var() Uses CSS variables
  • rgb(), hsl() Color functions
  • min(), max(), clamp() Flexible sizing
  • url() Loads external resources (e.g., images)
  • attr() Reads HTML attribute values
  • rotate() Rotates an HTML element
  • scale() Scales the size of an HTML element
  • perspective() Applies a 3D effect to an HTML element

The CSS calc() Function

The calc() function performs a mathematical calculation that will be used as the property value.

It supports addition, subtraction, multiplication, and division, and can combine different units, like pixels and percentages.

The following example ensures that the container always fills the width, but leaves space (150px) for a sidebar/padding:

Example

.container {
  width: calc(100% - 150px);
  border: 1px solid black;
  padding: 10px;
}
Try it Yourself »

The CSS var() Function

The var() function is used to insert the value of a CSS variable.

A good way to use CSS variables is when it comes to the colors of your design. Instead of copy and paste the same colors over and over again, you can place them in variables.

The following example declares three global variables (--main-bg-color, --main-txt-color, --main-padding), and then use the var() function to insert the values of the variables later in the style sheet:

Example

:root {
  --main-bg-color: linen;
  --main-txt-color: gray;
  --main-padding: 10px;
}

#div1 {
  background-color: var(--main-bg-color);
  color: var(--main-txt-color);
  padding: var(--main-padding);
}
Try it Yourself »


The CSS rgb() Function with Opacity

The rgb() function specifies a color using the Red-Green-Blue (RGB) color model. An optional alpha component can also be added (which represents the transparency of the color).

The following example adds opacity to the background color without changing the element's text opacity:

Example

button {
  background: rgb(255 0 0 / 0.3);
  padding: 8px;
}
Try it Yourself »

The CSS hsl() Function

The hsl() function specifies a color using the Hue-Saturation-Lightness (HSL) color model. An optional alpha component can also be added (which represents the transparency of the color).

The following example shows the color green (120) with different lightness:

Example

#p1 {background-color:hsl(120 100% 50%);}
#p2 {background-color:hsl(120 100% 75%);}
#p3 {background-color:hsl(120 100% 25%);}
Try it Yourself »

The following example shows some HSL colors with opacity:

Example

#p1 {background-color:hsl(120 100% 25% / 20%);}
#p2 {background-color:hsl(290 60% 70% / 0.3);}
Try it Yourself »

The CSS min() Function

The min() function help create responsive layouts with flexible sizing.

The min() function will use the smallest value, from a comma-separated list of values, as the property value.

The following example sets the width of #div1 to whichever value is smallest: 50% or 300px:

Example

#div1 {
  width: min(50%, 300px);
}
Try it Yourself »

The CSS max() Function

The max() function help create responsive layouts with flexible sizing.

The max() function will use the largest value, from a comma-separated list of values, as the property value.

The following example sets the width of #div1 to whichever value is largest: 50% or 300px:

Example

#div1 {
  width: max(50%, 300px);
}
Try it Yourself »

The CSS clamp() Function

The clamp() function is used to set a value that will adjust responsively between a minimum value, a preffered value, and a maximum value depending on the size of the viewport.

The following example sets the font-size of the div element to 2.5vw, but never smaller than 1rem or larger than 2rem:

Example

div {
  font-size: clamp(1rem, 2.5vw, 2rem);
}
Try it Yourself »

The CSS url() Function

The url() function is used to load external resources like images and fonts.

The following example sets a background image for the page:

Example

body {
  background-image: url("paper.gif");
}
Try it Yourself »

The CSS attr() Function

The attr() function is used to insert an attribute value into content.

The following example inserts the value of the cite attribute after the blockquote text:

Example

blockquote:after {
  content: " (source: " attr(cite) ") ";
  color: hotpink;
}
Try it Yourself »

The CSS rotate() Function

The rotate() function is used to rotate an element.

The following example rotates several <div> elements:

Example

#myDiv1 {
  transform: rotate(25deg);
}

#myDiv2 {
  transform: rotate(45deg);
}

#myDiv3 {
  transform: rotate(-45deg);
}
Try it Yourself »

The CSS scale() Function

The scale() function is used to scale the size of an element.

The following example scales an image to a little bit larger when hovering over it:

Example

img:hover {
  transform: scale(1.1);
}
Try it Yourself »

The CSS perpective() Function

The perspective() function is used to apply a 3D effect directly to an HTML element.

Example

.box {
  transform: perspective(500px) rotateY(45deg);
}
Try it Yourself »

Summary

CSS functions are useful tools to:

  • Perform dynamic calculations
  • Use flexible measurements
  • Apply reusable styles
  • Control layouts and colors responsively

Related Pages


×

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-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.