已弃用的代码:< b> vs style =" font-weight:bold;" [英] Deprecated code: <b> vs style="font-weight:bold;"
问题描述
我一直使用< b>
标记来粗体,因为这是我被教导要做很久以前的方式。但现在我的IDE总是通知我,< b>
已被弃用,并使用CSS样式。假设他们希望我使用< div style =font-weight:bold;>粗体文本< / div>
。这个消息是多么重要,我的IDE给我?我应该返回并将所有< b>
更改为样式吗?
。是否有人可以解释两者之间的差异,以及为什么< b>
现已弃用了?
< b>粗体文字< / b>
Vs。
code>< div style =font-weight:bold;>粗体文字< / div>
会< b>
正确的问题是:什么标记最好描述我的内容?
让我们从< b>
标记开始已弃用):
b元素代表一个文本跨度,按照风格偏移
传达任何额外的重要性,例如文档摘要中的
关键字,评论中的产品名称或其他典型的排版显示为粗体的
跨度文本。
...
如果有更具描述性和
相关标签可用,则不应使用b和i标签。如果你使用它们,通常最好
添加类属性来描述标记的预期含义,
使你可以区分一个用途和另一个。
...
这可能有助于将b或i元素视为一个跨元素
,具有自动备份样式。就像一个span元素,这些
元素通常受益于类名,如果它们是有用的。
http://www.w3.org/International/questions/qa-b-and-i-tags
相比之下,< strong>
有更具体的用途:
strong元素表示具有很高重要性的文本跨度。
http://www.w3.org/TR/html-markup/strong.html
例如:
< p>< strong> ;警告。< / strong>这里是龙。< / p>
这里我们强调警告 / p> 但不是: Item 1并不意味着强调,因此 如果文本的意义具有很强的重要性, 也许你只是想要一个更厚的字体并且文本没有特别的意义。在这种情况下, strong>
< p>< strong& 1:< / strong> Foo,bar和baz。< / p>
< strong>
是错误的标签。
< strong>
是合适的(就像这一行)。
< strong>
或< b>
$ b
< span class =product-name> Hello World< / span>
.product-name {font-weight:bold; }
将
< b>
更好,因为如果有人在
浏览器,它仍会正确显示?
否。为作业使用正确的标记。对于使用您的网站的可视化表示的人来说,禁用样式表是非常不寻常的,但非视觉消费者主要关心文档的结构。 非视觉消费者可以是解析您的内容或屏幕阅读器应用程序的搜索引擎。
其他阅读:
- http://www.w3.org/TR/html51/text-level-semantics.html#the-strong-element
- http://www.w3.org/TR/html51/text-level-semantics.html# the-b元素
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?
Below is an example of both situations. Could someone explain the difference's between both and why <b>
is deprecated now?
<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?
The correct question is: "What markup best describes my content?"
Let's start with the <b>
tag (which is not deprecated):
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.
http://www.w3.org/International/questions/qa-b-and-i-tags
By comparison, <strong>
has a more specific purpose:
The strong element represents a span of text with strong importance.
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.
If the meaning of the text has strong importance, <strong>
is appropriate (just like this line).
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.
<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?
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:
- http://www.w3.org/TR/html51/text-level-semantics.html#the-strong-element
- http://www.w3.org/TR/html51/text-level-semantics.html#the-b-element
这篇关于已弃用的代码:< b> vs style =" font-weight:bold;"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!