Consider the following HTML:
<ul>
<li>
<h2>Item 1</h2>
<p class="subline">Meta information bla bla</p>
<div class="description">
<p>Paragraph one</p>
<p>Paragraph two</p>
</div>
</li>
<!-- More ... -->
</ul>
I'd like to link each of the li
s to a link. As proof of concept, I give you this invalid code:
<ul>
<li>
<a href="http://www.google.com/search?q=test+1">
<h2>Item 1</h2>
<p class="subline">Meta information bla bla</p>
<div class="description">
<p>Paragraph one</p>
<p>Paragraph two</p>
</div>
</a>
</li>
<!-- More ... -->
</ul>
Obviously, this won't validate because I can't have block-level elements inside inline elements.
EDIT: As it turns out, the code above is valid in HTML5. Problem solved.
I need to find another solution:
a
tag into each block level elementI considered adding identical h2 > a
, p.subline > a
, and div > p > a
tags, but I'd like to have a hover state using :hover
that affects the whole area of the link, so that won't work.
I have used Javascript to solve this problem before (li.onclick = function() { window.location.href = ...
), but then I can't use the middle mouse key to open in a new window. This affects usability, depends on Javascript, and is, frankly, super annoying.
<ul>
<li>
<a href="http://www.google.com/search?q=test+1">
<span class="title">Item 1</span>
<span class="subline">Meta information bla bla</span>
<span class="description">
<span>Paragraph one</span>
<span>Paragraph two</span>
</span>
</a>
</li>
<!-- More ... -->
</ul>
Most likely, display: block
would need to be applied to some of or all of those spans to make them behave.
This is valid HTML, but it's really kind of awful.
Does anyone know the best way to tackle this problem?
It is best practice to use HTML semantic elements for the proper assembly of your web page. Avoid using <divs> in place of HTML semantics. Do not use <div> elements to show headers and footers on your web page. Use semantic <header> and <footer> elements instead.
7.1 Introduction to the structure of an HTML document An HTML 4 document is composed of three parts: a line containing HTML version information, a declarative header section (delimited by the HEAD element), a body, which contains the document's actual content.
Use HTML5:
http://davidwalsh.name/html5-elements-links
HTML5 presents a simpler line of thought with HTML than XHTML. And quite honestly, it's a much needed simplification. One of those simplifications is the ability to wrap block-level elements like DIVs, H-tags, and P's with basic A elements. You read that correctly: wrap block-level elements with A tags.
Personally, I've never worried about this. Yea, it's technically invalid, but I'm a fan of pragmatic validation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With