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
Joseph Torres
6,282 PointsDiv tags and nested div tags!
Good morning, I keep seeing div tags nested within div tags for either text or images and such. Is this typical?
1 Answer
Steven Parker
243,318 PointsIt could be typical, depending on how the code was created.
Without seeing the specific code, I couldn't guess if the tags are functional or just excessive. But depending on how the code was created, it might be typical either way.
Since the div tag can be a general-purpose container element, many automated coding helper tools use them "just in case". Or they could be left-overs from previous page edits. Or, if they have id's and/or classes, they might be performing a useful function in combination with some CSS or JavaScript code.
Joseph Torres
6,282 PointsJoseph Torres
6,282 PointsThank you so much. So for example if I create a div tag named container and a nested div which houses an image for the back ground, would I make the container absolute and the nested div relative or vice versa? This is where Im so stuck...
Steven Parker
243,318 PointsSteven Parker
243,318 PointsIt depends on what you are trying to achieve. Backgrounds have a large complement of properties you can set directly, so it could be you can get what you want without using element positioning. In that case you could likely eliminate at least one of the div's as well.
If you truly need positioning for some other reason, that might be a legitimate use for nested div's. I'd need to see exactly what you are doing to be sure, but if the outer (first) div is for reference, you would set its position to "relative". Then you could locate the absolute div within it.