Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

HTML

When to use div in HTML?

I don't want to use div that much. So, when to consider using divs exactly?

6 Answers

James Barnett
James Barnett
39,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...

James Barnett
James Barnett
39,199 Points

> Each time, the phrases "it's not semantically correct to use divs", "divitis" and on, often pop up.

Sounds like some really bad advice.

Check out what HTML5 doctor has to say on the matter http://html5doctor.com/you-can-still-use-div/

Chris Mitchell
Chris Mitchell
12,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 Lane
David Lane
33,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 Lay
Stevan Lay
8,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!