views:

53

answers:

5

How do you guys handle marginal CSS? By marginal, I mean a single word or phrase that needs italics or bolding. It seems silly to declare a 'bold' class with just

Bold { font-weight: bold; }

Or italics, either!

Italic { font-style: italics; }

But I find myself hesitating to put class like that into my css reset.

<p>
    <span class="Italic">This</span> man? 
    <span class="Bold">What</span> are you thinking?
</p>

Obviously if you're going to combine a bunch of properties to make something look different, it makes sense...

.HoverOnMe
{
    color: #880;
    text-decoration: underline;
    font-style: italic;
}

But I'd classify the above css style as 'non-marginal'.

We're taught that elements like <b> and <i> are bad because they mix structure and style, and therefore we shouldn't use them. So what is the right way to handle 'marginal css'?

+4  A: 

Instead of <b> and <i> use <strong> and <em>. These have semantic meaning and are often default-styled by browsers to be the same as <b> and <i>, and of course you can control their style like any other element.

Jake
+1, please give semantic to your html.
Clement Herreman
Is there a semantic version of other single-shot stylings like underline?
C Bauer
Off the top of my head I can't think of any notable ones. Things like underline aren't really semantic in the first place. The best way to achieve those would probably be classes - although you can still be semantic by thinking about why you want that one-shot style (I see no reason why you couldn't style a `<em>` to be non-italic and underlined, though, for example - if underlind is for emphasis)
Jake
A: 

I certainly would avoid creating Bold and Italic classes in your CSS stylesheet.

When you say 'Marginal', if you mean that it is like a one-off case and you dont feel you should create a class just for this one case, then what about just using inline CSS?

e.g...

<p>
    <span style="font-style:italic">This</span> man? 
    <span style="font-weight:bold">What</span> are you thinking?
</p>
barrylloyd
Inline CSS is evil : code gets ugly, changing it could be kind of difficult and if you're working with designers not supposed to change code, you will have muuuch more to do.
Kaaviar
@Kaaviar: I totally agree, was just suggesting it as an answer to the question which seems to be about avoiding creating a style 'class' just for a single-word styling case. I did think of suggesting <STRONG>, <EM> and that answer seems to be most popular - but I'm not sure that is really any better in terms of separating structure and style.
barrylloyd
Inline CSS like this is no less evil than the examples listed in the question. It's the same thing really. Except that this version is more straightforward.
jessegavin
For the record I am in the <strong> and <em> camp.
jessegavin
See bobince accepted answer (Ive voted it up) . Use semantic tags such as EM, STRONG when the text should be emphasized but if you are styling something in italics/bold for a different reason then inline CSS is as good a way as any. Creating a Class only makes sense if it will be re-used elsewhere and you would want all the instances to change if you changed the class.
barrylloyd
In my opinion, if you choose to decorelate styles from code you have to do it for everything.
Kaaviar
A: 

I think you did it write using . You could use too to render default bold but it would have another meaning since it precise the importance of the text within it.

There's no better way for a one-attribute css rule.

Kaaviar
A: 

Why not create a css class that describes the intent rather than the action of emphasizing. You could call it something like "importantWord", and yes, it's fine that it only contains a single rule. The important thing is that if you decide to change the appearance of the style, semantically, it will still make sense.

spender
No need of a class, there is already a bunch of HTML tag, (em, strong, etc..)
Clement Herreman
Perhaps use of predefined tags has already been exhausted, so this remains a viable option.
spender
+5  A: 

When you add a class to an element, name that class by what it represents, not what it looks like. class="Italic" is an anti-pattern that completely misses the point of separating content and styling.

<span class="Italic">This</span> man? 
<span class="Bold">What</span> are you thinking?

If what you mean to say is that the word “This” is an emphasised word—that is, if you were to read the sentence, you'd change your tone of voice when pronouncing it—then you should say so with a class name like class="emphasised". However you don't need to do that, because there is already an element available in HTML that has exactly that meaning, specifically <em>.

<em>This</em> man?

As luck would have it, browsers will render <em> as italic by default, so you wouldn't need any more CSS.

You shouldn't always use <em> for italics. There are other reasons a word might be italicised. For example it might be a citation (use <cite>), or a phrase in another language (use <span lang="fr">c'est la vie</span>), or it might just be a typographical quirk with no semantic meaning (in which case a plain <span> with styling is fine). Use the element that most closely matches the semantics of what you are trying to say, and adjust the rendering with CSS if the default rendering doesn't match what you wanted it to look like.

There is a second form of emphasis that is rendered as bold by default, <strong>:

<strong>What</strong> are you thinking?

This is usually considered to mean “more emphasised than <em>”. If that is what you were going for, use that tag. But again, don't jump for <strong> just because you want something bold. If it should be bold because it's a heading, use the heading tags. If it should be bold because it's the first line of an article or something, add a class="first-line" (or simply use a CSS :first-line selector, where appropriate).

bobince
This answer is very clear and concise, and explains why em and strong should be used. I understand this a lot better now. Thanks!
C Bauer