上一篇
html字体代码大全
- 行业动态
- 2025-05-11
- 4
HTML字体控制通过标签实现,如设大小、颜色、族,/加粗斜体,现多
HTML字体标签与CSS属性
基础字体标签(已废弃,建议使用CSS)
作用 | 示例 | |
---|---|---|
<font> | 设置字体 | <font face="宋体">文字</font> |
<b> | 粗体 | <b>加粗</b> |
<i> | 斜体 | <i>斜体</i> |
<u> | 下划线 | <u>下划线</u> |
<small> | 缩小字体 | <small>小字</small> |
<sup> | 上标 | x<sup>2</sup> |
<sub> | 下标 | H<sub>2</sub>O |
CSS字体属性
属性 | 说明 | 示例 |
---|---|---|
font-family | 字体族(可多选) | p { font-family: "微软雅黑", Arial, sans-serif; } |
font-size | 字体大小 | h1 { font-size: 32px; } |
font-weight | 粗细(100-900) | strong { font-weight: bold; } |
font-style | 样式(normal/italic) | em { font-style: italic; } |
font-variant | 小型大写 | small-caps { font-variant: small-caps; } |
line-height | 行高 | p { line-height: 1.6; } |
font | 复合属性 | body { font: 14px/1.5 "宋体"; } |
Google字体引入方法
- 选择字体:访问 Google Fonts 搜索字体
- 复制链接代码:
<link href="https://fonts.googleapis.com/css2?family=Noto+Sans+SC&display=swap" rel="stylesheet">
- 使用字体:
body { font-family: 'Noto Sans SC', sans-serif; }
字体图标库(如Font Awesome)
- 引入图标库:
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
- 使用图标:
<i class="fas fa-camera"></i> <!-实心图标 --> <i class="far fa-camera"></i> <!-空心图标 -->
常见问题与解答
Q1:如何同时设置字体大小、颜色和粗细?
A:推荐使用CSS复合属性或单独声明:
h2 { font-size: 24px; color: #333; font-weight: 700; } / 或简写 / h2 { font: bold 24px/1.5 "楷体"; color: #333; }
Q2:为什么网页显示不出自定义字体?
A:可能原因及解决方案:
- 字体未加载:检查
@font-face
或外部链接是否正确 - 跨域问题:确保字体文件允许跨域访问(现代浏览器已自动处理)
- 后备字体缺失:指定通用后备字体(如
sans-serif
) - 文件路径错误