株洲市文章资讯

HTML5中使用Noto Sans CJK字体的详细步骤

2026-05-17 08:47:02 浏览次数:2
详细信息

方法1:通过Google Fonts CDN(推荐)

步骤:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>使用Noto Sans CJK</title>

    <!-- 引入Noto Sans CJK SC(简体中文) -->
    <link href="https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@100;300;400;500;700;900&display=swap" rel="stylesheet">

    <!-- 或者引入完整CJK版本 -->
    <!-- <link href="https://fonts.googleapis.com/css2?family=Noto+Sans+JP&family=Noto+Sans+KR&family=Noto+Sans+SC&family=Noto+Sans+TC&display=swap" rel="stylesheet"> -->

    <style>
        * {
            font-family: 'Noto Sans SC', sans-serif;
        }

        .content {
            font-weight: 400; /* 正常粗细 */
            line-height: 1.6;
        }

        .bold-text {
            font-weight: 700; /* 粗体 */
        }

        .light-text {
            font-weight: 300; /* 细体 */
        }
    </style>
</head>
<body>
    <h1>Noto Sans CJK 示例</h1>
    <p class="content">这是一段使用Noto Sans SC字体的文本内容。</p>
    <p class="bold-text">这是粗体文本</p>
    <p class="light-text">这是细体文本</p>
</body>
</html>

不同语言的变体:

方法2:自托管字体文件

步骤1:下载字体文件

从Google Fonts或GitHub下载:

https://github.com/googlefonts/noto-cjk

步骤2:准备字体文件

常用格式:

步骤3:CSS @font-face定义

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>自托管Noto Sans CJK</title>
    <style>
        /* 定义简体中文字体 */
        @font-face {
            font-family: 'Noto Sans SC';
            font-style: normal;
            font-weight: 400;
            src: url('fonts/NotoSansSC-Regular.woff2') format('woff2'),
                 url('fonts/NotoSansSC-Regular.woff') format('woff');
            font-display: swap;
        }

        @font-face {
            font-family: 'Noto Sans SC';
            font-style: normal;
            font-weight: 700;
            src: url('fonts/NotoSansSC-Bold.woff2') format('woff2'),
                 url('fonts/NotoSansSC-Bold.woff') format('woff');
            font-display: swap;
        }

        /* 定义繁体中文字体(可选) */
        @font-face {
            font-family: 'Noto Sans TC';
            font-style: normal;
            font-weight: 400;
            src: url('fonts/NotoSansTC-Regular.woff2') format('woff2');
            font-display: swap;
        }

        body {
            font-family: 'Noto Sans SC', 'Noto Sans TC', sans-serif;
            font-weight: 400;
        }

        h1, h2, h3 {
            font-weight: 700;
        }

        /* 字体降级策略 */
        .font-stack {
            font-family: 'Noto Sans SC', 
                         'Noto Sans TC', 
                         'Microsoft YaHei', 
                         'Hiragino Sans GB', 
                         sans-serif;
        }
    </style>
</head>
<body>
    <h1>自托管Noto Sans CJK</h1>
    <p>使用本地字体文件,加载更快,不依赖外部CDN。</p>
</body>
</html>

方法3:使用CSS变量管理

<style>
    :root {
        --font-cjk-sc: 'Noto Sans SC', sans-serif;
        --font-cjk-tc: 'Noto Sans TC', sans-serif;
        --font-fallback: -apple-system, BlinkMacSystemFont, 'Segoe UI', 
                         'Microsoft YaHei', sans-serif;
    }

    body {
        font-family: var(--font-cjk-sc), var(--font-fallback);
    }

    /* 繁体中文内容 */
    .traditional-chinese {
        font-family: var(--font-cjk-tc), var(--font-fallback);
    }

    /* 响应式字体大小 */
    html {
        font-size: 16px;
    }

    @media (min-width: 768px) {
        html {
            font-size: 18px;
        }
    }
</style>

