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

James Nelson
James Nelson
23,956 Points

Anchor element

Hello,

I have always been under the impression that the anchor element being inline, should always be surrounded by some sort of block element such as a paragraph element. Am I right in thinking so?

4 Answers

Having <a> tags which are not inside a block element will fail to validate under XHTML 1.0 Strict. It validates fine in HTML5.

James Barnett
James Barnett
39,199 Points

Learned something new today.

HTML 4 Strict & XHTML 1 Strict it fails validation, in HTML 4 Transitional, XHTML 1 Transitional & HTML 5 it validates.

James Nelson
James Nelson
23,956 Points

That would make sense as I haven't dabbled into the world of HTML5 as of yet

James Barnett
James Barnett
39,199 Points

If you are using a doctype of <!DOCTYPE html> then you are already using HTML5.

James Nelson
James Nelson
23,956 Points

I tend to use XHTML 1 Strict. Its good practice for beginners to use, drills better markup into a student I think

James Barnett
James Barnett
39,199 Points

I think you are probably right, if you faithfully validate all of your markup against that standard.

On the topic of good markup I consider this to be the gold standard

An anchor element is inline by default. However you can set it to display: block; or any other property.

James Barnett
James Barnett
39,199 Points

An <a> is usually used in the context of a block of text or a list however, it can function as a button in which case you don't have to wrap it in a <div> or any other block element.

Jeremy Germenis
Jeremy Germenis
29,854 Points

I am under the assumption that inline elements always reside in a block element. Using an anchor as a button does not change the fact that it is still an inline element and goes inside a block element. I can set a paragraph's display with css to inline but that does not change the relational meaning of a paragraph to the browser. I have only changed the visual aspect of the paragraph and it is still a block element.

James Barnett
James Barnett
39,199 Points

> I am under the assumption that inline elements always reside in a block element.

I don't think it works like that.

If you create a page that only has a <span> element on it then inspect it, you won't see any block elements in the DOM.

further reading

Jeremy Germenis
Jeremy Germenis
29,854 Points

Maybe I was not clear. Inline elements should reside in a block element, semantically speaking. You should not have a page with just a span on it.

Jeremy Germenis
Jeremy Germenis
29,854 Points

By the way.... the body element is a block element.