html - 引用的字体 'sans-serif' 在 CSS 中无法正常工作

标签 html css fonts

我正在编写一个小型静态网站(HTML 和 CSS),并偶然发现了属性 font-family 中的行为,但我不明白该行为。我喜欢以漂亮、干净的外观编写代码,这就是为什么总是在 CSS 中引用字体。

据我了解CSS2.2 specification允许使用正确的、带引号的字体:

Font family names must either be given quoted as strings, or unquoted as a sequence of one or more identifiers.

不幸的是,它在我的情况下无法正常工作。我在下面附上了一个例子。

.test1
{
    font-family: 'sans-serif';
}

.test2
{
    font-family: 'arial';
}

.test3
{
    font-family: sans-serif;
}
<h1 class="test1">Test</h1>
<h1 class="test2">Test</h1>
<h1 class="test3">Test</h1>

我使用 Google Chrome 54.0.2840.99 和 Internet Explorer 11.0.9600.18450 测试了该属性。为什么引用的字体对 arial 有效,但对 sans-serif 无效?

最佳答案

Sans-serif 不是字体名称,而是字体类型... 为了使字体工作,您必须至少输入字体名称! 字体名称为必填项。 例如

.myclass{font-family: 'myFontName', sans-serif;}
  • family-name - The name of a font-family, like "times", "courier", "arial", etc.
  • generic-family - The name of a generic-family, like "serif", "sans-serif", "cursive", "fantasy", "monospace".

Source

关于html - 引用的字体 'sans-serif' 在 CSS 中无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40783931/

相关文章:

javascript - Body Background Image Rotation 应支持动态图像数量

html - @Font face 在任何浏览器中都不起作用

python - 在 PIL 中用 Webdings 绘图

mobile - 为什么使用 LWUIT 的三星手机不支持阿拉伯字体?

linux - 如何更改 Visual Studio Code 用户界面的字体?

javascript - 为什么我的 <div> 有抖动? onmouseleave 事件在元素上触发

html - 更改导航栏字体

jquery - 如何在可折叠内容面板内创建按钮并使内容可见并隐藏在其中?

css - 如何截断CSS中的段落行

css - 在容器内用 flexbox 填充高度