Much of this response is based on good practice, ux, accessibility and my own opinion.
What is the <div>
The div 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.
Translation: "The element div
has no special meaning. It represents your children. It can be used with attributes class
, lang
and title
to mark the semantics common to a group of consecutive elements."
Source of official documentation W3C https://www.w3.org/TR/2011/WD-html5-author-20110809/the-div-element.html
div = divisor serves to divide the layout into blocks. And logically you can receive CSS styles and receive behaviors from the most diverse.
But why do they use so much <div>
?
I believe we see so many div in HTML because they are often used in the wrong way, for example to build tables, menus, and even forms... I won’t generalize, but if you see in HTML an element that by defaul is block type and is with the display
setado with display:inline
it is probably being used incorrectly structurally speaking.
I believe that in the years to come you will see less and less div
, Do you know why? Because HTML 5 is now semantic. Now we can divide our layout by <sections>
, <articles>
, <footers>
, <headers>
, <nav>
etc. With the display:flex
we no longer need to use display:inline-block
nor float:left
in div
to line up next to each other and still have to do the "gambiarra" of clearfix not to let the layout break.
Semantic layout without needing to div
Another reason to see so many div
is because of those frameworks
that deliver ready-made components and you put one component inside the other and with it creates a lot of divs
unnecessary... Any site in wordpress for example usually have an immense number of divs
for being mostly a template full of components and plugins.
Example div in Bootstrap component construction:
In short, <table>
is to make table, <form>
is to form if you want a div
next to each other don’t use float
, float
not for that. And use the semantic tags of HTML5 <nav>
for menus, <footers>
for footer etc. and if you need to use a div or other within these elements to split the elements into blocks if needed.
Read only the first paragraph: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/div
– Sam
The question sounds a little strange because when asking what the
div
s and say "I don’t understand what exactly it’s for or why they put div inside another div" As far as I can see, it conflicts a little with what you say here "Div is one of the tags I see most in HTML and that is used from start to finish in body"... while it seems to have no knowledge of HTML, it seems that it does, mentioning "tags", "body" with property... : D... even wrote "HTML" in upper case...– Sam