Home > Web Front-end > CSS Tutorial > Why Doesn't Sass Resolve Variables Inside the `calc()` Function?

Why Doesn't Sass Resolve Variables Inside the `calc()` Function?

DDD
Release: 2024-12-01 14:50:14
Original
823 people have browsed it

Why Doesn't Sass Resolve Variables Inside the `calc()` Function?

Sass Variable in CSS Calc() Function: Unresolved Variable Error

When utilizing Sass's calc() function, you may encounter instances where Sass variables are not recognized within the function's arguments. Consider the following scenario:

$body_padding: 50px

body
    padding-top: $body_padding
    height: calc(100% - $body_padding)
Copy after login

While employing the literal value 50px generates the expected results, switching to the $body_padding variable results in an output that maintains the variable within the calc() function.

To resolve this issue and ensure proper variable interpretation, one recommended solution is to interpolate the variable within the calc() function using the following syntax:

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

Another viable option, specifically for this use case, is to utilize the border-box property:

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

Employing border-box ensures that the height property encompasses the padding, effectively eliminating the need for custom calculations within the calc() function.

The above is the detailed content of Why Doesn't Sass Resolve Variables Inside the `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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template