Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could i use <a> in <ul> around <li>

Ive got the following code:

<ul> 
     <a href="./index.php?profile=User1">
        <li>User1 : 16</li>
     </a>
     <a href="./index.php?profile=User2">
        <li>User2 : 4</li>
    </a>
</ul>

This works perfectly fine in all major browsers, but it isn't allowed/invalid HTML and the right way should be this:

<ul>
    <li> 
        <a href="./index.php?profile=User1">User1 : 16</a>
    </li>
    <li> 
        <a href="./index.php?profile=User2">User2 : 4</a>
    </li>
</ul>

But if I do it like the second example only the text not the whole <li> is clickable like i want it to.
Should I stay with the invalid working version or has anyone a better solution?

like image 738
Vloxxity Avatar asked Apr 25 '13 09:04

Vloxxity


People also ask

Does a li need to be in a UL?

Answer: yes it does, but with: ol or menu, that's all! Save this answer.

Can you have a UL within a UL?

A nested list is a list inside another list. You can create a nested unordered list, or a nested ordered list, or even an ordered list nested inside an unordered one. Remember that the only direct child of the ul tag is li .

What elements can go inside a UL?

HTML ul element can reside within APPLET, BLOCKQUOTE, BODY, BUTTON, CENTER, DD, DEL, DIV, FIELDSET, FORM, IFRAME, INS, LI, MAP, NOFRAMES, NOSCRIPT, OBJECT, TD, TH.

Can you put a class on LI?

Classes (i.e. classnames) are used for styling the li element. Multiple classnames are separated by a space. JavaScript uses classes to access elements by classname. Tip: class is a global attribute that can be applied to any HTML element.


2 Answers

Use CSS to make the link take up the entire list item, eg. display: block (and any other styling you might want).

Wrapping links around list items is invalid HTML.

like image 106
sevenseacat Avatar answered Oct 06 '22 00:10

sevenseacat


Short answer is NO, it won't be validated, only li can be inside ul and ol elements.

So this is incorrect

<ul>
  <a><li></li></a>
</ul>

This is fine

<ul>
  <li><a></a></li>
</ul>
like image 39
Mr. Alien Avatar answered Oct 06 '22 00:10

Mr. Alien