如何在网页中使用计算机现代字体?

Cha*_* Xu 31 css fonts

我从来没有在任何网页上看到Computer Modern字体,这是LaTeX类型设置系统的默认字体.

如何更改CSS以使字体实际工作?

Hol*_*ger 56

在网页中使用计算机现代字体变得非常容易!只需在html代码的head部分粘贴以下CSS代码行,即可激活该字体的sans-serif版本.

<style type="text/css">
  @font-face {
    font-family: "Computer Modern";
    src: url('http://mirrors.ctan.org/fonts/cm-unicode/fonts/otf/cmunss.otf');
  }
  @font-face {
    font-family: "Computer Modern";
    src: url('http://mirrors.ctan.org/fonts/cm-unicode/fonts/otf/cmunsx.otf');
    font-weight: bold;
  }
  @font-face {
    font-family: "Computer Modern";
    src: url('http://mirrors.ctan.org/fonts/cm-unicode/fonts/otf/cmunsi.otf');
    font-style: italic, oblique;
  }
  @font-face {
    font-family: "Computer Modern";
    src: url('http://mirrors.ctan.org/fonts/cm-unicode/fonts/otf/cmunbxo.otf');
    font-weight: bold;
    font-style: italic, oblique;
  }

  body {
    font-family: "Computer Modern", sans-serif;
  }
</style>
Run Code Online (Sandbox Code Playgroud)

请注意,此处的解决方案使浏览器从CTAN镜像加载当前版本的字体,这可能非常慢.这可以用于测试目的,但从长远来看,我建议您这些.otf文件下载到您自己的网络服务器.

  • 非常好,霍尔格,我能够做到这一点。还有一件好事,将去 [Font Squirrel 字体生成器](http://www.fontsquirrel.com/fontface/generator) 并将它们从 2.2Mb 压缩到 291Kb。现在我可以拥有它们 o 网站!:) 另外,不要忘记从 [sf cm-unicode 项目](http://cm-unicode.sourceforge.net/download.html) 中获取 .ttf,而不是 .otf。 (3认同)
  • 虽然我的答案被接受了,但这个答案是(更)正确的。Paul Irish 也有一篇关于这个的有趣文章:http://paulirish.com/2009/bulletproof-font-face-implementation-syntax/ (2认同)

dre*_*lse 8

您可以将https://cdn.rawgit.com/dreampulse/computer-modern-web-font/master/fonts.csscss-stylesheet 插入到您的 html 标题中。像这样:

<head>
  <!-- ... -->

  <link rel="stylesheet" type="text/css" href="https://cdn.rawgit.com/dreampulse/computer-modern-web-font/master/fonts.css">
  <style>
    body {
      font-family: "Computer Modern Sans", sans-serif;
    }
  </style>
</head>
Run Code Online (Sandbox Code Playgroud)

您可以将该字体用于具有任意流量的生产网站。文件通过 MaxCDN 的超快速全球 CDN 提供服务。没有流量限制或节流。

Github 上的 README.md


asm*_*ier 7

现在,您可以从此网页下载所需的一切(字体文件和CSS):

http://checkmyworking.com/cm-web-fonts/

那么你唯一需要做的就是将相应的css文件添加到你的html文件的标题部分,如:

<head>
    <meta charset="UTF-8" />
    <title>Test</title>
    <!-- Computer Modern Serif-->
    <link rel="stylesheet" href="fonts/Serif/cmun-serif.css"></link>
    ...
</head>
Run Code Online (Sandbox Code Playgroud)


aaa*_*hat 5

对于 2020 年及以后仍在寻找优化的网络字体而不是.otf上述答案中使用的较大字体的任何人,我已经通过 jsDelivr CDN 托管了 Computer Modern 字体系列。

要使用它,您可以<link>在您的 html 中添加一个,<head>它通过以下地址请求优化字体:

https://cdn.jsdelivr.net/gh/aaaakshat/cm-web-fonts@latest/fonts.css

示例代码:

<head>
  <!-- Other imports... -->
  <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/gh/aaaakshat/cm-web-fonts@latest/fonts.css">
  <style>
    body {
      font-family: "Computer Modern Serif", serif;
    }
  </style>
</head>
Run Code Online (Sandbox Code Playgroud)

此处查看文档