tags:

views:

482

answers:

4

I want to know which of the following code is correct... among all the three...

<h1><span class="bold">realestate</h1>
<h1>realestate</h1>
<h1 class=bold>realestate</h1>

Please let me know about that...

+2  A: 

The first is not correct, since the span element has no end-tag:

<h1><span class="bold">...</span></h1>

The second and third are both correct (but the doublequotes are missing in the third example).


But if you want to use which one to use, I'd say use the second one and define the bold style for the h1 element, e.g:

<style>
h1 { font-weight: bold; }
</style>
M4N
The doublequotes are required in the third example in XHTML, but not in HTML.
Steve Jessop
For a good CSS tutorial, see this site: http://w3schools.com/css/
M4N
+16  A: 

The best would be option 4:

<h1 class="main-heading">realestate</h1>

You should give your classes semantic meaning - a main heading will always be a main heading, but what if the designer decides they would look better underlined? You'd end up with this:

.bold { font-weight: normal; text-decoration: underline; }

Which is confusing at best!

Greg
+1 100% agree. semantic meaning in class names FTW!
Jonathan Fingland
just to add a slightly more serious note to my last comment, class names should define what something *is* or what kind of thing it is -- not how it should look. Seeing things like class="bold blue size14" tell you how it looks but make it far more difficult/unnatural to later change those styles. Class names with semantic meanining will almost always be easier to maintain and easier to develop for different devices, etc
Jonathan Fingland
And if you ever do find yourself calling a class "bold", then it's probably because, for whatever reason, you're not writing your HTML with an eye to restyling it later. As long as that's OK, just say what you mean: use <b></b>, or style="font-weight: bold" right in the HTML.
Steve Jessop
A: 

The third option certainly seems to me to be the cleanest option, except the attribute value should be enclosed in quotes, assuming you are using XHTML:

<h1 class="bold">realestate</h1>

It doesn't really make sense to enclose the entire contents of the <h1> tag in another tag. It would make more sense to enclose just the part of the tag that should be different to the rest of the <h1>, but not completely different, e.g:

<h1>Real <span class="bold">Estate</span></h1>

Think of the <span> tag as the inline equivalent of the <div> tag.

Wayne Koorts
A: 

It depends on what you are trying to do.

Are you trying to make all your h1's bold. In that case, use #2 and use CSS to style all h1 elements accordingly.

CSS

h1 {
  font-weight: bold;
}

HTML

<h1>realestate</h1>

If you are trying to make only a select few h1's bold, use #3 and use CSS to style all h1 elements that have the bold class.

CSS

h1.bold {
  font-weight: bold;
}

HTML

<h1 class="bold">realestate</h1>

The first one is really only useful if for some reason you wanted part of h1 to be bold

CSS

.bold {
  font-weight: bold;
}

HTML

<h1><span class="bold">real</span>estate</h1>

Also, in general from your samples, watch out for quotes and end tags.

tschaible