简体   繁体   English

CSS/HTML:使文本斜体的正确方法是什么?

[英]CSS/HTML: What is the correct way to make text italic?

What is the correct way to make text italic?使文本斜体的正确方法是什么? I have seen the following four approaches:我见过以下四种方法:

<i>Italic Text</i>

<em>Italic Text</em>

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

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

<i>

This is the "old way".这是“老方法”。 <i> has no semantic meaning and only conveys the presentational effect of making the text italic. <i>没有语义,仅传达使文本斜体的表现效果。 As far as I can see, this is clearly wrong because this is non-semantic.据我所知,这显然是错误的,因为这是非语义的。


<em>

This uses semantic mark up for purely presentational purposes.这将语义标记用于纯粹的展示目的。 It just happens that <em> by default renders text in italic and so it is often used by those who are aware that <i> should be avoided but who are unaware of its semantic meaning.碰巧<em>默认以斜体呈现文本,因此它经常被那些知道应该避免<i>但不知道其语义含义的人使用。 Not all italic text is italic because it is emphasised.并非所有斜体文本都是斜体,因为它被强调。 Sometimes, it can be the exact opposite, like a side note or a whisper.有时,它可能正好相反,例如旁注或耳语。


<span class="italic">

This uses a CSS class to place presentation.这使用 CSS 类来放置演示文稿。 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> .这似乎没有传达任何更多的语义含义<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.此外,不清楚为什么我会想要更改我网站上的所有斜体文本,或者至少我们可以想到不希望或不需要这样做的情况。


<span class="footnote">

This uses a CSS class for semantics.这使用 CSS 类进行语义。 So far this appears to be the best way but it actually has two problems.到目前为止,这似乎是最好的方法,但实际上有两个问题。

  1. 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.也许它没有特殊含义,只需要以斜体呈现,以便在外观上将其与前面的文本分开。

  2. 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?我们如何选择一个比<em>更不通用但避免这些问题的语义类?


Summary概括

It appears that the requirement of semantics seems to be overly burdensome in many instances where the desire to make something italic is not meant to carry semantic meaning.在许多情况下,语义的要求似乎过于繁重,在这种情况下,希望将某些东西变成斜体并不意味着要承载语义。

Furthermore, the desire to separate style from structure has led CSS to be touted as a replacement to <i> when there are occasions when this would actually be less useful.此外,将样式与结构分开的愿望导致 CSS 被吹捧为<i>的替代品,但在某些情况下,这实际上不太有用。 So this leaves me back with the humble <i> tag and wondering whether this train of thought is the reason why it is left in the HTML5 spec?所以这让我回到了不起眼的<i>标签,并想知道这种思路是否是它留在 HTML5 规范中的原因?

Are there any good blog posts or articles on this subject as well?有没有关于这个主题的好的博客文章或文章? Perhaps by those involved in the decision to retain/create the <i> tag?也许是那些参与决定保留/创建<i>标签的人?

You should use different methods for different use cases:您应该针对不同的用例使用不同的方法:

  1. If you want to emphasise a phrase, use <em> .如果要强调一个短语,请使用<em>
  2. The <i> tag has a new meaning in HTML5 , representing "a span of text in an alternate voice or mood". <i>标签在 HTML5 中有一个新的含义,代表“一段文本以另一种声音或情绪”。 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).规范还建议船名(但不再建议书/歌曲/电影名称;使用<cite>代替)。
  3. If the italicised text is part of a larger context, say an introductory paragraph, you should attach the CSS style to the larger element, ie p.intro { font-style: italic; }如果斜体文本是较大上下文的一部分,例如介绍性段落,则应将 CSS 样式附加到较大的元素,即p.intro { font-style: italic; } p.intro { font-style: italic; }

<i> is not wrong because it is non-semantic. <i>没有错,因为它是非语义的。 It's wrong (usually) because it's presentational.这是错误的(通常),因为它是展示性的。 Separation of concern means that presentional information should be conveyed with CSS.关注点分离意味着表现性信息应该用 CSS 传达。

