Home > Web Front-end > CSS Tutorial > How Can I Use Sass Variables Inside CSS's `calc()` Function?

How Can I Use Sass Variables Inside CSS's `calc()` Function?

Barbara Streisand
Release: 2024-12-02 20:27:11
Original
851 people have browsed it

How Can I Use Sass Variables Inside CSS's `calc()` Function?

Sass Variables Within CSS calc() Function

When incorporating Sass variables into the calc() function, challenges may arise. For instance, using a variable like $body_padding within calc() doesn't automatically trigger the desired substitution. Instead, the output resembles the following:

body {
    padding-top: 50px;
    height: calc(100% - $body_padding);
}
Copy after login

To rectify this situation and ensure Sass recognizes the need to replace variables within the calc() function, employ interpolation. Interpolate the variable using the syntax #{$body_padding}:

body
    height: calc(100% - #{$body_padding})
Copy after login

Alternatively, using the border-box property can also effectively resolve this issue:

body
    box-sizing: border-box
    height: 100%
    padding-top: $body_padding
Copy after login

With these methods, Sass will now correctly interpret and substitute variables within the calc() function, achieving the desired calculations.

The above is the detailed content of How Can I Use Sass Variables Inside CSS's `calc()` Function?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template