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

If an anchor is supposed to nest inside of an element such as h1 why am i having trouble styling with css?

For example:

When styling with CSS, this doesn't work.

<h1><a href=“” target=“”>HI MY NAME IS HANNAH</a></h1>

But when the anchor is outside of h1, it does work! When i style a with CSS.

<a href="" target=""><h1>HI MY NAME IS HANNAH</h1></a>

Hannah, I have two questions.

  • what styles are you trying to apply
  • how are you targeting the example that doesn't work

Austin already gave an answer so if your issue is already resolved feel free to disregard this post. (although I am curious)

1 Answer

Inline elements (such as anchor tags) will take on a number of styles of their parent element's text styles. In this case, your block level h1 element is that parent. This helps keep styles applied to text elements (generally inline) consistent across siblings.

Prior to HTML5, you were not supposed to put a block-level element (your h1) inside of an inline element (your a) and it would not be considered valid code. It is technically allowed now, though you'll still see more people use the <h1><a>...</a></h1> structure than not.

Luckily, not matter how you structure your HTML, you can modify them with CSS targeting to make sure everything looks as you'd like it.