Naming in general can be tricky to get right, and class names are no exception, but nevertheless it's what you have to do.一般来说,正确命名可能很棘手,类名也不例外,但无论如何这是你必须做的。 If you're using italics to make a block stand out from the body text, then maybe a class name of "flow-distinctive" would be in order.如果您使用斜体来使块从正文中脱颖而出,那么可能需要使用“flow-distinctive”类名。 Think about reuse: class names are for categorization - where else would you want to do the same thing?考虑重用:类名用于分类——您还想在哪里做同样的事情? That should help you identify a suitable name.这应该可以帮助您确定合适的名称。

<i> is included in HTML5, but it is given specific semantics. <i>包含在 HTML5 中,但它被赋予了特定的语义。 If the reason why you are marking something up as italic meets one of the semantics identified in the spec, it would be appropriate to use <i> .如果将某些内容标记为斜体的原因符合规范中确定的语义之一,则使用<i>是合适的。 Otherwise not.否则不行。

I'm no expert but I'd say that if you really want to be semantic, you should use vocabularies (RDFa).我不是专家,但我想说,如果你真的想要语义化,你应该使用词汇表 (RDFa)。

This should result in something like that:这应该导致这样的事情:

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

em is used for the presentation (humans will see it in italic) and the property and href attributes are linking to a definition of what italic is (for machines). em用于表示(人类会以斜体看到它),并且propertyhref属性链接到斜体的定义(对于机器)。

You should check if there's a vocabulary for that kind of thing, maybe properties already exist.您应该检查是否有这种东西的词汇表,也许属性已经存在。

More info about RDFa here: http://www.alistapart.com/articles/introduction-to-rdfa/有关 RDFa 的更多信息,请访问: http : //www.alistapart.com/articles/introduction-to-rdfa/

TLDR TLDR

The correct way to make text italic is to ignore the problem until you get to the CSS, then style according to presentational semantics.使文本斜体的正确方法是忽略问题,直到进入 CSS,然后根据表示语义设置样式。 The first two options you provided could be right depending on the circumstances.您提供的前两个选项可能是正确的,具体取决于具体情况。 The last two are wrong.最后两个是错误的。

Longer Explanation更长的解释

Don't worry about presentation when writing the markup.编写标记时不要担心演示文稿。 Don't think in terms of italics.不要用斜体来思考。 Think in terms of semantics.从语义的角度考虑。 If it requires stress emphasis, then it's an em .如果它需要强调强调,那么它是一个em If it's tangential to the main content, then it's an aside .如果它与主要内容相切,那么它就在aside Maybe it'll be bold, maybe it'll be italic, maybe it'll be fluorescent green.也许它会是粗体,也许它会是斜体,也许它会是荧光绿色。 It doesn't matter when you're writing markup.编写标记时无关紧要。

When you do get to the CSS, you might already have a semantic element that makes sense to put italics for all its occurrences in your site.当您使用 CSS 时,您可能已经有了一个语义元素,可以在您的站点中为所有出现的地方添加斜体。 em is a good example. em就是一个很好的例子。 But maybe you want all aside > ul > li on your site in italics.但也许您希望在您的网站上以斜体显示所有aside > ul > li You have to separate thinking about the markup from thinking about the presentation.您必须将考虑标记与考虑演示文稿分开。

As mentioned by DisgruntledGoat , i is semantic in HTML5.正如DisgruntledGoat所提到的, i在 HTML5 中是语义的。 The semantics seem kind of narrow to me, though.不过,语义对我来说似乎有点狭窄。 Use will probably be rare.使用可能很少。

The semantics of em have changed in HTML5 to stress emphasis. em的语义在 HTML5 中发生了变化以强调强调。 strong can be used to show importance as well. strong也可以用来表示重要性。 strong can be italic rather than bold if that's how you want to style it.如果您希望这样设置样式, strong可以是斜体而不是粗体。 Don't let the browser's stylesheet limit you.不要让浏览器的样式表限制您。 You can even use a reset stylesheet to help you stop thinking within the defaults.您甚至可以使用重置样式表来帮助您停止思考默认值。 (Though there are some caveats .) (虽然有一些警告。)

