r/learnwebdev • • Jan 13 '20

Question about "Div-itis"

So, I am learning HTML, CSS, and JavaScript first. I went through the HTML and CSS portions, and am currently on JS. However, I was curious about this "div-itis" I keep hearing about. I get what it is...and I get how to avoid it (kinda) What I am curious about it when to use it properly. In what situations would it be a good idea to use the <div>?

2 Upvotes

4 comments sorted by

View all comments

Show parent comment

1

u/UnderPressureICrackn Jan 13 '20

Thank you! I get that divs should be used as wrappers and containers for layout purposes, but is that not what CSS is used for? Like adding classes or ids? Do all webdevs use CSS? Or not all do?

1

u/SushiNChill Jan 13 '20

These suggestions are mainly to help with readability and maintenance. Imagine an html file with all div tags and each of them has a class name that can be described by the proper html tag. I.e. <div class=header> can be replaced with <h2>, or something of that sort

To my knowledge, all webdevs should know html and css at the minimum with most jobs requiring JS now.

1

u/[deleted] Jan 15 '20 edited Jan 15 '20

I get that divs should be used as wrappers and containers for layout purposes, but is that not what CSS is used for?

I'm not really following this. Using divs and CSS are not mutually exclusive. CSS is for styling, but you need markup to style in the first place.

When building complex layouts and components, it's often necessary to group or break up portions of content with a generic container element to achieve a desired result. When that needs to be done to flow content in a way that isn't better described with a semantic element (<header>, <article>, <section>, etc.) then a div is the way to go, because it is the generic container element and comes with no inherent styling (aside from display: block).

Divs are a ubiquitous part of HTML and it would be counterproductive to try and avoid using them altogether. Div-itis comes from 1) Using too many nested divs to create a result that could be achieved with fewer elements and more effective CSS, and 2) using divs in place of more appropriate elements. For example, don't create what is effectively a list with divs; use <ol> or <ul> instead.