Html 弃用代码:<b> vs style="font-weight:bold;"
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18965561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Deprecated code: <b> vs style="font-weight:bold;"
提问by Arian Faurtosh
I've always used <b>
tag to bold something, because that is the way I was taught to do it a long time ago. But now my IDE always informs me that <b>
is deprecated and to use css style. Assuming by that they want me to use <div style="font-weight:bold;">Bold Text</div>
. How vital is this message that my IDE is giving me? Should I go back and change all my<b>
to style?
我一直使用<b>
标记来加粗,因为很久以前我就被教导这样做。但是现在我的 IDE 总是通知我<b>
已弃用并使用 css 样式。假设他们希望我使用<div style="font-weight:bold;">Bold Text</div>
. 我的 IDE 给我的这条消息有多重要?我应该回去改变我所有的<b>
风格吗?
Below is an example of both situations. Could someone explain the difference's between both and why <b>
is deprecated now?
下面是这两种情况的示例。有人可以解释两者之间的区别以及为什么<b>
现在已弃用吗?
<b>Bold Text</b>
Vs.
对比
<div style="font-weight:bold;">Bold Text</div>
Would <b>
be better because if someone has css turned off on the browser, it would still be show correctly?
会<b>
更好,因为如果有人在浏览器上关闭了 css,它仍然会正确显示?
回答by Tim Medora
The correct question is: "What markup best describes my content?"
正确的问题是:“什么标记最能描述我的内容?”
Let's start with the <b>
tag (which is notdeprecated):
让我们从<b>
标签开始(未弃用):
The b element represents a span of text to be stylistically offset from the normal prose without conveying any extra importance, such as key words in a document abstract, product names in a review, or other spans of text whose typical typographic presentation is boldened.
...
You should not use b and i tags if there is a more descriptive and relevant tag available. If you do use them, it is usually better to add class attributes that describe the intended meaning of the markup, so that you can distinguish one use from another.
...
It may help to think of b or i elements as essentially a span element with an automatic fallback styling. Just like a span element, these elements usually benefit from class names if they are to be useful.
b 元素代表一段文本,在文体上与普通散文有所偏移,但不传达任何额外的重要性,例如文档摘要中的关键词、评论中的产品名称,或其他典型排版显示加粗的文本。
...
如果有更具描述性和相关性的标签可用,则不应使用 b 和 i 标签。如果您确实使用它们,通常最好添加描述标记预期含义的类属性,以便您可以将一种用法与另一种用法区分开来。
...
将 b 或 i 元素本质上视为具有自动回退样式的 span 元素可能会有所帮助。就像 span 元素一样,如果这些元素有用的话,它们通常会受益于类名。
http://www.w3.org/International/questions/qa-b-and-i-tags
http://www.w3.org/International/questions/qa-b-and-i-tags
By comparison, <strong>
has a more specific purpose:
相比之下,<strong>
有一个更具体的目的:
The strong element represents a span of text with strong importance.
strong 元素表示具有很强重要性的文本范围。
http://www.w3.org/TR/html-markup/strong.html
http://www.w3.org/TR/html-markup/strong.html
For example:
例如:
<p><strong>Warning.</strong> Here be dragons.</p>
Here we emphasize the word "warning" to stress its importance.
这里我们强调“警告”这个词来强调它的重要性。
But not:
但不是:
<p><strong>Item 1:</strong> Foo, bar, and baz.</p>
"Item 1" isn't meant to be stressed, so <strong>
is the wrong tag. Furthermore, it's possible that the whole structure could be better represented.
“项目 1”并不意味着强调,所以<strong>
是错误的标签。此外,有可能更好地表示整个结构。
If the meaning of the text has strong importance, <strong>
is appropriate (just like this line).
如果文本的含义具有很强的重要性,<strong>
则是合适的(就像这一行)。
Perhaps you just want a thicker font for style purposes and the text has no particular meaning. In that case, neither <strong>
nor <b>
may be appropriate.
也许您只是出于样式目的而想要更粗的字体,而文本没有特别的含义。在这种情况下,既不合适<strong>
也<b>
不合适。
<span class="product-name">Hello World</span>
.product-name { font-weight: bold; }
In all cases:
在所有情况下:
- Use the markup which describes the content.
- Do not use inline styles (use an external stylesheet).
- Do not name styles based on their visual representation (e.g. naming a style "bold" is a poor choice)
- 使用描述内容的标记。
- 不要使用内联样式(使用外部样式表)。
- 不要根据它们的视觉表现来命名样式(例如,将样式命名为“粗体”是一个糟糕的选择)
Would
<b>
be better because if someone has css turned off on the browser, it would still be show correctly?
会
<b>
更好,因为如果有人在浏览器上关闭了 css,它仍然会正确显示?
No. Use the correct markup for the job. It's fairly unusual for someone using the visual representation of your site to willingly disable the stylesheet, but non-visual consumers care primarily about the structure of your document. A "non-visual consumer" could be a search engine parsing your content or a screen reader application.
否。为作业使用正确的标记。使用您网站的视觉表示的人自愿禁用样式表是相当不寻常的,但非视觉消费者主要关心您的文档结构。“非视觉消费者”可以是解析您的内容的搜索引擎或屏幕阅读器应用程序。
Additional Reading:
补充阅读:
回答by Sean Ryan
You should be using <strong>
in place of <b>
. You could use styles (text-weight: bold
in a separate sheet) if a particular group of text was always going to be bold, and you didn't (or couldn't) want to use <strong>
for whatever reason. But I would only go that route if you already were applying other styles to that same element.
您应该使用<strong>
代替<b>
. text-weight: bold
如果一组特定的文本总是要加粗,并且您不想(或不能)<strong>
出于任何原因使用样式,则您可以使用样式(在单独的工作表中)。但是,如果您已经将其他样式应用于同一元素,我只会走这条路。
回答by David
It's not "vital" if the code still works. Though it would conform to current standards which will give the code a longer future.
如果代码仍然有效,这不是“重要的”。尽管它将符合当前的标准,这将使代码具有更长的未来。
The difference is that using CSS separates your styling from your content. <b>
is a style, nothing more. And it tightly couples that markup to that style. The separation allows you to emphasize the markup in other ways instead of always using a bold font.
不同之处在于使用 CSS 将您的样式与您的内容分开。 <b>
是一种风格,仅此而已。并且它将该标记与该样式紧密结合。分离允许您以其他方式强调标记,而不是总是使用粗体。
Would be better because if someone has css turned off on the browser, it would still be show correctly?
会更好,因为如果有人在浏览器上关闭了 css,它仍然会正确显示?
No, because if the user wants to disable styling then your <b>
tag undermines that, because it's mixing styling with content.
不,因为如果用户想要禁用样式,那么您的<b>
标签会破坏这一点,因为它将样式与内容混合在一起。
回答by l2aelba
回答by nathanfirth
I find that using <strong></strong>
is the better approach than using <b>
or inline styles.
我发现使用<strong></strong>
是比使用<b>
或内联样式更好的方法。