class="italic" is bad. class="italic"不好。 Don't use it.不要使用它。 It is not semantic and is not flexible at all.它没有语义,根本不灵活。 Presentation still has semantics, just a different kind from markup.表示仍然具有语义,只是与标记不同。

class="footnote" is emulating markup semantics and is incorrect as well. class="footnote"模拟标记语义,也是不正确的。 Your CSS for the footnote should not be completely unique to your footnote.脚注的 CSS 不应与脚注完全相同。 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.您应该有一些散布在您的页面中的视觉模式,您可以将它们转换为 CSS 类。 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).您可以考虑采用 OOCSS 的做法(以下链接)。

Separation of concerns and semantics are big in HTML5.关注点和语义的分离在 HTML5 中很重要。 People often don't realize that the markup isn't the only place where semantics is important.人们通常没有意识到,标记并不是语义很重要的唯一地方。 There is content semantics (HTML), but there is also presentational semantics (CSS) and behavioral semantics (JavaScript) as well.有内容语义 (HTML),但也有表示语义 (CSS) 和行为语义 (JavaScript)。 They all have their own separate semantics that are important to pay attention to for maintainability and staying DRY.它们都有自己独立的语义,这些语义对于可维护性和保持 DRY 很重要。

OOCSS Resources OOCSS 资源

Perhaps it has no special meaning and only needs to be rendered in italics to separate it presentationally from the text preceding it.也许它没有特殊含义,只需要以斜体呈现,以便在外观上将其与前面的文本分开。

If it has no special meaning, why does it need to be separated presentationally from the text preceding it?如果它没有特殊含义,为什么需要将它与前面的文本分开表示? This run of text looks a bit weird , because I've italicised it for no reason.这段文字看起来有点奇怪,因为我无缘无故地将它斜体了。

I do take your point though.不过我确实接受你的观点。 It's not uncommon for designers to produce designs that vary visually, without varying meaningfully.对于设计师来说,设计在视觉上有所不同而没有有意义的变化的情况并不少见。 I've seen this most often with boxes: designers will give us designs including boxes with various combinations of colours, corners, gradients and drop-shadows, with no relation between the styles, and the meaning of the content.我经常在盒子上看到这种情况:设计师会给我们设计包括各种颜色、角、渐变和阴影组合的盒子,样式和内容的含义之间没有关系。

Because these are reasonably complex styles (with associated Internet Explorer issues) re-used in different places, we create classes like box-1 , box-2 , so that we can re-use the styles.因为这些是在不同地方重用的相当复杂的样式(具有相关的 Internet Explorer 问题),我们创建了box-1box-2类的类,以便我们可以重用这些样式。

The specific example of making some text italic is too trivial to worry about though.但是,将某些文本设为斜体的具体示例太琐碎而无需担心。 Best leave minutiae like that for the Semantic Nutters to argue about.最好把这样的细节留给语义疯子们去争论。

HTML italic text displays the text in italic format. HTML 斜体文本以斜体格式显示文本。

<i>HTML italic text example</i>

The emphasis and strong elements can both be used to increase the importance of certain words or sentences.强调和强元素都可以用来增加某些单词或句子的重要性。

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

The emphasis tag should be used when you want to emphasize a point in your text and not necessarily when you want to italicize that text.当您想强调文本中的某个点时,应使用强调标记,而不必在要将该文本变为斜体时使用。

See this guide for more: HTML Text Formatting有关更多信息,请参阅本指南: HTML 文本格式

The i element is non-semantic, so for the screen readers, Googlebot, etc., it should be some kind of transparent (just like span or div elements). i元素是非语义的,因此对于屏幕阅读器、Googlebot 等,它应该是某种透明的(就像spandiv元素一样)。 But it's not a good choice for the developer, because it joins the presentation layer with the structure layer - and that's a bad practice.但这对开发人员来说不是一个好的选择,因为它将表示层与结构层结合在一起——这是一种不好的做法。

