font-weight属性用于控制文本粗细,支持normal、bold及100-900数字值;其生效依赖字体是否包含对应字重,否则会就近回退;可通过@font-face引入多字重字体或使用可变字体实现精细控制;实际项目中推荐结合CSS变量管理字重,提升维护性与一致性。

在CSS中,我们主要通过
font-weight
font-weight
最常见的几种值包括:
normal
400
bold
700
lighter
bolder
100
900
font-weight: 300;
font-weight: 600;
这些数字值并非凭空出现,它们背后对应的是字体设计者在制作字体时预设的各种字重。浏览器会尝试匹配最接近的可用字重。
立即学习“前端免费学习笔记(深入)”;
/* 示例 */
p {
font-weight: normal; /* 默认粗细 */
}
h1 {
font-weight: bold; /* 粗体 */
}
.light-text {
font-weight: 300; /* 细体 */
}
.semi-bold {
font-weight: 600; /* 半粗体 */
}font-weight
这其实是一个挺常见的“坑”,很多初学者都遇到过。当你尝试设置
font-weight: 300
500
normal
bold
字体文件在设计时,并不会包含从100到900的所有字重。很多字体可能只提供了
Regular
bold
font-weight: 300
normal
font-weight: 600
解决这个问题的关键在于了解你所使用的字体。
检查字体文件: 如果你使用的是自定义字体(通过
@font-face
font-weight: 300
YourFont-Light.woff2
@font-face
font-weight
300
@font-face {
font-family: 'MyCustomFont';
src: url('fonts/MyCustomFont-Light.woff2') format('woff2');
font-weight: 300; /* 明确声明这个文件对应的字重 */
font-style: normal;
}
@font-face {
font-family: 'MyCustomFont';
src: url('fonts/MyCustomFont-Regular.woff2') format('woff2');
font-weight: 400; /* 默认字重 */
font-style: normal;
}
/* ... 其他字重文件 ... */
body {
font-family: 'MyCustomFont', sans-serif;
font-weight: 300; /* 现在这个300才可能生效 */
}使用系统字体: 对于系统字体(如 Arial, Helvetica, Microsoft YaHei),它们通常只提供有限的字重。如果你需要更丰富的字重选择,考虑使用Google Fonts或Adobe Fonts等服务,它们提供了大量拥有多字重的字体。
了解字体设计: 有些字体天生就没有很细的字重,比如一些手写体或艺术字体。在这种情况下,即使你设置了
font-weight: 100
所以,当
font-weight
font-weight
当然有,虽然
font-weight
text-shadow
text-shadow
.faux-bold {
font-weight: normal; /* 保持正常字重 */
color: #333;
text-shadow: 0.5px 0 0 #333; /* 向右偏移0.5px,颜色相同 */
/* 也可以是多个阴影叠加,增强效果 */
/* text-shadow: 0.5px 0 0 #333, -0.5px 0 0 #333; */
}
.faux-thinner {
/* 这种方法更多用于加粗,变细比较难通过阴影实现,
除非是背景色阴影,但效果不自然 */
}这种方法的好处是灵活,但缺点是可能会让文本边缘显得不那么锐利,或者在某些浏览器下渲染效果不一致。
CSS transform: scale()
transform: scale()
font-size
Variable Fonts (可变字体) 和 font-variation-settings
font-weight
font-variation-settings
/* 假设你引入了一个支持 'wght' 轴的可变字体 */
@font-face {
font-family: 'MyVariableFont';
src: url('fonts/MyVariableFont.woff2') format('woff2');
/* 声明字体支持的字重范围,例如从100到900 */
font-weight: 100 900;
font-style: normal;
}
.super-fine {
font-family: 'MyVariableFont', sans-serif;
font-variation-settings: 'wght' 150; /* 设置字重到150,非常精细 */
}
.extra-bold {
font-family: 'MyVariableFont', sans-serif;
font-variation-settings: 'wght' 850; /* 设置字重到850 */
}使用可变字体,你甚至可以设置
font-weight: 543
font-weight
在实际的Web项目中,选择
font-weight
优先使用语义化关键词 (normal
bold
p
li
font-weight: normal;
h1
h6
font-weight: bold;
当设计有明确的字重需求时,使用数字值: 如果你的设计稿明确指定了某个文本需要
300
500
600
利用CSS变量(Custom Properties)进行字重管理: 为了提高代码的可维护性和一致性,我强烈建议在项目中定义一套字体粗细的CSS变量。这样,设计师如果需要调整某个字重,你只需要修改一处变量定义,而不是去查找和替换所有使用该字重的地方。
:root {
--font-weight-light: 300;
--font-weight-regular: 400;
--font-weight-medium: 500;
--font-weight-semibold: 600;
--font-weight-bold: 700;
--font-weight-extrabold: 800;
}
h1 {
font-weight: var(--font-weight-bold);
}
.subtitle {
font-weight: var(--font-weight-medium);
}
.body-text {
font-weight: var(--font-weight-regular);
}这种做法不仅让代码更清晰,也为响应式设计提供了便利,你可以通过媒体查询来调整这些变量的值,从而在不同屏幕尺寸下调整字重。
考虑性能: 每引入一个字重的字体文件,都会增加页面的加载时间。所以,在选择字重时,要有所取舍,只加载那些你实际会用到的字重。例如,如果你的设计中只用到了
300
400
700
总结一下,
font-weight
以上就是CSS字体粗细怎么控制_CSS字体粗细控制属性介绍的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号