gpt4 book ai didi

CSS 变量和规则

转载 作者:行者123 更新时间:2023-12-04 14:28:14 27 4
gpt4 key购买 nike

我遇到了一个涉及 CSS 变量和规则的问题。我在 :root 中声明了以下变量范围:

:root {
--font-weight-light: 300;
--font-weight-normal: 400;
--font-weight-semibold: 600;
}

以及以下规则:
@font-face {
font-family: 'Proxima Nova';
font-display: swap;
font-style: normal;
font-weight: var(--font-weight-light);
src: url('/fonts/proximanova-light.woff') format('woff');
}

/* other font-face rules for other font variants */

当我尝试使用这些声明来设计我的段落时:
p {
font-family: 'Proxima Nova';
font-weight: var(--font-weight-light);
}

结果不是预期的(轻字体变体),而是字体变体是最后声明的。

当我硬编码时 font-weight: 300;在规则中:
@font-face {
font-family: 'Proxima Nova';
font-display: swap;
font-style: normal;
font-weight: 300; /* var(--font-weight-light) */
src: url('/fonts/proximanova-light.woff') format('woff');
}

一切似乎都有效。

我怀疑我的案例不起作用,因为 :root是所有选择器的基类,而 at 规则不属于 :root .这样 at-rules 就不能访问 :root 中定义的 CSS 变量.

我该如何解决这个问题?或者我错过了什么?

谢谢!

附言我想使用标准 CSS 解决这个问题。我知道SASS变量等,谢谢! :)

更新

我整理了一个可运行的代码片段来说明问题。段落的字体应该是Arial,但我们看到的是Comic Sans... 😓

:root {
--font-weight-light: 300;
--font-weight-semibold: 600;
}

@font-face {
font-family: 'Foo Sans';
font-display: swap;
font-style: normal;
font-weight: var(--font-weight-light);
src: local('Arial');
}

@font-face {
font-family: 'Foo Sans';
font-display: swap;
font-style: normal;
font-weight: var(--font-weight-semibold);
src: local('Comic Sans MS');
}

p {
font-family: 'Foo Sans';
font-weight: var(--font-weight-light);
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Text</title>
<link rel="stylesheet" href="/css/style.css">
</head>
<body>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras interdum ullamcorper elementum. In vitae mi viverra, congue nisi in, accumsan libero. Cras convallis, tortor ac semper molestie, ex orci dapibus dolor, at suscipit enim magna vel ligula. Suspendisse quis tristique urna. Quisque posuere mi sed sem faucibus malesuada. Duis quis orci bibendum sapien rhoncus sollicitudin. Donec sodales magna ut blandit porttitor. Proin dictum augue a justo vehicula auctor.</p>
</body>
</html>

最佳答案

其实browsers不渲染 CSS variable value .取而代之的是 render var(--font-weight-light); .但是你的CSS variable属性会起作用。因为 browser不是编译器。见screenshot .还有这个 rules不支持 IE , Opera以及所有旧版本 browsers . Using CSS custom properties (variables) .

:root {
--font-weight-light: 300;
--font-weight-regular: 400;
--font-weight-bold: 700;
}
div{
font-family: 'Open Sans', sans-serif;
font-weight: var(--font-weight-bold);
}
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,700" rel="stylesheet">

<div>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div>

关于CSS 变量和规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49733602/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com