em element ( strong as well) should be always used in a semantic context, not a presentation one. em元素(也是strong )应该总是在语义上下文中使用,而不是在表示中使用。 It has to be used whenever some word or sentence is important.每当某个词或句子很重要时,就必须使用它。 Just for an example in the previous sentence, I should use em to put more emphasis on the 'should be always used' part.仅作为上一句中的示例,我应该使用em来更加强调“应该始终使用”部分。 Browsers provides some default CSS properties for these elements, but you can and you're supposed to override the default values if your design requires this to keep the correct semantic meaning of these elements.浏览器为这些元素提供了一些默认的 CSS 属性,但是如果您的设计需要覆盖这些默认值以保持这些元素的正确语义,您可以并且应该覆盖这些默认值。

<span class="italic">Italic Text</span> is the most wrong way. <span class="italic">Italic Text</span>是最错误的方式。 First of all, it's inconvenient in use.首先,使用不方便。 Secondly, it suggest that the text should be italic.其次,它建议文本应该是斜体。 And the structure layer (HTML, XML, etc.) shouldn't ever do it.结构层(HTML、XML 等)不应该这样做。 Presentation should be always kept separated from the structure.演示文稿应始终与结构分开。

<span class="footnote">Italic Text</span> seems to be the best way for a footnote. <span class="footnote">Italic Text</span>似乎是脚注的最佳方式。 It doesn't suggest any presentation and just describes the markup.它不建议任何演示文稿,只是描述标记。 You can't predict what will happen in the feature.您无法预测功能中会发生什么。 If a footnote will grow up in the feature, you might be forced to change its class name (to keep some logic in your code).如果脚注在功能中长大,您可能会被迫更改其类名(以在代码中保留一些逻辑)。

So whenever you've some important text, use em or strong to emphasis it.所以每当你有一些重要的文本时,使用emstrong来强调它。 But remember that these elements are inline elements and shouldn't be used to emphasis large blocks of text.但请记住,这些元素是行内元素,不应用于强调大块文本。

Use CSS if you care only about how something looks like and always try to avoid any extra markup.如果您只关心事物的外观,并且始终尽量避免任何额外的标记,请使用 CSS。

I think the answer is to use <em> when you intend emphasis.我认为答案是在您打算强调时使用<em>

If when reading the text to yourself, you find that you use a slightly different voice to emphasise a point, then it should use <em> because you would want a screen reader to do the same thing.如果在给自己朗读文本时,您发现使用略有不同的声音来强调某一点,那么应该使用<em>因为您希望屏幕阅读器也能做同样的事情。

If it is purely a style thing, such as your designer has decided that all your <h2> headings would look better in italic Garamond, then there is no semantic reason to include it in the HTML and you should just alter the CSS for the appropriate elements.如果它纯粹是一个样式问题,例如您的设计师已经决定您的所有<h2>标题在斜体 Garamond 中看起来会更好,那么没有语义上的理由将它包含在 HTML 中,您应该只更改 CSS 以获得适当的元素。

I can't see any reason to use <i> , unless you specifically need to support some legacy browser with no CSS.我看不出有任何理由使用<i> ,除非您特别需要支持一些没有 CSS 的旧浏览器。

OK, the first thing to note is that好的,首先要注意的是<i> has been deprecated, and shouldn't be used <i>已被弃用,不应使用<i> has not been deprecated, but I still do not recommend using it—see the comments for details. <i>还没有被弃用,但我仍然不推荐使用它——详情见评论。 This is because it goes entirely against keeping presentation in the presentation layer, which you've pointed out.这是因为它完全反对在表示层中保持表示,您已经指出了这一点。 Similarly, <span class="italic"> seems to break the mold too.同样, <span class="italic">似乎也打破了常规。

So now we have two real ways of doing things: <em> and <span class="footnote"> .所以现在我们有两种真正的做事方式: <em><span class="footnote"> Remember that em stands for emphasis .请记住, em代表强调 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.当你想强调一个词、短语或句子时,把它放在<em>标签中,不管你是否想要斜体。 If you want to change the styling in some other way, use CSS: em { font-weight: bold; font-style: normal; }如果您想以其他方式更改样式,请使用 CSS: em { font-weight: bold; font-style: normal; } em { font-weight: bold; font-style: normal; } em { font-weight: bold; font-style: normal; } . 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:当然,您也可以将类应用于<em>标签:如果您决定希望某些强调的短语以红色显示,请给它们一个类并将其添加到 CSS 中:

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

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

