Welcome to the Treehouse Community
Looking to learn something new?
When to use div in HTML?
I don't want to use div that much. So, when to consider using divs exactly?
James Barnett39,199 Points
Knowing when to use divs is something that you learn. In general you use a div whereever you need one to group elements together for the purposes of layout. It's best practice to give all of your divs a semantic class name so they have some semantic meaning.
Yeh, good question. I have heard some differing viewpoints on this particular subject. Each time, the phrases "it's not semantically correct to use divs", "divitis" and on, often pop up. In my opinion, use divs as and when you need to but don't go too over the top. I have discovered with HTML 5 more semantic elements are available such as header, nav, section which tend to help when you structure your site but that's for another discussion...
Chris Mitchell12,719 Points
You are starting off really well Suli by recognising that you dont want to use too many DIVs so well done there.
Always try to keep in mind the word "Semantic", semantic is good in html because it is explaining the web page content more accurately than what a generic tag DIV will do and will help browsers understand your page.
Check out new tag elements you can use in html5
I would say that DIVs are ok because they are neither good or bad in semantic terms, they are neutral.
Could a good example of a DIV be where for example you have an ARTICLE element and you want to wrap a container within the element??? not sure if that makes sense but hope it helps
David Lane33,414 Points
The short unhelpful answer is whenever its necessary. Div tags are a great way to create "blocks" of content for placement on a page, along with many more uses. Why would you not want to use them?
Yep, good article James!
Stevan Lay8,306 Points
Apologies for bringing this old thread/question back up but I'm still trying to figure out a better way of thinking about the usage of <div> in html. How is it best approached when to include the <div> tag and when is it best to not include the <div> tag in html? It sometimes confuses me when/where a <div> begins and/or ends. I've read the article but wanting more of people's input or ideas of best practice and thoughts on this subject. Cheers!