Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

HTML

Laura Cressman
Laura Cressman
12,548 Points

Div Tag General Question

I'm trying to understand div tags. When should I use them and when should I not use them? Is it easier to just put all the HTML elements inside div tags? Why or why not? :)

5 Answers

Ricardo Diaz
Ricardo Diaz
30,415 Points

div tags if I just describe them in "simple talk" is just a container. Generally they are the default tags you use but there are more options becoming available like the nav, section, video tags and the standard ones we use. I think the best rule is to use them when no other option is available. But this really relies on your knowledge of HTML 5 and what is currently cross browser compatible.

My advice is to just keep reading as much as you can on the topic and for now just use div tags till you can understand how to use other ones correctly. The more you learn the better able you are to use other more appropriate tags.

div tags are containers, it is not about you should use or not, it is about whenever you need to use specific class or id and you want that class or id to affect specific elements you put those elements into div tags. As a result I would say div tags are just containers.

James Barnett
James Barnett
39,199 Points

The short answer is that a div is used to group elements together for styling/layout purposes. As to when to use a div instead of another element, only use a div when there's not a more semantic choice.

You might find some reading on Semantic HTML helpful:

Mohammed Hossen
Mohammed Hossen
5,591 Points

In HTML4 the <div> tag is used to separate sections. The <div> tag defines a division or a section in an HTML document. The <div> tag is used to group block-elements to format them with CSS. For example if you have a navigation at the top you can nets it in the div element like <div id="navigation><ul><li>1</li></ul></div> and for footer <div id="footer"></div>. Something like that. However, with HTML5 you have the new element for the navigation <nav> and for footer <footer>.

Laura Cressman
Laura Cressman
12,548 Points

Wow, thank you everybody for the responses! Hope you all had a great weekend :)