If you're applying italics for some other reason, go with the other method and give the section a class.如果您出于其他原因使用斜体,请使用其他方法并为该部分指定一个类。 That way, you can change its styling whenever you want without adjusting the HTML.这样,您可以随时更改其样式而无需调整 HTML。 In your example, footnotes should not be emphasised—in fact, they should be de-emphasised, as the point of a footnote is to show unimportant but interesting or useful information.在你的例子中,脚注不应该被强调——事实上,它们应该被削弱,因为脚注的重点是显示不重要但有趣或有用的信息。 In this case, you're much better off applying a class to the footnote and making it look like one in the presentation layer—the CSS.在这种情况下,最好将一个类应用到脚注并使其看起来像表示层(CSS)中的一个类。

I'd say use <em> to emphasize inline elements.我会说使用<em>来强调内联元素。 Use a class for block elements like blocks of text.对块元素(如文本块)使用类。 CSS or not, the text still has to be tagged.无论是否使用 CSS,文本仍然必须被标记。 Whether its for semantics or for visual aid, I'm assuming you'd be using it for something meaningful...无论是用于语义还是用于视觉辅助,我假设您会将它用于有意义的事情......

If you're emphasizing text for ANY reason, you could use <em> , or a class that italicizes your text.如果您出于任何原因强调文本,则可以使用<em>或将文本斜体化的类。

It's OK to break the rules sometimes!偶尔违反规则也可以!

Use <em> if you need some words/characters in italic in content without other styles.如果您需要在没有其他样式的内容中使用斜体字/字符,请使用 <em>。 It also helps make content semantic.它还有助于使内容语义化。

text-style is better suited for multiple styles and no semantic need. text-style更适合多种样式且不需要语义。

DO

  1. Give the class attribute a value indicating the nature of the data (ie class="footnote" is good)给 class 属性一个表示数据性质的值(即class="footnote"是好的)

  2. Create a CSS style sheet for the page为页面创建一个 CSS 样式表

  3. Define a CSS style that is attached to the class that you assign to the element定义附加到您分配给元素的类的 CSS 样式

    .footnote { font-style:italic; }

DO NOT不要

  1. Don't use <i> or <em> elements - they're unsupported and ties the data and presentation too close.不要使用<i><em>元素 - 它们不受支持并且将数据和演示文稿联系得太紧密。
  2. Don't give the class a value that indicates the presentation rules (ie don't use class="italic" ).不要给类一个指示表示规则的值(即不要使用class="italic" )。

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

相关问题 将文本设置为斜体的正确方法是什么? - What is the proper way to style text as italic? 在语法上是否存在用CSS / HTML中的图标替换文本的正确方法 - Is there a syntactically correct way to replace text with an icon in CSS / HTML 在 HTML 页面上使用 CSS Pencil 效果的正确方法是什么? - What is The Correct Way to Use a CSS Pencil Effect On an HTML Page? 使用 CSS 和 Javascript 向 HTML DOM 对象添加动画的正确方法是什么? - What is the correct way of adding animation to an HTML DOM object with CSS and Javascript? 使这些文本框排成一行的正确css宽度是多少? - What is the correct css width to make these text boxes line up? 使不同的 HTML 元素可访问的正确方法是什么? - What is the correct way to make the different HTML elements accessible? 在HTML中建立索引(目录)的正确方法是什么 - What's the correct way to make an Index (Table of contents) in HTML 使屏幕阅读器阅读跨度文本的正确方法是什么 - What is the correct way to make screen reader read span text 有没有办法使文本仅使用css或html填充按钮大小? - is there is way to make text fill the button size using css or html only? HTML / CSS:制作粘性菜单的正确方法是什么? - HTML/CSS: What is the right way to make sticky menus?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM