ChatGPT解决这个技术问题 Extra ChatGPT

CSS calc() 函数中的 Sass 变量

我正在尝试在 Sass 样式表中使用 calc() 函数,但遇到了一些问题。这是我的代码:

$body_padding: 50px

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

如果我使用文字 50px 而不是我的 body_padding 变量,我会得到我想要的。但是,当我切换到变量时,这是输出:

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

如何让 Sass 认识到它需要替换 calc 函数中的变量?

Usa a Variable in a Mixin 的可能重复项
@user3705055 您确实需要 calc 因为 Sass 无法计算 100% - 50px,因为单位不同。这只能由浏览器计算,一旦它知道容器有多大,以便在添加值之前将 100% 转换为 px。这就是 calc 存在的确切原因。
@cimmonon好吧,现在反过来了,因为这个问题变得更受欢迎了。

C
Christoph

Interpolate

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

对于这种情况,border-box 也足够了:

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

在我看来,box-sizing: border-box; 当然是理想的选择。一般来说,盒子尺寸模型应该使
非常感谢!我正要与 calc(100% - var(--body_padding)) 一起走 css 路线
@leachim Sass 插值由 Sass 编译器评估,而不是由浏览器评估。
D
DA SILVA MARLY Joao

要在 height 属性的 calc() 中使用 $variables

HTML:

<div></div>

SCSS:

$a: 4em;

div {
  height: calc(#{$a} + 7px);
  background: #e53b2c;
}

出于某种原因,我发现这个答案更清楚。我没有做很多 scss 工作,所以这对我来说“更简单”并且更容易理解。
这绝对是一个比公认的更清晰的答案。 “插值:”对我来说毫无意义。
@JacquesMathieu sass-lang.com/documentation/interpolation - 如果它对你没有任何意义,而且它是一个有这么多选票的答案,那么你可能应该明白大惊小怪的原因。就我的两分钱...
@一个。 Chiesa 当然,这定义了插值。但是,我和 OP 不知道 SASS 中的插值,否则永远不会问这个问题。因此,在没有定义或任何伪装的情况下,仅仅陈述一个人们可能从未听说过的词并不能帮助创建一个包罗万象的答案。该链接当然很有帮助。很高兴在接受的答案上看到这一点。
我被困在这个问题上超过 1 小时。
R
Ryan Taylor

尽管它没有直接关系。但是我发现如果你没有正确放置空格,CALC 代码将不起作用。

所以这对我不起作用calc(#{$a}+7px)

但这有效calc(#{$a} + 7px)

我花了一些时间来解决这个问题。


calc 之后也不允许有额外的空格。 calc (#{$a} + 7px) 行不通。
上面的人正确使用了空格,所以这并不能真正回答他们的问题,但它回答了我的问题。谢谢!
是的,这对我有用。我试过 height:calc(100%-95px); (即没有适当的空间。)起初并没有奏效。但是在适当的间距下,它可以在没有插值的情况下工作
小心,这在 IE 和 Edge HTML 中不起作用
S
Serkan KONAKCI

我已经尝试过了,然后我解决了我的问题。它将按照给定的速率(基本大小/速率大小)自动计算所有媒体断点


$base-size: 16;
$rate-size-xl: 24;

    // set default size for all cases;
    :root {
      --size: #{$base-size};
    }

    // if it's smaller then LG it will set size rate to 16/16;
    // example: if size set to 14px, it will be 14px * 16 / 16 = 14px
    @include media-breakpoint-down(lg) {
      :root {
        --size: #{$base-size};
      }
    }

    // if it is bigger then XL it will set size rate to 24/16;
    // example: if size set to 14px, it will be 14px * 24 / 16 = 21px
    @include media-breakpoint-up(xl) {
      :root {
        --size: #{$rate-size-xl};
      }
    }

@function size($px) {
   @return calc(#{$px} / $base-size * var(--size));
}

div {
  font-size: size(14px);
  width: size(150px);
}

C
Carl L.D.

这是一个使用 SASS/SCSS 和数学公式样式的非常简单的解决方案:

/* frame circle */
.container {
    position: relative;
    border-radius: 50%;
    background-color: white;
    overflow: hidden;
    width: 400px;
    height: 400px; }

/* circle sectors */
.menu-frame-sector {
    position: absolute;
    width: 50%;
    height: 50%;
    z-index: 10000;
    transform-origin: 100% 100%;
  }

$sector_count: 8;
$sector_width: 360deg / $sector_count;

.sec0 {
    transform: rotate(0 * $sector_width) skew($sector_width);
    background-color: red; }
.sec1 {
    transform: rotate(1 * $sector_width) skew($sector_width);
    background-color: blue; }
.sec2 {
    transform: rotate(2 * $sector_width) skew($sector_width);
    background-color: red; }
.sec3 {
    transform: rotate(3 * $sector_width) skew($sector_width);
    background-color: blue; }
.sec4 {
    transform: rotate(4 * $sector_width) skew($sector_width);
    background-color: red; }
.sec5 {
    transform: rotate(5 * $sector_width) skew($sector_width);
    background-color: blue; }
.sec6 {
    transform: rotate(6 * $sector_width) skew($sector_width);
    background-color: red; }
.sec7 {
    transform: rotate(7 * $sector_width) skew($sector_width);
    background-color: blue; }

最后,我强烈建议您了解 transform-originrotate()skew()

https://tympanus.net/codrops/2013/08/09/building-a-circular-navigation-with-css-transforms/


P
Pang

尝试这个:

@mixin heightBox($body_padding){
   height: calc(100% - $body_padding);
}

body{
   @include heightBox(100% - 25%);
   box-sizing: border-box
   padding:10px;
}

它与问题中的代码的原因不同。