如何在calc函数中使用CSS变量

我正在尝试在css calc函数中使用css变量,但它无法按预期方式呈现。

我的代码

font-size: calc(var(--typography-headings-h1-minfontsize) + unquote("px") + (var(--typography-headings-h1-maxfontsize) - var(--typography-headings-h1-minfontsize)) * ((100vw - 320) / (1296 - 320)));

:root {
  --primary-color: #0652dd;
  --secondary-color: #ffffff;

  --navbar-bg-color: #f1f1f1;

  --typography-body-fontsize: 16px;
  --typography-body-family: Merriweather;
  --typography-body-weight: 400;
  --typography-body-color: #000;
  --typography-body-lineheight: 1.5;

  --typography-headings-family: Fira Sans;
  --typography-headings-weight: 400;
  --typography-headings-color: #dd3333;
  --typography-headings-style: normal;
  --typography-headings-letterspacing: 0;
  --typography-headings-texttransform: none;

  --typography-headings-h1-minfontsize: 36;
  --typography-headings-h1-maxfontsize: 60;
  --typography-headings-h1-lineheight: 1.3;
}

h1 {
  font-size: calc(14px + (26 - 14) * ((100vw - 320px) / (1296 - 320)));
  font-size: calc(var(--typography-headings-h1-minfontsize) + unquote("px") + (var(--typography-headings-h1-maxfontsize) - var(--typography-headings-h1-minfontsize) ) * ((100vw - 320) / (1296 - 320))
  );
}
<h1>Lorem ipsum dolor sit,amet consectetur adipisicing elit. Debitis,impedit.</h1>

预期结果:

font-size: calc(14px + (26 - 14) * ((100vw - 320px) / (1296 - 320)));

任何帮助将不胜感激

EDAshiyan951 回答:如何在calc函数中使用CSS变量

CSS中没有称为unquote("px")的东西,您需要乘以1px

:root {
  --primary-color: #0652dd;
  --secondary-color: #ffffff;

  --navbar-bg-color: #f1f1f1;

  --typography-body-fontsize: 16px;
  --typography-body-family: Merriweather;
  --typography-body-weight: 400;
  --typography-body-color: #000;
  --typography-body-lineheight: 1.5;

  --typography-headings-family: Fira Sans;
  --typography-headings-weight: 400;
  --typography-headings-color: #dd3333;
  --typography-headings-style: normal;
  --typography-headings-letterspacing: 0;
  --typography-headings-texttransform: none;

  --typography-headings-h1-minfontsize: 36;
  --typography-headings-h1-maxfontsize: 60;
  --typography-headings-h1-lineheight: 1.3;
}

h1 {
  /*font-size: calc(14px + (26 - 14) * ((100vw - 320px) / (1296 - 320)));*/
  font-size: 
     calc(var(--typography-headings-h1-minfontsize)*1px + 
       (
        var(--typography-headings-h1-maxfontsize) - var(--typography-headings-h1-minfontsize)) 
        * 
        ((100vw - 320px) / (1296 - 320)) /* don't forget the px unit for the first 320 */
       );
}
<h1>Lorem ipsum dolor sit,amet consectetur adipisicing elit. Debitis,impedit.</h1>

相关:How do I debug CSS calc() value?

本文链接:https://www.f2er.com/2394921.html

大家都在问