Skip to content
geeksforgeeks
  • 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
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • 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 circle() function
Next article icon

CSS calc() Function

Last Updated : 30 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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 article explores the syntax, usage, and benefits of the calc() function, demonstrating its practical applications through examples to help you enhance your web design flexibility and efficiency.

Syntax: 

calc( Expression )

Parameters: This function accepts single parameter Expression which is mandatory. This parameter contains mathematical expressions which need to implement. The operators used by this parameters are: +, -, *, /

Example: The below program illustrates the calc() function in CSS:

html
<!DOCTYPE html> <html>  <head>     <title>calc function</title>     <style>         .geeks {             position: absolute;             left: 50px;             width: calc(100% - 20%);             height: calc(500px - 300px);             background-color: green;             padding-top: 30px;             text-align: center;         }                  .gfg {             font-size: 40px;             font-weight: bold;             color: white         }                  h1 {             color: white;         }     </style> </head>  <body>     <div class="geeks">         <div class="gfg">GeeksforGeeks</div>         <h1>The calc() Function</h1>     </div> </body>  </html> 

Output: 

Understanding the working of the calc() function in CSS:

  • The calc() function in CSS does calculations based on the values of their parent element or the values provided by the user during the calculation. 

Let’s understand the work in more depth using some examples:

Example 1: This example shows the working of the calc() function in CSS.

HTML
<!DOCTYPE html> <html lang="en">  <head>     <meta charset="UTF-8">     <meta http-equiv="X-UA-Compatible"            content="IE=edge">     <meta name="viewport"            content="width=device-width, initial-scale=">     <title>Calc Function</title>     <style>         .parent{             background-color: skyblue;                    }         .heading {                      left: 30px;             width: calc(50% - 100px);             height: 100px;             background-color: #579dcf;             padding-top: 20px;             text-align: center;         }                  h2 {             color: #e9e8e9;         }     </style> </head>  <body>     <br>     <div class="parent">         <div class="heading">             <h2>                Welcome to GEEKSFORGEEKS.               </h2>         </div>     </div> </body>  </html> 

In the above code example, the calc() function is used to give the width value to the heading. Here , we have used the value of the parent which is set to 100% of the screen width by default.  The calc(50% – 100px) means that the width of the heading will be equal to “50% of the width of the parent – 100 px”. Thus , we here use the calc() function with both the value from parent and a constant value.

Here is the output of the code:

Example 2: This example shows the working of the calc() function in CSS.

HTML
<!DOCTYPE html> <html lang="en">  <head>     <meta charset="UTF-8">     <meta http-equiv="X-UA-Compatible"            content="IE=edge">     <meta name="viewport"            content="width=device-width, initial-scale=">     <title>Calc Function</title>     <style>         .parent{             background-color: skyblue;             width: calc(80% - 2em);         }         .heading {                      left: 30px;             width: calc(50% - 100px);             height: 100px;             background-color: #579dcf;             padding-top: 20px;             text-align: center;         }                  h2 {             color: #e9e8e9;         }     </style> </head>  <body>     <br>     <div class="parent">         <div class="heading">             <h2>                Welcome to GEEKSFORGEEKS.               </h2>         </div>     </div> </body>  </html> 

In the above code example, we have used the calc() function twice. First, using the calc() function we have defined the value of the parent div. Then, using the value of the parent div element and using the calc() function, we define the value of the heading div element. Thus, it is clear that calc() function present in the parent element can access the value produced by the child calc() function.

Here is the output of the code:

The calc() function in CSS provides a flexible way to perform dynamic calculations directly within your stylesheets. By leveraging this function, developers can create more responsive and adaptive layouts, seamlessly combining different units and values. Understanding and utilizing the calc() function can significantly improve the efficiency and maintainability of your CSS, leading to more sophisticated and adaptable web designs.

Supported Browser:

  • Google Chrome
  • Edge
  • Firefox
  • Opera
  • Safari


Next Article
CSS circle() function

V

vijay_raj
Improve
Article Tags :
  • CSS
  • Web Technologies
  • CSS-Functions
  • Functions
Practice Tags :
  • Functions

Similar Reads

  • CSS attr() Function
    The attr() function is an inbuilt function in CSS that returns the value of an attribute of the selected elements. Syntax: attr( attr_name )Parameter: This function accepts a single parameter attr_name which is used to hold the name of the attribute in an HTML element. It is a mandatory parameter. R
    2 min read
  • CSS blur() Function
    The CSS blur() function applies a Gaussian blur effect to an element, making it appear out of focus. It is used with the filter property and accepts a length value defining the blur radius. CSS blur() function is part of the CSS filter property, which allows you to apply graphical effects like blurr
    3 min read
  • CSS brightness() Function
    The brightness() function is an inbuilt function which is used to apply a filter to set the brightness of the image. This function uses the linear multiplier to the image to increase or decrease brightness. Syntax: brightness( amount ) Parameters: This function accepts single parameter amount which
    1 min read
  • 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
  • CSS circle() function
    The circle() function is an inbuilt function in CSS that is used to create floating text around the circular shape picture or anything else. Syntax: circle(100px at 10px 150px); or circle( percentage ); Parameters: This function accepts a single parameter length or percentage which is used to hold t
    3 min read
  • CSS conic-gradient() Function
    The conic-gradient() function is an inbuilt function in CSS that is used to set a conic gradient as the background image. The conic gradient angle starts from 0 degrees - 360 degrees. Conic are circular and use the center of the element as the source point for color stop. Conic Gradients include pie
    4 min read
  • CSS contrast() Function
    The contrast() function is an inbuilt function which is used to apply a filter to set the contrast of the image. This function adjusts the contrast of the image. Syntax: contrast( amount ) Parameters: This function accepts single parameter amount which holds the amount of contrast. The value of cont
    1 min read
  • CSS cubic-bezier() Function
    The cubic-bezier() function is an inbuilt function in CSS that is used to define a Cubic Bezier curve. A Bezier curve is a mathematically defined curve used in two-dimensional graphic applications like adobe illustrator, Inkscape, etc. The curve is defined by four points: the initial position and th
    1 min read
  • CSS drop-shadow() Function
    The CSS drop-shadow() function adds a shadow effect to elements, like images, using horizontal and vertical offsets, blur radius, spread radius, and color parameters. It enhances visual depth and prominence in web design. Syntax: filter: drop-shadow(offset-x offset-y blur-radius spread-radius color)
    3 min read
  • CSS ellipse() Function
    The ellipse() function is an inbuilt function in CSS used to create floating text around the ellipse shape picture or anything else. Syntax: circle(100px 10 px at 10px 150px);or ellipse( percentage percentage );Parameter: This function accepts a single parameter length or percentage which is used to
    3 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