
- CSS - Home
- CSS - Roadmap
- CSS - Introduction
- CSS - Syntax
- CSS - Inclusion
- CSS - Types
- CSS - Measurement Units
- CSS - Selectors
- CSS - Colors
- CSS - Backgrounds
- CSS - Fonts
- CSS - Text
- CSS - Images
- CSS - Links
- CSS - Tables
- CSS - Borders
- CSS - Border Block
- CSS - Border Inline
- CSS - Margins
- CSS - Lists
- CSS - Padding
- CSS - Cursor
- CSS - Outlines
- CSS - Dimension
- CSS - Scrollbars
- CSS - Inline Block
- CSS - Dropdowns
- CSS - Visibility
- CSS - Overflow
- CSS - Clearfix
- CSS - Float
- CSS - Arrows
- CSS - Resize
- CSS - Quotes
- CSS - Order
- CSS - Position
- CSS - Hyphens
- CSS - Hover
- CSS - Display
- CSS - Focus
- CSS - Zoom
- CSS - Translate
- CSS - Height
- CSS - Hyphenate Character
- CSS - Width
- CSS - Opacity
- CSS - Z-Index
- CSS - Bottom
- CSS - Navbar
- CSS - Overlay
- CSS - Forms
- CSS - Align
- CSS - Icons
- CSS - Image Gallery
- CSS - Comments
- CSS - Loaders
- CSS - Attr Selectors
- CSS - Combinators
- CSS - Root
- CSS - Box Model
- CSS - Counters
- CSS - Clip
- CSS - Writing Mode
- CSS - Unicode-bidi
- CSS - min-content
- CSS - All
- CSS - Inset
- CSS - Isolation
- CSS - Overscroll
- CSS - Justify Items
- CSS - Justify Self
- CSS - Tab Size
- CSS - Pointer Events
- CSS - Place Content
- CSS - Place Items
- CSS - Place Self
- CSS - Max Block Size
- CSS - Min Block Size
- CSS - Mix Blend Mode
- CSS - Max Inline Size
- CSS - Min Inline Size
- CSS - Offset
- CSS - Accent Color
- CSS - User Select
- CSS - Cascading
- CSS - Universal Selectors
- CSS - ID Selectors
- CSS - Group Selectors
- CSS - Class Selectors
- CSS - Child Selectors
- CSS - Element Selectors
- CSS - Descendant Selectors
- CSS - General Sibling Selectors
- CSS - Adjacent Sibling Selectors
- CSS Advanced
- CSS - Grid
- CSS - Grid Layout
- CSS - Flexbox
- CSS - Visibility
- CSS - Positioning
- CSS - Layers
- CSS - Pseudo Classes
- CSS - Pseudo Elements
- CSS - @ Rules
- CSS - Text Effects
- CSS - Paged Media
- CSS - Printing
- CSS - Layouts
- CSS - Validations
- CSS - Image Sprites
- CSS - Important
- CSS - Data Types
- CSS3 Advanced Features
- CSS - Rounded Corner
- CSS - Border Images
- CSS - Multi Background
- CSS - Color
- CSS - Gradients
- CSS - Box Shadow
- CSS - Box Decoration Break
- CSS - Caret Color
- CSS - Text Shadow
- CSS - Text
- CSS - 2d transform
- CSS - 3d transform
- CSS - Transition
- CSS - Animation
- CSS - Multi columns
- CSS - Box Sizing
- CSS - Tooltips
- CSS - Buttons
- CSS - Pagination
- CSS - Variables
- CSS - Media Queries
- CSS - Functions
- CSS - Math Functions
- CSS - Masking
- CSS - Shapes
- CSS - Style Images
- CSS - Specificity
- CSS - Custom Properties
- CSS Responsive
- CSS RWD - Introduction
- CSS RWD - Viewport
- CSS RWD - Grid View
- CSS RWD - Media Queries
- CSS RWD - Images
- CSS RWD - Videos
- CSS RWD - Frameworks
- CSS References
- CSS Interview Questions
- CSS Online Quiz
- CSS Online Test
- CSS Mock Test
- CSS - Quick Guide
- CSS - Cheatsheet
- CSS - Properties References
- CSS - Functions References
- CSS - Color References
- CSS - Web Browser References
- CSS - Web Safe Fonts
- CSS - Units
- CSS - Animation
- CSS Resources
- CSS - Useful Resources
- CSS - Discussion
CSS - Math Functions
CSS math functions allow you to perform mathematical calculations directly in your CSS stylesheets. These functions can be used to manipulate values such as lengths, angles, colors, and more.
div{ width: calc(100% - 40px); /* 100% width minus 40px for padding */ width: max(200px, 50%); /* Set width to the maximum value between 200px and 50% of the viewport width */ }
Table of Contents
- Types of Mathematical Functions in CSS
- Math Functions
- Basic Arithmetic Functions
- Comparison Functions
- Stepped Value Functions
- Trigonometric Functions
Types of Mathematical Functions in CSS
There are several types of math functions that can be used in CSS. They include:
-
Basic Arithmetic Functions This includes the
calc()
function for performing calculations on numerical values. -
Comparison Functions This includes functions like
min()
,max()
, andclamp()
for comparing variables. -
Stepped Value Functions This includes the
round()
function for calculating a rounded number based on a rounding strategy. -
Trigonometric Functions These functions, such as
sin()
,cos()
, andtan()
, introduce mathematical functions like sine, cosine, and tangent into stylesheets.
The calc Function
The calc() function is a basic arithmetic function in CSS that allows you to perform calculations on numerical values. It can be used to dynamically modify property values by carrying out mathematical operations like addition, subtraction, multiplication, and division.
Example
Here's an example that demonstrates the usage of the calc()
function:
<!DOCTYPE html> <html lang="en"> <head> <style> .container { width: 80%; margin: 0 auto; border: 1px solid #000; padding: 20px; } .box { /* 100% width minus 40px for padding */ width: calc(100% - 40px); /* 100% of viewport height minus 20px for padding */ height: calc(100% - 20px); background-color: lightblue; padding: 20px; box-sizing: border-box; } </style> </head> <body> <div class="container"> <div class="box"> This box uses the CSS calc() function to dynamically calculate its width and height. </div> </div> </body> </html>
The max Function
The max() function is a comparison function in CSS that allows you to determine the maximum value from a given list of values. It can be used to compare variables and apply conditional styling based on the maximum value.
Example
Here's an example that demonstrates the usage of the max()
function:
<!DOCTYPE html> <html lang="en"> <head> <style> .container { width: 80%; margin: 0 auto; border: 1px solid #000; padding: 20px; } .box { /* Set the width to the maximum value between 200px and 50% of the viewport width */ width: max(200px, 50%); background-color: lightblue; padding: 20px; box-sizing: border-box; } </style> </head> <body> <div class="container"> <div class="box"> This box uses the CSS max() function to set its width to the maximum value between 200px and 50% of the viewport width. </div> </div> </body> </html>
The min Function
The min() function is a comparison function in CSS that allows you to determine the minimum value from a given list of values. It can be used to compare variables and apply conditional styling based on the minimum value.
Example
Here's an example that demonstrates the usage of the min()
function:
<!DOCTYPE html> <html lang="en"> <head> <style> .container { width: 80%; margin: 0 auto; border: 1px solid #000; padding: 20px; } .box { /* Set the width to the minimum value between 200px and 50% of the viewport width */ width: min(200px, 50%); background-color: lightblue; padding: 20px; box-sizing: border-box; } </style> </head> <body> <div class="container"> <div class="box"> This box uses the CSS min() function to set its width to the minimum value between 200px and 50% of the viewport width. </div> </div> </body> </html>
Comparison Functions
The assessment of values is made easier by CSS comparison functions, which allows conditional styling based on comparisons within stylesheets.
Following table lists comparison functions:
Function | Description | Example |
---|---|---|
min() | Determines the minimum value from a given set of values. | |
max() | Determines the maximum value from a given list of values. | |
clamp() | Calculates the central of a minimum, central, and maximum values. |
Stepped Value Functions
Stepped value functions provide granular control in stylesheets by enabling exact changes and discrete jumps in property values in CSS.
Following table lists stepped value functions:
Function | Description | Example |
---|---|---|
round() | Calculates a rounded number based on a rounding strategy. |
Trigonometric Functions
CSS trigonometric functions allow for more complex design alterations by introducing mathematical functions like sine, cosine, and tangent into stylesheets.
Following table lists trigonometric functions:
Function | Description | Example |
---|---|---|
sin() | Calculates the trigonometric sine of a number. | |
cos() | Calculates the trigonometric cosine of a number | |
tan() | Calculates the trigonometric tangent of a number. | |
asin() | Calculates the trigonometric inverse sine of a number. | |
acos() | Calculates the trigonometric inverse cosine of a number. | |
atan() | Calculates the trigonometric inverse tangent of a number. | |
atan2() | Calculates the trigonometric inverse tangent of two-numbers in a plane. |