开发者

Can we have content text directly in <div>?

can we have content text directly in <div>text conte开发者_开发知识库nt</div> or it should be like <div><p>text content</p></div>? according to web standards.


You can have character data directly inside a div element. If (and only if) that character data is a paragraph, then it should also be marked up as a paragraph (as per your second example). If you only have one paragraph of content inside the div, then you should usually avoid having the div element as it serves no purpose.


You can have it directly in a div. The div is often used to group block-elements to format them with styles, but normal, unmarkedup text in a div is just fine.


There's a two questions implied here, can and should. can is straight-forward, should is not.

So, Can you nest content text directly into a div?

The current active spec states The div element has no special meaning **at all**. So, yes there is no reason why you cannot nest text directly into a div, but that text has no special meaning.

Should you nest content text directly into a div?

No, not really. There is one fundamental problem with putting text into an element that has no special meaning. What does that text mean? Is it a title or a simple block of text, etc. For most people this isn't an issue. If you're using a screen reader it is. The screen reader needs more information to understand the context of the text. This is why the HTML spec on div now includes the following text:

Note Authors are strongly encouraged to view the div element as an element of last resort, for when no other element is suitable. Use of more appropriate elements instead of the div element leads to better accessibility for readers and easier maintainability for authors.

so ideally all your text should be nested into a HTML element that represents it's context, e.g. <p>, <h1>, etc.


Yes, you can directly add content text into a div tag, although using p tags would be preferable in most circumstances.


Screen readers seem to handle <div>phrasing content</div> and <div><p>phrasing content</p></div>

differently in some circumstances.

It seems that the rules for elements that can contain flow content and elements that can contain phrasing content are different.


The standards are not so well defined. I would choose your second example, because it is more structurally sound, and therefore more semantic.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