Introduction
CSS calc lets you do math inside your styles to mix different units and create flexible layouts.
Jump into concepts and practice - no test required
property: calc(expression);width: calc(100% - 50px);
margin-top: calc(2rem + 10px);
font-size: calc(1vw + 1rem);
height: calc(50vh / 2);
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>CSS calc Example</title> <style> body { font-family: Arial, sans-serif; padding: 2rem; } .box { width: calc(100% - 40px); height: 100px; background-color: #4a90e2; margin: 20px auto; color: white; display: flex; align-items: center; justify-content: center; font-size: calc(1rem + 1vw); border-radius: 0.5rem; } </style> </head> <body> <div class="box">This box uses calc() for width and font size.</div> </body> </html>
calc() function allow you to do?calc()calc() function is designed to perform math operations like addition, subtraction, multiplication, and division inside CSS property values.calc() in CSS?calc()calc() for correct parsing.div {
width: calc(50% - 100px);
}p {
margin-left: calc(20px+10%);
}calc() must have spaces on both sides to be valid.margin-left supports calc(), so no error there.calc() and limits the width?calc(100px + 10%) to combine fixed and percentage widths.max-width: 200px; separately, which correctly limits the maximum width.