跳至正文
Wener Notes

CSS Font

约 2 分钟阅读

默认字体

css
body {
  /* 优先系统默认 */
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans',
    'Droid Sans', 'Helvetica Neue', sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

code {
  font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', monospace;
}

body {
  /* Sans-Serif */
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif, 'Apple Color Emoji',
    'Segoe UI Emoji';

  /* Serif */
  font-family: Iowan Old Style, Apple Garamond, Baskerville, Times New Roman, Droid Serif, Times, Source Serif Pro, serif,
    Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol;

  /* Monospace */
  font-family: ui-monospace, SFMono-Regular, SF Mono, Menlo, Consolas, Liberation Mono, monospace;
}

中文

css
body {
  /* mac 优先 苹方 windows 优先 雅黑 */
  font-family: 'PingFang SC', 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', '微软雅黑', Helvetica, Arial,
    Verdana, sans-serif;
}

font-face

css
@font-face {
  font-family: 'OpenSans';
  /* full name, postscript name, fallback */
  src: local('Open Sans'), local('Open Sans'), url('/fonts/OpenSans.woff');
}

@font-face {
  font-family: 'Montserrat';
  src: local('Montserrat'), url('/assets/fonts/Montserrat.ttf');
  /* 字体支持多种 weight */
  font-weight: 100 900;
}

字体 子集

字体单位如何选择

html
<meta name="viewport" content="width=device-width, initial-scale=1" />
JavaScript
let rem = 14;

if (width > 768) {
  rem = 45;
} else if (width > 414) {
  rem = 26;
} else if (width > 375) {
  rem = 18;
} else if (width > 320) {
  rem = 16;
}
css
html {
  font-size: 16px;

  @media screen and (min-width: 900px) {
    font-size: 18px;
  }

  @media screen and (min-width: 1200px) {
    font-size: 20px;
  }
}

关联信息

反向链接、本文链接的其他页面和外部资料。

参考资料

GitHub3 条
其他外链6 条
最近更新commit a33aca9编辑