tags:

views:

658

answers:

4

I don't understand how to do this!

+18  A: 

The better way to emphasize text nowadays is using the <em> tag:

<em>My Text</em>

But if you just need to show something italicized that is not semantically an "emphasis", it is perfectly fine to use the <i> tag, which is ancient but still alive:

<i>My Text</i>

Or if you have a particular element.class combination you want to show italicized:

<span class='author'>Paolo Bergantino</span>

With the style:

span.author { font-style: italic; }

More: HTML - Italic(s)

Paolo Bergantino
Technically only <i> actually ensures italic, although that's "presentation markup" and mildly deprecated. <em> just means emphasis, and while most browsers' default stylesheets show that as italic, this is by no means enforced. (Just so my comment isn't taken out of context, I'm vastly in favour of <em> instead of <i>.)
Chris Jester-Young
I agree with Chris. Really, we need to know /why/ the OP wants italics. Do they just want the font style? If so, <em> is no better than <i>, and <span style="..."> is probably the best. If they want emphasis, then <em> is correct. If they want formatting for a published work, maybe <cite> would work best. Or it's a quotation, and they should use <q>. Ok, I'm out of examples.
Tom
the <i> tag is depreciated because it's not semantic. It's best to use <em>.
Matthew James Taylor
The <i> tag is not deprecated (check the HTML spec). It's perfectly valid to use in situations where italics has a meaning that isn't emphasis. For example: Several <i>de facto</i> English-speaking countries have no <i>de jure</i> official national language.
Rene Saarsoo
As another option to be added to Chris', you could use the <em> tag for emphasis, and then add em { font-style: italic; } in your stylesheet to ensure that it renders italic - that is, if you want *both* the emphasis *and* the italic style this is the way to go.
Tomas Lycken
“Technically only <i> actually ensures italic” — as long as the user agent supports it. Technically, I don’t think they have to.
Paul D. Waite
+1  A: 

Both <em> and <i> work, but <em> is considered the "correct" way, as the <i> tag just means "italic", but the <em> tag means "emphasis" - which refers to the meaning of the content, not just how to display it. So screen reading software, for example, knows how to pronounce "emphasis" but not "italics".

So, do something like this:

<p>This is normal text. <em>This is in italics.</em> This is back to normal. </p>

(Of course, you can still use <i> if you really just mean italic.)

(Or, you can really go crazy and add "font-style:italic" to the css class of the particular piece of text in question)

Electrons_Ahoy
Where does this "<i> is deprecated" misconception come from? I guess nobody reads specs any more: http://www.w3.org/TR/html4/present/graphics.html#edef-I
Rene Saarsoo
Well I'll be damned - you know what? You're absolutely right. Lots of people *say* it is, but the spec says it isn't. Answer corrected. Thanks, Rene.
Electrons_Ahoy
+9  A: 

There are a couple of ways to do this, depending on why you're doing it:

For emphasis When emphasizing something, you should use the <em> tag as this will be correctly interpreted as emphasis by screen readers etc.

Fore style purposes only If you only want italic text for style purposes, for example on a whole paragraph or the entire page, you're not really looking for emphasis so the <em> tag is wrong. Instead, you could do one of two:

  • Style with css (better): add a font-style: italic; to the css class of the element you want to style
  • Style with html (not so good, but it works and it's easy): encapsulate the italic text in an <i> tag.
Tomas Lycken
YES! +1 for not slavishly replacing all italics with <em>!
Tom
A: 
another <span style="font-style: italic;">simple</span> answer
Matthew
Avoid inline style. Avoid span when a better element exists (if one does depends on context).
David Dorward
Opinion, opinion... it's amazing how many features languages make available to make our lives easier that by some are considered taboo, dirty, and wrong. Who needs to spend time defining a style sheet when all you want is a simple text effect? I define style sheets when inline style starts to become tedious and repetitive. Language should be a tool, not a burden.
Matthew