views:

62

answers:

4

I have often wrapped block level elements with <a> tags in order to make the whole group clickable. For example, say I have a list of events:

<ul>
    <li>
        <a href="#" style="display: block;">
            <div style="float: left; display: inline;">12/12/2010</div>
            <div style="float: left; display: inline;">Some event information</div>
        </a>
    </li>
    <!-- etc... -->
</ul>

NOTE: inline styles applied for example only.

This way, the whole thing is clickable rather than only the text within the elements.

Ofcourse, the (x)html validator at validator.w3.org doesn't like this because I've placed a block level element (<div>) insider an inline level element (<a>). Even though, I am using CSS to define the <a> tags as block level, and the <div> tags as inline.

I've always coded by the rule of thumb that you should always strive to author valid code; however, if your code doesn't validate, and you understand why it doesn't validate, and you have a valid reason for it not validating, then don't worry about it.

So my question is tri-fold:

  1. Is this a valid reason for this not to validate?
  2. Is the usability gain (by having a larger clickable area) worth it not validating?
  3. Is there a better way?
+5  A: 

Using span as below is perfectly valid and you can achieve the same effect.

<a href="#" style="display: block;">
    <span style="float: left;">12/12/2010</span>
    <span style="float: left;">Some event information</span>
</a>
roryf
Adding display: inline; helps to prevent some css bugs in older versions of IE - http://www.positioniseverything.net/explorer/doubled-margin.html
Justin Gallagher
I wasn't aware of `float` implicitly setting an element to `display: block;` Now, a new concern with this solution, I've always understood that I should add `display: inline;` to any floated element to prevent IE double margin bugs, does this solution interfere with that bug solution?
jordanstephens
Keep your display: inline. The floats will still work the same as they do without that extra rule.
Justin Gallagher
Thanks for pointing this out, I wasn't aware of that.
roryf
+2  A: 

I would use <span> instead of <div> and then apply the same styling. That way you get the best of all worlds. You can still have your larger click targets, but it will also validate.

Justin Gallagher
A: 

With respect to your three questions:

  1. Not worth the energy to argue.
  2. Not if you can find a better way.
  3. Use <span> tags instead of divs (really, what is to be gained by making a div display:inline?); or use Javascript and add an onclick event to the containing <li>.
Robusto
First, this post really isn't helpful at all: you've effectively said absolutely nothing. Second, the reason I set the div to `display: inline;` is to fix the IE double margin bug on floated elements.
jordanstephens
@jordanstephens: If you read a little closer, you'll see I said to use real inline elements instead of block elements that have been twisted into becoming inline. That way you don't need to float them. Your code took something simple and wrestled it into something complicated. You wouldn't have had to worry about an IE double margin bug if you hadn't done that in the first place.
Robusto
A: 
  1. In my opinion, yes. However, you're pretty much on your own if you decide to disregard validation.

  2. Benefits to the user win over benefits to the author, certainly.

  3. Use HTML5, where <a> elements are allowed to wrap block level elements.

Alohci
in regards to point #3, I wasn't aware of this change. I don't think I'm comfortable switching to HTML5 just yet; but that's one more thing to look forward to!
jordanstephens