CSS Math Functions
CSS Math functions are a set of built-in functions that allow you to perform mathematical operations in CSS.
Here are some of the most commonly used math functions:
The calc()
Functions
This function allows you to perform simple math operations like addition, subtraction, multiplication, and division within a CSS property.
As an example:
Editor
In this example:
- We use the
calc()
function to set the width and height of adiv
element to be20
pixels less than the full width and100
pixels less than the full height of the viewport, respectively. - We also use
calc()
to set the font size of anh1
element to be1.5
times the base font size plus10
pixels, and the font size of ap
element to be14
pixels plus0.5%
of the viewport width.
The min()
Functions
This function returns the smallest value among two or more input values. You can use min()
to set a property to the smallest of several values.
As an example:
Editor
In this example:
- We use the
min()
function to set the width of adiv
element to be either50%
of the container width or300
pixels, whichever is smaller. - We also use
min()
to set the height of thediv
element to be either400
pixels or80%
of the viewport height, whichever is smaller. - We also use
min()
to set the font size of anh1
element to be either2.5
times the base font size or 5% of the viewport width, whichever is smaller. - We use
min()
to set the font size of ap
element to be either16
pixels or1.5
times the parent element's font size, whichever is smaller.
The min()
function allows you to set a property to the smallest of several values, which can be useful for making your layouts responsive to different screen sizes and devices.
The max()
Functions
This function returns the largest value among two or more input values. You can use max()
to set a property to the largest of several values.
As an example:
Editor
In this example:
- We use the
max()
function to set the width of adiv
element to be either30%
of the container width or200
pixels, whichever is larger. - We also use
max()
to set the height of thediv
element to be either300
pixels or60%
of the viewport height, whichever is larger. - We also use
max()
to set the font size of anh1
element to be either2
times the base font size or3%
of the viewport width, whichever is larger. - We use
max()
to set the font size of ap
element to be either14
pixels or80%
of the parent element's font size, whichever is larger.
The max()
function allows you to set a property to the largest of several values, which can be useful for making your layouts responsive to different screen sizes and devices.
The clamp()
Functions
This function allows you to set a value that is within a range of minimum and maximum values. You can use clamp()
to set a property to a value that is within a specified range.
As an example:
Editor
In this example:
- We use the
clamp()
function to set the width of adiv
element to be a value that is between200
pixels and50%
of the container width, but not less than200
pixels or greater than500
pixels. - We also use
clamp()
to set the height of thediv
element to be a value that is between200
pixels and60%
of the viewport height, but not less than200
pixels or greater than400
pixels. - We also use
clamp()
to set the font size of anh1
element to be a value that is between2
times the base font size and5%
of the viewport width, but not less than 2rem or greater than 4rem. - We use
clamp()
to set the font size of ap
element to be a value that is between14
pixels and120%
of the parent element's font size, but not less than14px
or greater than18px
.
The clamp()
function allows you to set a property to a value that is between a minimum and maximum value, which can be useful for creating flexible and responsive layouts.