h1 and the span

倾然丶 夕夏残阳落幕 提交于 2020-01-01 09:05:15

问题


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?


回答1:


  • 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)




回答2:


You're closing your tags in the wrong order:

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

should be

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



回答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




回答4:


you cannot spit an element with another element

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

should be this

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



回答5:


did you try to write this?

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

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




回答6:


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>


来源:https://stackoverflow.com/questions/4513136/h1-and-the-span

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!