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

Andrew Zhao
Andrew Zhao
3,267 Points

Is it true that block level elements should not be nested within inline elements?

For example:

<!-- INCORRECT: -->
<span><h1>Hello</h1></span>

<!-- CORRECT: -->
<h1><span>Hello</span></h1>

2 Answers

You shouldn't put block elements within inline elements because it breaks the element structure. Inline elements are meant to reside within block level elements so that they render correctly and follow the flow of content. And since block elements break apart content, putting a span inside of a paragraph of text is perfectly fine, but putting a span outside of the paragraph is not a best practice because the span doesn't flow with any content.

Another reason that you should put inline elements inside of block elements (instead of the other way around) is so that the browser can render the elements in an easily predictable way. If we reverse the structure, we've created elements that aren't rendering as intended, even though it might not break any HTML or CSS rules to do so.

You can read more about this at the W3's site under CSS2.1 specs: Anonymous Block Level

Andrew Zhao
Andrew Zhao
3,267 Points

Marcus, thank you for your response.

So I shouldn't put block elements within inline elements, but inline elements within block elements are fine?

Your first sentence has me confused with the rest of your answer.

Apologies, I mixed up inline and block in that first sentence. I edited it so that it reflects the proper order.

But yes, inline elements within block elements are great, but block elements within inline elements shouldn't be used.