Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • CSS Tutorial
  • CSS Exercises
  • CSS Interview Questions
  • CSS Selectors
  • CSS Properties
  • CSS Functions
  • CSS Examples
  • CSS Cheat Sheet
  • CSS Templates
  • CSS Frameworks
  • Bootstrap
  • Tailwind
  • CSS Formatter
Open In App
Next Article:
CSS Math Functions
Next article icon

CSS Math Functions

Last Updated : 03 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

CSS Math Functions allow you to perform calculations directly in your stylesheets, making it easier to create flexible and responsive designs. With functions like calc(), max(), and min(), you can dynamically adjust styles to suit various conditions.

1. calc() Function

The calc() function allows you to perform mathematical calculations in CSS. It supports addition (+), subtraction (-), multiplication (*), and division (/).

HTML
<!--Driver Code Starts--> <html> <head> <!--Driver Code Ends-->      <style>         .box {             width: calc(50% - 20px);             height: 100px;             background-color: lightblue;         }     </style>  <!--Driver Code Starts--> </head> <body>     <div class="box"></div> </body> </html> <!--Driver Code Ends--> 
  • The width of the .box is set to 50% of the container's width, minus 20px for padding.
  • This ensures the box takes up half of the container's width but with an offset of 20px on each side.

2. min() Function

The min() function returns the smallest value from a list of values. It’s useful when you want an element’s property to have a dynamic value that adjusts based on other conditions but doesn't exceed a certain limit.

HTML
<!--Driver Code Starts--> <html> <head> <!--Driver Code Ends-->      <style>         .box {             width: min(200px, 50%);             height: 100px;             background-color: lightgreen;         }     </style>  <!--Driver Code Starts--> </head> <body>     <div class="box"></div> </body> </html> <!--Driver Code Ends--> 
  • The .box width will be either 200px or 50% of the container width, whichever is smaller.
  • This is useful in responsive designs where you don’t want the element to grow beyond a certain size.

3. max() Function

The max() function works the opposite of min(). It returns the largest value from a list of values. This is useful when you want an element's property to be at least a certain size but can grow larger if necessary.

HTML
<!--Driver Code Starts--> <html> <head> <!--Driver Code Ends-->      <style>         .box {             width: max(300px, 50%);             height: 100px;             background-color: lightcoral;         }     </style>  <!--Driver Code Starts--> </head> <body>     <div class="box"></div> </body> </html> <!--Driver Code Ends--> 
  • The .box width will be either 300px or 50% of the container width, whichever is larger.
  • This ensures that the box always has a minimum width of 300px, but can expand further if the container allows.

4. clamp() Function

The clamp() function defines a value that is constrained within a defined minimum and maximum range, while allowing for a preferred value in between. This is perfect for creating responsive designs that adjust with the viewport size.

HTML
<!--Driver Code Starts--> <html> <head> <!--Driver Code Ends-->      <style>         .box {             font-size: clamp(14px, 5vw, 24px);             height: 100px;             background-color: lightgoldenrodyellow;         }     </style>  <!--Driver Code Starts--> </head> <body>     <div class="box">Responsive Text</div> </body> </html> <!--Driver Code Ends--> 
  • The font-size is set to scale with the viewport width (5vw), but it will not go below 14px or exceed 24px.
  • This is useful for making text responsive, ensuring readability across devices without it getting too small or too large.

5. var() Function

The var() function is used to access the value of a CSS custom property (variable). Custom properties allow you to store values and reuse them throughout your CSS, making the code more maintainable and flexible.

HTML
<!--Driver Code Starts--> <html> <head> <!--Driver Code Ends-->      <style>         :root {             --box-width: 300px;         }         .box {             width: var(--box-width);             height: 100px;             background-color: lightpink;         }     </style>  <!--Driver Code Starts--> </head> <body>     <div class="box"></div> </body> </html> <!--Driver Code Ends--> 
  • The :root selector defines a custom property --box-width with a value of 300px.
  • The .box then uses this custom property to set its width, making it easy to reuse the value elsewhere in the stylesheet.
  • If you need to change the width, you only need to update it in one place (--box-width).

Exponential functions

                  Type Syntax                          Description
pow() Functionwidth: calc(10px * pow(5,2)); It calculates the base raised to the power of the number.          
sqrt() Functionwidth: calc(100px * sqrt(9));It calculates the square root of a number
hypot() Functionwidth: hypot(3em, 4em);It calculates the square root of the sum of the squares of its arguments.
log() Functionwidth: calc(100px * log(8, 2)); /* 300px */It calculates the logarithm of a number
exp() Functionwidth: calc(100px * exp(0)); /* 100px * 1 = 100px */It is used to calculate e raised to the power of a number.

Sign-related functions

          Type Syntax                          Description
abs() Functionwidth: abs(20% - 100px);It calculates the absolute value of the number     
sign() Functiontop: sign(20vh - 100px);It calculates the sign of the number

Stepped value functions

          Type Syntax                          Description
round() Functionproperty: round(<rounding-strategy>, valueToRound, roundingInterval);It calculates the rounded number based on the rounding strategy.
mod() Functionmod(dividend, divisor)It calculates the modulus by dividing one number by another.
rem() Functionrem(dividend, divisor)It calculates the remainder by dividing one number with another.

Best Practices for Using Math Functions in CSS

  • Combine Units Easily: Use calc() to mix different CSS units (e.g., percentages and pixels) for more flexible layouts.
  • Set Responsive Limits: Apply min(), max(), and clamp() to ensure elements adapt smoothly to various screen sizes.
  • Keep Calculations Simple: Avoid overly complex expressions to maintain readability and ease of maintenance.

Next Article
CSS Math Functions

M

mansigeekso9ii
Improve
Article Tags :
  • Web Technologies
  • CSS
  • CSS-Functions

Similar Reads

    CSS calc() Function
    The calc() function is an inbuilt CSS function used to perform calculations to determine CSS property values dynamically. This function allows combining different units, such as percentages and pixels, using basic arithmetic operations like addition, subtraction, multiplication, and division. This a
    4 min read
    PHP | Functions
    A function in PHP is a self-contained block of code that performs a specific task. It can accept inputs (parameters), execute a set of statements, and optionally return a value. PHP functions allow code reusability by encapsulating a block of code to perform specific tasks.Functions can accept param
    8 min read
    Math.js compile() Function
    The Math.js is an extensive library with mathematical functions that work on JavaScript and Node.js. The additional feature of this library is that it provides a flexible expression parser that supports symbolic computation. It is quite powerful and easy to use since it comes with many built-in func
    2 min read
    Less.js Math acos() Function
    Less.js is a simple CSS pre-processor that facilitates the creation of manageable, customizable, and reusable style sheets for websites. Because CSS is a dynamic style sheet language, it was chosen. Because LESS is adaptable, it works with a wide range of browsers. Only CSS that has been written in
    3 min read
    PHP create_function() Function
    The create_function() is an inbuilt function in PHP which is used to create an anonymous (lambda-style) function in the PHP. Syntax: string create_function ( $args, $code ) Parameters: This function accepts two parameters which is describes below: $args: It is a string type function argument. $code:
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences