What to use instead of div elements in HTML5

I read in the HTML5 specification that a generic tag divshould only be used as a “last resort” when a more specific element is not available. For example, tags header, footerand sectionare preferred for thematic thematic placement of content.

But it seems that the vast majority of websites are still using divas the main element. My question is how often and in what contexts is it appropriate to use div? And what other elements should be used in their place?

I am looking for answers based on specifications, not personal opinions. Thank!

+4
source share
3 answers

There is nothing that replaces <div>(this is the reason it is still in the specification), but HTML5 has more elements available.

In addition to <header>, <footer>and <section>:

  • <nav>
  • <article>
  • <aside>
  • <main>
  • <details>
  • <summary>
  • <figure>
  • <dialog>
  • <menu>
  • and much more!

Basically, any new HTML5 element can replace <div>.

When should you use a div? You yourself answered:

when a more specific item is unavailable

MDN has an HTML5 element list that contains all the standard HTML5 elements and indicates which elements are new with HTML5.

+4
source

, div HTML5 . , div HTML4, , . , HTML5 , div .

div , , - . , HTML5, , .

section, , , , header, footer .., , , . , , , section div.

div HTML5. , . section , section .

+2

header, section, footer , css . W3C , - div HTML5. div , . , , .

? . header, . -. . div 'header'. , .

Of great importance for divis the creation of a wrapper around all your content, like this.

<div id="wrapper">
    <!--content-->
</div>

Then you can reference it in css to the center:

#wrapper{margin:20px auto;}

Hope this helps!

+1
source

Source: https://habr.com/ru/post/1528781/


All Articles