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; } } }
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.
<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.
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.
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.
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
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