views:

81

answers:

4

How do I rewrite this HTML to validate to XHTML 1.0 Strict?

<div>
    <a href="link.html">
        <p>Some text</p>
        <ul>
            <li>Item 1</li>
        <li>Item 2</li>
        <li>Item 3</li>
        </ul>
    </a>
</div>

My intent is to have the entire div serve as a clickable link. The content within simply describes the contents of the destination page. W3 Validator says I can't have a block element (<p>) inside a span tag (<a>).

How do I rearrange this so that my DIVs remain block links?

+3  A: 

You're not allowed to wrap a block level element in an inline level element so you have a few alternatives.

  • You can wrap every line that you want linked in the <a href="link.html">...</a>

    <div>
        <p><a href="link.html">Some text</a></p>
        <ul>
            <li><a href="link.html">Item 1</a></li>
            <li><a href="link.html">Item 2</a></li>
            <li><a href="link.html">Item 3</a></li>
        </ul>
    </div>​  
    
  • You can add a javascript onclick function to reproduce the same results.

     //jQuery
     ​$('div').click(function() {
         window.location = 'link.html';
     });​​​​​​
    
    
     //Non jQuery
     document.getElementById('yourDiv').onclick = function() {
         window.location = 'link.html';
     }
    
    • If you use the Javascript, make sure you use CSS to make it apparent that the contents are links. I'd recommend pseudo classes

      div {
          text-decoration: underline;
          color: #0000FF;//or whatever your link color is
      }
      div li:hover, p:hover {
          color: #CC00FF;
          cursor: pointer;
      }
      

Robert
You should mention that this solution depends on jQuery.
jigfox
@jigfox Makes sense, updated.
Robert
Hi Robert! If I use the jQuery function, where do I put it? The page in question has anywhere from 5 to 30 of these div blocks, each going to a different page.
Andrew Heath
I'd wrap it in a `$(document).ready(function() {...here...});` Be sure to change the `div` selector to something relevant.
Robert
+1  A: 

You can't rearrange it to make the block a link. What you could do is to make every single element in the block a link, or you can use javascript.

<div style="cursor:pointer" onclick="location.href='link.html'">
  <!-- ... -->
</div>
jigfox
I'd usually discourage the use of `onclick` as an attribute, but I approve of your use of `cursor:pointer` which has been neglected in the other posts. +1
LeguRi
@LeguRi, I agree with you, normally I don't use it too, but I wanted to present a quick and short solution. And Robert already presented the better solution.
jigfox
A: 

Your <UL> is also a block element, so it won't work there either. How about:

<div onclick="location.href = 'link.html'">
    <p>Some text</p>
    <ul>
        <li>Item 1</li>
        <li>Item 2</li>
        <li>Item 3</li>
    </ul>
</div>
gilly3
+1  A: 

As is, your fragment is valid HTML5. Use that instead and your problem vanishes. All you have to do is change the doctype to <!doctype html>.

Zack
Really!? Do you have any links to cite? Did they ditch the whole "block-level versus inline level" thing in HTML5?
LeguRi
Yup. See http://www.whatwg.org/specs/web-apps/current-work/multipage/content-models.html -- particularly the description of "flow content", which seems to have subsumed both categories; also, at http://www.whatwg.org/specs/web-apps/current-work/multipage/text-level-semantics.html#the-a-element the `<a>` element is specifically allowed to contain "any flow content" but to still be treated as "phrasing content" if its contents are entirely "phrasing content".
Zack
Also, http://validator.nu/ is an HTML5 validator. I determined my original answer by pasting the OP's fragment into the skeleton document it gives you. No errors.
Zack