Posted about 4 years ago. Visible to the public. Repeats.

It's OK to put block elements inside an <a> tag

In general, you should not put a block element inside an inline element. So don't do this:

<span> <div>text</div> </span>

The browser will think you wrote invalid HTML by accident, and will sometimes reorder elements silently.

There is one notable exception: It's OK to wrap block elements in a <a> tag in HTML5 (not 4). The spec says:

The a element may be wrapped around entire paragraphs, lists, tables, and so forth, even entire sections, so long as there is no interactive content within (e.g. buttons or other links).

So feel free to use a single <a> link to make a series of block elements clickable:

<a href="/foo"> <h1>title</h1> <p>text</p> </a>

History of this exception

The XHTML 2 spec allowed most elements could have an href attribute. When XHTML 2 was abandoned in 2009 in favor of HTML 5, people wanted to bring over this feature to HTML 5, at least in some form. In the spirit of HTML 5, a pragmatic solution was chosen.

makandra has been working exclusively with Ruby on Rails since 2007. Our laser focus on a single technology has made us a leader in this space.

Owner of this card:

Henning Koch
Last edit:
over 1 year ago
by Arne Hartherz
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Henning Koch to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more