性能优化建议

1. 字体加载策略

@font-face {
    font-family: 'Noto Sans SC';
    src: url('fonts/NotoSansSC-Regular.woff2') format('woff2');
    font-display: swap; /* 使用swap避免FOIT(不可见文本闪烁) */
    font-weight: 400;
    font-style: normal;
}

2. 字体子集化(减少文件大小)

<!-- 仅加载需要的字符集 -->
<link href="https://fonts.googleapis.com/css2?family=Noto+Sans+SC&display=swap&subset=chinese-simplified" rel="stylesheet">

3. 预加载关键字体

<head>
    <!-- 预加载关键字体 -->
    <link rel="preload" 
          href="https://fonts.gstatic.com/s/notosanssc/v26/...Regular.woff2" 
          as="font" 
          type="font/woff2" 
          crossorigin>
</head>

完整示例

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>完整示例 - Noto Sans CJK</title>

    <!-- Google Fonts CDN -->
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">

    <style>
        :root {
            --font-primary: 'Noto Sans SC', -apple-system, BlinkMacSystemFont, 
                           'Segoe UI', 'Microsoft YaHei', sans-serif;
            --color-text: #333;
            --color-heading: #222;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: var(--font-primary);
            color: var(--color-text);
            line-height: 1.7;
            padding: 20px;
            max-width: 800px;
            margin: 0 auto;
            background-color: #f9f9f9;
        }

        h1, h2, h3, h4 {
            color: var(--color-heading);
            margin: 1.5em 0 0.8em;
            font-weight: 700;
            line-height: 1.3;
        }

        h1 { font-size: 2.5rem; }
        h2 { font-size: 2rem; }
        h3 { font-size: 1.5rem; }

        p {
            margin-bottom: 1.2em;
            font-weight: 400;
        }

        .light { font-weight: 300; }
        .regular { font-weight: 400; }
        .medium { font-weight: 500; }
        .bold { font-weight: 700; }

        .article {
            background: white;
            padding: 40px;
            border-radius: 8px;
            box-shadow: 0 2px 20px rgba(0,0,0,0.1);
        }

        @media (max-width: 768px) {
            body {
                padding: 10px;
            }

            .article {
                padding: 20px;
            }

            h1 { font-size: 2rem; }
            h2 { font-size: 1.5rem; }
        }
    </style>
</head>
<body>
    <article class="article">
        <h1>Noto Sans CJK字体演示</h1>
        <p class="regular">Noto Sans CJK是Google推出的开源字体系列,全面支持简体中文、繁体中文、日文和韩文。字体设计优美,阅读体验舒适。</p>

        <h2>不同字重示例</h2>
        <p class="light">细体 (300) - 用于辅助文本或需要轻盈感的内容</p>
        <p class="regular">常规体 (400) - 正文阅读的最佳选择</p>
        <p class="medium">中粗体 (500) - 用于强调或中等重要性的内容</p>
        <p class="bold">粗体 (700) - 用于标题或需要强烈强调的内容</p>

        <h2>跨语言支持</h2>
        <p>简体中文:今天天气真好,适合外出散步。</p>
        <p>繁体中文:今天天氣真好,適合外出散步。</p>
        <p>日本語:今日はいい天気で、散歩にぴったりです。</p>
        <p>한국어:오늘 날씨가 좋아서 산책하기 딱 좋아요.</p>
    </article>
</body>
</html>

注意事项

版权:Noto Sans CJK是开源字体(SIL Open Font License),可免费商用 文件大小:完整CJK字体包较大,建议按需加载或使用子集 兼容性:现代浏览器都支持,旧版IE需要提供TTF格式 性能:使用font-display: swap避免布局偏移,预加载关键字体 字体降级:始终提供备用字体栈,确保内容可访问性

推荐使用Google Fonts CDN方式,简单方便且有良好的缓存策略。对于国内项目,可以考虑使用国内CDN或自托管以提高访问速度。

相关推荐