使文本变成斜体的正确方法是什么?我看到了以下四种方法:

<i>Italic Text</i>

<em>Italic Text</em>

<span class="italic">Italic Text</span>

<span class="footnote">Italic Text</span>

<i>

这是“老办法”。<i>没有语义意义,只传达了使文本变成斜体的表示效果。在我看来,这显然是错误的,因为这是非语义的。


<em>

它使用语义标记来实现纯粹的表示目的。它只是发生在默认情况下,<em>以斜体显示文本,所以它经常被那些知道<i>应该避免,但不知道其语义的人使用。并非所有的斜体文本都是斜体,因为它被强调了。有时,它可以是完全相反的,就像一个边注或耳语。


< span class = "斜体”>

This uses a CSS class to place presentation. This is often touted as the correct way but again, this seems wrong to me. This doesn't appear to convey any more semantic meaning that <i>. But, its proponents cry, it is much easier to change all your italic text later if you, say, wanted it bold. Yet this is not the case because I would then be left with a class called "italic" that rendered text bold. Furthermore, it is not clear why I would ever want to change all italic text on my website or at least we can think of cases in which this would not be desirable or necessary.


<跨越阶层= footnote”>

它使用CSS类来实现语义。到目前为止,这似乎是最好的方法,但实际上它有两个问题。

Not all text has sufficient meaning to warrant semantic markup. For example, is italicised text at the bottom of the page really a footnote? Or is it an aside? Or something else entirely. Perhaps it has no special meaning and only needs to be rendered in italics to separate it presentationally from the text preceding it. Semantic meaning can change when it is not present in sufficient strength. Lets say I went along with "footnote" based upon nothing more than the text being at the bottom of the page. What happens when a few months later I want to add more text at the bottom? It is no longer a footnote. How can we choose a semantic class that is less generic than <em> but avoids these problems?


总结

在许多情况下,语义的要求似乎过于繁重,在这些情况下,使某些内容成为斜体的愿望并不意味着具有语义意义。

此外,将样式与结构分离的愿望导致CSS被吹捧为<i>的替代品,当它实际上没有那么有用的时候。所以这让我回到卑微的<i>标签,并想知道这条思路是否是它留在HTML5规范中的原因?

有没有关于这个主题的好博客或文章?也许是那些参与决定保留/创建<i>标记的人?


当前回答

我不是专家,但我想说,如果您真的想要语义化,您应该使用词汇表(RDFa)。

结果应该是这样的:

<em property="italic" href="http://url/to/a/definition_of_italic"> Your text </em>

Em用于表示(人类将看到它是斜体),属性和href属性链接到斜体的定义(对于机器)。

你应该检查是否有这样的词汇表,也许属性已经存在。

更多关于RDFa的信息,请访问:http://www.alistapart.com/articles/introduction-to-rdfa/

其他回答

HTML斜体文本以斜体格式显示文本。

<i>HTML italic text example</i>

强调和强烈元素都可以用来增加某些单词或句子的重要性。

<p>This is <em>emphasized </em> text format <em>example</em></p>

当您想要强调文本中的某个点时,而不是当您想要斜体时,应该使用强调标记。

有关更多信息,请参阅本指南:HTML文本格式

i元素是非语义的,所以对于屏幕阅读器、Googlebot等来说,它应该是某种透明的(就像span或div元素一样)。但对于开发人员来说,这不是一个好的选择,因为它将表示层与结构层连接起来——这是一个糟糕的实践。

Em元素(也很强)应该总是在语义上下文中使用,而不是表示上下文中。当某个词或句子很重要时,就必须使用这个短语。就拿上一句中的例子来说,我应该用它们来强调“应该一直使用”这部分。浏览器为这些元素提供了一些默认的CSS属性,但是如果您的设计需要这样做来保持这些元素的正确语义,那么您可以并且应该重写这些默认值。

<span class="italic"> italic Text</span>是最错误的方式。首先,它使用起来很不方便。其次,建议正文采用斜体。而结构层(HTML、XML等)不应该这样做。表示应该始终与结构分离。

<span class="footnote">斜体Text</span>似乎是脚注的最佳方式。它不建议任何表示,只是描述标记。你无法预测剧情中会发生什么。如果一个脚注将在特性中增加,您可能会被迫更改它的类名(以在代码中保留一些逻辑)。

所以当你有一些重要的文本时,用em或strong来强调。但是请记住,这些元素是内联元素,不应该用于强调大块的文本。

如果你只关心事物的外观,并且总是尽量避免任何额外的标记,那么就使用CSS。

你应该针对不同的用例使用不同的方法:

If you want to emphasise a phrase, use <em>. The <i> tag has a new meaning in HTML5, representing "a span of text in an alternate voice or mood". So you should use this tag for things like thoughts/asides or idiomatic phrases. The spec also suggests ship names (but no longer suggests book/song/movie names; use <cite> for that instead). If the italicised text is part of a larger context, say an introductory paragraph, you should attach the CSS style to the larger element, i.e. p.intro { font-style: italic; }

好的,首先要注意的是<i>已弃用,不应该使用<i>已弃用,但我仍然不建议使用它——查看评论了解详细信息。这是因为它完全违背了将表示保留在表示层,正如您所指出的那样。类似地,<span class="italic">似乎也打破了这个模式。

So now we have two real ways of doing things: <em> and <span class="footnote">. Remember that em stands for emphasis. When you wish to apply emphasis to a word, phrase or sentence, stick it in <em> tags regardless of whether you want italics or not. If you want to change the styling in some other way, use CSS: em { font-weight: bold; font-style: normal; }. Of course, you can also apply a class to the <em> tag: if you decide you want certain emphasised phrases to show up in red, give them a class and add it to the CSS:

Fancy some <em class="special">shiny</em> text?

em { font-weight: bold; font-style: normal; }
em.special { color: red; }

如果由于其他原因使用斜体,则使用其他方法并为该节指定一个类。这样,您就可以随时更改样式,而无需调整HTML。在你的例子中,脚注不应该被强调——事实上,它们应该被淡化,因为脚注的重点是显示不重要但有趣或有用的信息。在这种情况下,最好将一个类应用到脚注中,并使其看起来像表示层中的一个类—CSS。

我不是专家,但我想说,如果您真的想要语义化,您应该使用词汇表(RDFa)。

结果应该是这样的:

<em property="italic" href="http://url/to/a/definition_of_italic"> Your text </em>

Em用于表示(人类将看到它是斜体),属性和href属性链接到斜体的定义(对于机器)。

你应该检查是否有这样的词汇表,也许属性已经存在。

更多关于RDFa的信息,请访问:http://www.alistapart.com/articles/introduction-to-rdfa/