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

<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>标记的人?


当前回答

DO

给class属性一个值,表明数据的性质(例如,class="footnote"是好的) 为页面创建一个CSS样式表 定义一个CSS样式,它附加到指定给元素的类上 .footnote { 字体样式:斜体; }

不要使用<i>或<em>元素——它们不受支持,并且将数据和表示绑得太紧。 不要给类指定表示规则的值(即不要使用class="italic")。

其他回答

<i>没有错,因为它是非语义的。这是错误的(通常),因为它是表现。关注点分离意味着应该用CSS来传达表示信息。

一般来说,要正确命名可能很棘手,类名也不例外,但无论如何,这是您必须要做的。如果您使用斜体使一个块从正文文本中脱颖而出,那么可能需要一个“flow-distinctive”类名。考虑重用:类名是用于分类的——您还想在其他什么地方做同样的事情?这将帮助您确定一个合适的名称。

<i>包含在HTML5中,但是给出了特定的语义。如果将某些内容标记为斜体的原因符合规范中确定的语义之一,则使用<i>是合适的。否则不。

也许它没有特殊的含义,只需要用斜体字表示,以便在表示上与前面的文本分开。

如果它没有特殊的意义,为什么它需要从它前面的文本中分离出来?这段文字看起来有点奇怪,因为我无缘无故地把它斜体了。

不过我同意你的观点。对于设计师来说,设计在视觉上变化,但没有意义上的变化是很常见的。我经常在盒子上看到这种情况:设计师会给我们设计包括各种颜色、角落、渐变和阴影组合的盒子,风格和内容之间没有任何关系。

因为这些是在不同地方重用的相当复杂的样式(带有相关的Internet Explorer问题),所以我们创建了像box-1、box-2这样的类,以便我们可以重用这些样式。

不过,将一些文本变成斜体的具体示例太琐碎了,不需要担心。最好把这些细枝末节留给语义狂人们去争论。

TLDR

使文本成为斜体的正确方法是忽略这个问题,直到使用CSS,然后根据表示语义设置样式。根据具体情况,您提供的前两个选项可能是正确的。最后两个是错误的。

更详细的解释

在编写标记时不要担心表示。不要用斜体字来思考。从语义的角度思考。如果它需要强调,那么它就是em。如果它与主要内容无关,那么它就是题外话。也许是粗体,也许是斜体,也许是荧光绿色。这与您何时编写标记无关。

当您使用CSS时,您可能已经有了一个语义元素,可以将其在站点中出现的所有内容都用斜体字表示。艾米就是一个很好的例子。但也许你想在你的网站上用斜体显示> ul > li。你必须把标记和表示分开考虑。

正如tledgoat提到的,i在HTML5中是语义的。不过对我来说,语义似乎有点狭隘。使用可能会很少。

em的语义在HTML5中发生了变化,以强调强调。Strong也可以用来表示重要性。Strong可以是斜体,而不是粗体,如果这是你想要的样式。不要让浏览器的样式表限制你。您甚至可以使用reset样式表来帮助您停止在默认值范围内思考。(不过也有一些注意事项。)

Class =“斜体”是不好的。不要用它。它不是语义的,一点也不灵活。表示仍然有语义,只是不同于标记。

class="footnote" is emulating markup semantics and is incorrect as well. Your CSS for the footnote should not be completely unique to your footnote. Your site will look too messy if every part is styled differently. You should have some visual patterns scattered through your pages that you can turn into CSS classes. If your style for your footnotes and your blockquotes are very similar, then you should put the similarities into one class rather than repeat yourself over and over again. You might consider adopting the practices of OOCSS (links below).

关注点和语义的分离在HTML5中很重要。人们通常没有意识到标记并不是语义唯一重要的地方。有内容语义学(HTML),也有表现语义学(CSS)和行为语义学(JavaScript)。它们都有自己独立的语义,对于可维护性和保持DRY来说,这些语义非常重要。

允许资源

面向对象的CSS 面向对象的CSS (OOCSS)简介 如何编写面向对象的CSS 开始学习面向对象的CSS (OOCSS) 面向对象的CSS:什么,如何,为什么 深入研究面向对象的CSS

使用<em>如果你需要一些字/字符斜体在内容没有其他样式。它还有助于使内容具有语义性。

文本样式更适合多种样式,没有语义需求。

好的,首先要注意的是<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。