Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wrap anchor tag around li element

Tags:

html

css

I am creating navigation menu. I want to use css so that anchor tag is wrapped around li element but anchor tags are inside li element.

Here is html

<ul>     <li><a href="">Uutiset</a></li>     <li><a href="">Foorumi</a></li>     <li><a href="">Kauppa</a></li>     <li><a href="">Messut</a></li>     <li><a href="">Asiakaspalvelu</a></li>     <li><a href="">Nakoislehti</a></li>     <li><a href="">Nae meidat</a></li> </ul> 

here is my less css

ul {   list-style-type: none;   padding: 0;   margin: 0;   li {     padding: 2% 4%;     border: 1px solid green;     a {         text-decoration: none;         display: block;     }   } } 
like image 487
Om3ga Avatar asked Aug 23 '12 07:08

Om3ga


People also ask

Can you wrap an LI in an A?

The inline element is contained by the block element. That makes sense. An OL and UL parent element can only have LI as direct children. We would never wrap an LI in a link.

Can Li be wrapped in Div?

<li> can only be a child of <ul> or <ol> , so wrapping them in a <div> doesn't make much sense. You really shouldn't be doing this. Instead, wrap the contents of the li in a div, not the li itself.

Can you put AP tag in an LI?

For semantic purposes, content within the li element should be wrapped in a p tag. If there is more than one paragraph or section in a li , do not close the li until after the last element. This differs from the current standard, but is cleaner for accessible content.


2 Answers

The only legal element allowed inside a <ul> is an <li>. You cannot have an anchor wrapped around the <li>. This holds true in HTML5, where an anchor can wrap around other block level elements.

What you have in CSS is nearly there, just add:

a {      text-decoration: none;      display: block;      width: 100%;      height: 100%; } 

And your anchor shall fill the entire space of the <li>.


Update for 2022: wrapping your li tags with anchors is now totally acceptable.

like image 196
Kyle Avatar answered Sep 22 '22 07:09

Kyle


Dont use padding in li , use line-height for the anchor text instead. This will make it cover full height of li element .

Here, have a look at this Example

like image 28
ygssoni Avatar answered Sep 23 '22 07:09

ygssoni