ChatGPT解决这个技术问题 Extra ChatGPT

What is the difference between <section> and <div>?

What is the difference between <section> and <div> in HTML?
Aren't we defining sections in both cases?


A
Andy

<section> means that the content inside is grouped (i.e. relates to a single theme), and should appear as an entry in an outline of the page.

<div>, on the other hand, does not convey any meaning, aside from any found in its class, lang and title attributes.

So no: using a <div> does not define a section in HTML.

From the spec:

The

element represents a generic section of a document or application. A section, in this context, is a thematic grouping of content. Each section should be identified, typically by including a heading (h1-h6 element) as a child of the
element. Examples of sections would be chapters, the various tabbed pages in a tabbed dialog box, or the numbered sections of a thesis. A Web site’s home page could be split into sections for an introduction, news items, and contact information. ... The
element is not a generic container element. When an element is needed only for styling purposes or as a convenience for scripting, authors are encouraged to use the
element instead. A general rule is that the
element is appropriate only if the element’s contents would be listed explicitly in the document’s outline.

(https://www.w3.org/TR/html/sections.html#the-section-element)

The

element has no special meaning at all. It represents its children. It can be used with the class, lang, and title attributes to mark up semantics common to a group of consecutive elements. Note: Authors are strongly encouraged to view the
element as an element of last resort, for when no other element is suitable. Use of more appropriate elements instead of the
element leads to better accessibility for readers and easier maintainability for authors.

(https://www.w3.org/TR/html/grouping-content.html#the-div-element)

Sections are most relevant in landmark navigation for assistive technology. To appear in the document outline or landmark list, they need a name, which can be assigned by means of aria-label, aria-labelledby or title:

<section aria-labelledby="s3-h2">
  <h2 id="s3-h2">Introduction</h2>
  …

For example VoiceOver on Mac then can provide an outline to navigate directly to that section.


Thinking more about section vs. div, including in light of this answer, I've come to the conclusion that they are exactly the same element. The W3C says a div "represents its children". Well, isn't that also what the section element does? Yes, section implies its children are grouped together, but by the very act of putting children inside a div, you are also, yes, grouping them together. At least the way I do it, I don't know about you guys.
@trysis: “Thinking more about section vs. div” — don’t think too much about it. HTML isn’t complicated. “by the very act of putting children inside a div, you are also, yes, grouping them together.” Not according to the HTML spec you’re not. You’re wrapping them in a div for styling purposes, or JavaScript convenience, or something else that the W3C hasn’t thought of yet, but doesn’t indicate to readers that the child elements are a group.
@Matian2040: because the purpose of HTML is to add meaning to text, for example <p>This is a paragraph</p> or <h2>This is a second-level heading</h2>. Because <div> adds no meaning, you’d only use it if there isn’t another HTML element that adds appropriate meaning to the text in question.
So there is not a single advantage if using sections? lol, why does it even exist then?!
@EdwardBlack: it conveys a different meaning than other tags do. Conveying meaning is the entire point of HTML.
Q
Quentin

<section> marks up a section, <div> marks up a generic block with no associated semantics.


@MarwenTrabelsi — The link isn't dead. "Section" is a standard English word. Dictionaries are available.
@MarwenTrabelsi — The terms you call "abstract and wide" are the key differences.
This seems like a more "in your face" kinda answer. And I mean it in a good way. Like no nonsense to the point. Thanks.
D
David Storfer

Just an observation - haven't found any documentation corroborating this

If a section contains another section, a h1-header in the inner section is displayed in a smaller font than a h1- header in outer section. When using div instead of section the inner div h1-header is diplayed as h1.

<section>
  <h1>Level1</h1>
  some text
  <section>
    <h1>Level2</h1>
    some more text
  </section>
</section>

-- the Level2 - header is displayed in a smaller font than the Level1 - header.

When using css to color h1 header, the inner h1 were also colored (behaves as regular h1). It's the same behaviour in Firefox 18, IE 10 and Chrome 28.


How very weird... created a quick stackblitz to demo this as it's still a thing stackblitz.com/edit/angular-h1ayez
Exactly like sections in Latex language, it's called subsection there.
The MDN docs make mention of avoiding multiple h1's in a single page, even though it's technically supported. I imagine that's the bug (?) that results in the strange styling of h1's nested in sections. In the demo posted by @GavinMannion (thanks), you will note that all the h2's are styled the same, no matter the level of nesting within sections. TLDR, h1's are treated differently, and sections should only start with h2's rather than h1.
@GavinMannion: Nice example. Interestingly, it does not have any affect on <h2> elements... i.e all <h2> are rendered in the same font-size. Wow! Amazing!
S
Subodh Ghulaxe

Vs

Round 1

<div>: The HTML element (or HTML Document Division Element) is the generic container for flow content, which does not inherently represent anything. It can be used to group elements for styling purposes (using the class or id attributes), or because they share attribute values, such as lang. It should be used only when no other semantic element (such as <article> or <nav>) is appropriate.

<section>: The HTML Section element (<section>) represents a generic section of a document, i.e., a thematic grouping of content, typically with a heading.

Round 2

https://i.stack.imgur.com/zSFmL.png

<section>: Browser Support

https://i.stack.imgur.com/jYJNk.png

In that vein, a div is relevant only from a pure CSS or DOM perspective, whereas a section is relevant also for semantics and, in a near future, for indexing by search engines.


Browser support is a non-issue here, it's about semantics. If you're using HTML5, you'll probably use a polyfill anyway.
@JackTuck And what if you don't want to use such kludges?
c
cf stands with Monica

In the HTML5 standard, the <section> element is defined as a block of related elements.

The <div> element is defined as a block of children elements.


I dont know why someone marked this down. Short, sweet, and too the point.
-1 it's don't make any sense, how you want to group related elements in the hierarchical structure document (XML/HTML), you can only group block of children elements using any tag.
@Svisstack You are correct that any enclosing tag (ie. not self-closing/void) can group blocks of child elements. Though I think this is getting more into what is the relation of the children. Do they all have a related context to convey? For example: a form containing several inputs would be grouped together for functionality/styling reasons. But this form is not a section of the website, but instead a piece with a function. Now lets say your page was describing a product. Different parts about the product would be listed in a section element because the elements convey a collective idea.
K
KoolWebDezign

Take caution not to overuse the section tag as a replacement for a div element. A section tag should define a significant region within the context of the body. Semantically, HTML5 encourages us to define our document as follows:

This strategy allows web robots and automated screen readers to better understand the flow of your content. This markup clearly defines where your major page content is contained. Of course, headers and footers are often common across hundreds if not thousands of pages within a website. The section tag should be limited to explain where the unique content is contained. Within the section tag, we should then continue to markup and control the content with HTML tags which are lower in the hierarchy, like h1, div, span, etc.

In most simple pages, there should only be a single section tag, not multiple ones. Please also consider also that there are other interesting HTML5 tags which are similar to section. Consider using article, summary, aside and others within your document flow. As you can see, these tags further enhance our ability to define the major regions of the HTML document.


"In most simple pages, there should only be a single section tag". Can you give a non-simple example where you would want to use multiple section tags in a single page?
I would use the main tag in there, and inside it, one or more section tags.
If you follow the spec, your section should be main, the div should be article and the span would likely be section (depending on what it's doing)
P
Paul D. Waite

<div>—the generic flow container we all know and love. It’s a block-level element with no additional semantic meaning (W3C:Markup, WhatWG)

<section>—a generic document or application section. A normally has a heading (title) and maybe a footer too. It’s a chunk of related content, like a subsection of a long article, a major part of the page (eg the news section on the homepage), or a page in a webapp’s tabbed interface. (W3C:Markup, WhatWG)

My suggestion: div: used lower version( i think 4.01 to still) html element(lot of designers handled that). section: recently comming (html5) html element.


d
drooh

Using <section> may be neater, help screen readers and SEO while <div> is smaller in bytes and quicker to type

Overall very little difference.

Also, would not recommend putting <section> in a <section>, instead place a <div> inside a <section>


P
Poomalairaj

The section tag provides a more semantic syntax for html. div is a generic tag for a section. When you use section tag for appropriate content, it can be used for search engine optimization also. section tag also makes it easy for html parsing. for more info, refer. http://blog.whatwg.org/is-not-just-a-semantic


“section tag also makes it easy for html parsing” — eh? Do you mean for generating an outline of the page?
A
Ashwin Krishnamurthy

<section></section>

The HTML

element represents a generic section of a document, i.e., a thematic grouping of content, typically with a heading. Each
should be identified, typically by including a heading (

-

element) as a child of the
element. For Details Please following link.

References :

http://www.w3schools.com/tags/tag_section.asp

https://developer.mozilla.org/en/docs/Web/HTML/Element/section

<div></div>

The HTML

element (or HTML Document Division Element) is the generic container for flow content, which does not inherently represent anything. It can be used to group elements for styling purposes (using the class or id attributes), or because they share attribute values, such as lang. It should be used only when no other semantic element (such as
s
steveax

The <section> tag defines sections in a document, such as chapters, headers, footers, or any other sections of the document.

whereas:

The <div> tag defines a division or a section in an HTML document.

The <div> tag is used to group block-elements to format them with CSS.


Headers, footers and other sections of the document have their own semantic tags. (<header>, <footer>, <nav>, <article> etc.)
K
Kaan E.

Here is a tip on how I distinguish couple of recent html5 elements in the case of a web application (purely subjective).

<section> marks a widget in a graphical user interface, whereas <div> is the container of the components of a widget like a container holding a button, and a label etc.

<article> groups widgets that share a purpose.

<header> is title and menubar.

<footer> is the statusbar.


This is so wrong I'm not even sure where to start. You are assigning visual meaning to elements that have 0 visual meaning and are 100% semantic. If you replaced
and
in your comment with
then you would be correct.