简体   繁体   English

CSS-不需要字体粗细属性吗?

[英]CSS - Is the Font Weight property not necessary?

I was looking at some code, and saw someone mention the font-weight property in CSS 我在看一些代码,看到有人提到CSS中的font-weight属性

font-weight: normal;

Now, I just checked W3Schools, and they said font-weight: normal is default. 现在,我刚刚检查了W3Schools,他们说font-weight:normal是默认值。

So does that mean you don't need to write that line at all? 那么这是否意味着您根本不需要编写该行? Is it unnecessary? 没必要吗?

W3schools is wrong, as usual. 与往常一样,W3schools是错误的。 The initial value of font-weight is normal , but initial value is not the same as default. font-weight初始值normal ,但初始值与默认值不同。 Browsers may have whatever default values they like for properties of elements. 浏览器可能具有其喜欢的元素属性默认值。 In practice, they tend to be relatively uniform in this. 实际上,它们在这方面往往相对统一。 Typically, several elements such as h1 , strong , th , and of course b have, by browser defaults, font-weight: bold set for them. 通常,根据浏览器的默认值, h1strongthb等几个元素都有font-weight: bold设置。

This means that if you do not want a first-level heading to appear in bold face, you need to declare the property explicitly: 这意味着,如果您希望第一级标题以粗体显示,则需要显式声明该属性:

h1 { font-weight: normal }

It is necessary when you have certain other styling options that could influence the text. 如果您有其他可能影响文本的样式选项,则很有必要。 It's better to have it there and make sure it doesn't get changed by anything else. 最好把它放在那里,并确保它不会被其他任何东西改变。 And not every browser starts with all CSS the same way. 并非每个浏览器都以相同的方式启动所有CSS。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM