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

后端 未结 12 1878
死守一世寂寞
死守一世寂寞 2020-12-02 06:46

What is the correct way to make text italic? I have seen the following four approaches:

Italic Text

I         


        
相关标签:
12条回答
  • 2020-12-02 07:06

    <i> is not wrong because it is non-semantic. It's wrong (usually) because it's presentational. Separation of concern means that presentional information should be conveyed with 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. 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. 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>. Otherwise not.

    0 讨论(0)
  • 2020-12-02 07:07

    OK, the first thing to note is that <i> has been deprecated, and shouldn't be used <i> has not been deprecated, but I still do not recommend using it—see the comments for details. 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.

    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; }
    

    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. 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.

    0 讨论(0)
  • 2020-12-02 07:13

    I'd say use <em> to emphasize inline elements. Use a class for block elements like blocks of text. CSS or not, the text still has to be tagged. 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.

    It's OK to break the rules sometimes!

    0 讨论(0)
  • 2020-12-02 07:21

    I'm no expert but I'd say that if you really want to be semantic, you should use vocabularies (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).

    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/

    0 讨论(0)
  • 2020-12-02 07:21

    DO

    1. Give the class attribute a value indicating the nature of the data (i.e. class="footnote" is good)

    2. Create a CSS style sheet for the page

    3. Define a CSS style that is attached to the class that you assign to the element

      .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.
    2. Don't give the class a value that indicates the presentation rules (i.e. don't use class="italic").
    0 讨论(0)
  • 2020-12-02 07:25

    I think the answer is to use <em> when you intend emphasis.

    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.

    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.

    I can't see any reason to use <i>, unless you specifically need to support some legacy browser with no CSS.

    0 讨论(0)
提交回复
热议问题