新手Web开发者求助:无法修改font-weight与font-size属性
字体字重与字号不生效的解决办法
字重(font-weight)无效的原因及修复
Sacramento字体无粗体变体
你给h1设置了font-weight: bold,但Sacramento字体本身只有400(常规)一种字重,Google Fonts未提供它的粗体版本,所以这个设置完全无效。要么删掉h1的font-weight声明,要么换个带粗体的手写字体。Montserrat未加载所需字重
当前的Google Fonts链接只加载了Montserrat的默认400字重,要是想给h2/h3设置粗体,得在链接里明确指定要加载的字重。把link标签的href改成:
<link href="https://fonts.googleapis.com/css2?family=Merriweather:wght@400;700&family=Montserrat:wght@400;700&family=Sacramento&display=swap" rel="stylesheet" />
改完后,给h2/h3加font-weight: 700就能生效了。
字号(font-size)无效的问题
你的代码里未给元素设置font-size属性,浏览器会使用默认样式的字号。如果是你设置了但没生效,先检查这两点:
- 有没有写错属性名:比如把
font-size写成font-szie这类低级错误; - 优先级问题:如果有其他CSS规则覆盖了你的设置,可临时给属性加
!important测试(不推荐长期使用,建议调整选择器优先级)。
修改后的完整代码
<head> <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=Merriweather:wght@400;700&family=Montserrat:wght@400;700&family=Sacramento&display=swap" rel="stylesheet" /> <style> body { margin: 0; text-align: center; font-family: "Merriweather", serif; font-weight: 700; font-size: 1.1rem; /* 自定义正文字号 */ } h1 { margin-top: 0; font-family: "Sacramento", cursive; font-size: 5rem; /* 自定义h1字号 */ /* 去掉无效的font-weight: bold */ } h2 { font-family: "Montserrat", sans-serif; font-weight: 700; font-size: 2.5rem; /* 自定义h2字号 */ } h3 { font-family: "Montserrat", sans-serif; font-weight: 700; font-size: 1.5rem; /* 自定义h3字号 */ } </style> </head> <body> test text <h1>h1 test</h1> <h2>h2 test</h2> <h3>h3 test</h3> </body>
内容的提问来源于stack exchange,提问作者Utku Orçan
相关产品推荐
相关产品推荐

