9

While using h1-h6 tags in my html, i keep getting error messages on w3c validator. I'm new to this and I've tried so many times to solve the problem but i can't.

The text appears perfectly fine on my website but it won't validate. How do i solve this problem? The error message is as follows;

Line 34, Column 4: document type does not allow element "h1" here; missing one of "object", "applet", "map", "iframe", "button", "ins", "del" start-tag

<h1><span> My website </h1>< span> <----this is the code i'm getting the error for.

The mentioned element is not allowed to appear in the context in which you've placed it; the other mentioned elements are the only ones that are both allowed there and can contain the element mentioned. This might mean that you need a containing element, or possibly that you've forgotten to close a previous element.

One possible cause for this message is that you have attempted to put a block-level element (such as "

" or "") inside an inline element (such as "", "", or "").

In any case what's the best way to use header tags? What am I doing wrong?

Community
  • 1
  • 1
Richard
  • 91
  • 1
  • 1
  • 2

6 Answers6

28
  • An span is an inline element
  • An h1 is a block element
  • An inline element cannot contain a block element
  • Elements cannot be partially contained by other elements

Therefore, from the perspective of the DTD:

<h1><span>…</span></h1> <!-- This is fine -->
<div><h1>…</h1></div>   <!-- This is fine -->
<h1><span>…</h1></span> <!-- This is wrong -->
<span><h1>…</h1></span> <!-- This is wrong -->

What the right solution to the problem actually is rather depends on what you are trying to use the span for.

(Note that the discussion of block and inline elements above is somewhat simplified. See How to read the HTML DTD for the full story, in particular the section on the Content Model)

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 1
    Er, you mean an inline element cannot contain a "block element" ? I'm pretty sure an inline can contain an inline or I've made a whole bunch of non-validating websites! – JakeParis Dec 22 '10 at 20:01
  • Whoops. Thinking one thing and typing another. – Quentin Dec 22 '10 at 20:06
9

You're closing your tags in the wrong order:

<H1><span> My website </h1></span>

should be

<h1><span>My website</span></h1>
lonesomeday
  • 233,373
  • 50
  • 316
  • 318
3
<h1 style="display:inline;">Bold text goes here</h1> <h2 style="display:inline;">normal text goes here</h2>

use the above if you looking at inline H1 Tags

takrishna
  • 4,884
  • 3
  • 18
  • 35
1

you cannot spit an element with another element

< H1>< span> My website < /h1>< /span>

should be this

< H1>< span> My website < /span>< /h1>
John Hartsock
  • 85,422
  • 23
  • 131
  • 146
1

did you try to write this?

<h1><span> My website </span></h1>

you should close the tags in the same order you open them.

0

Your elements are not nesting correctly.

Think of them like different types of brackets.

If <h1></h1> is like {} and <span></span> is like [], then you have

 { [ My website } ]

As you can see, the brackets are off.

You want

 <h1><span>My website</span></h1>
Dancrumb
  • 26,597
  • 10
  • 74
  • 130