How to make the entire area of โ€‹โ€‹a list item in my navigation bar accessible by link?

I have a horizontal navigation bar made from an unordered list, and there are many indents in each element of the list to make it look beautiful, but the only area that acts as a link is the text itself. How can I let the user click anywhere in the list item to activate the link?

#nav { background-color: #181818; margin: 0px; overflow: hidden; } #nav img { float: left; padding: 5px 10px; margin-top: auto; margin-bottom: auto; vertical-align: bottom; } #nav ul { list-style-type: none; margin: 0px; background-color: #181818; float: left; } #nav li { display: block; float: left; padding: 25px 10px; } #nav li:hover { background-color: #785442; } #nav a { color: white; font-family: Helvetica, Arial, sans-serif; text-decoration: none; } 
 <div id="nav"> <img src="/images/renderedicon.png" alt="Icon" height="57" width="57" /> <ul> <li><a href="#">One1</a></li> <li><a href="#">Two</a></li> <li><a href="#">Three</a></li> <li><a href="#">Four</a></li> </ul> </div> <div> <h2>Heading</h2> </div> 
+88
html css anchor
Jun 19 '10 at 5:22
source share
10 answers

Do not put padding in the "li" element. Instead, attach the anchor tag to display:inline-block; and apply a gasket to it.

+104
Jun 19 '10 at 5:24
source share

Define the css property for the tag binding as:

 {display:block} 

Then the anchor will occupy the entire area of โ€‹โ€‹the list, so your click will work in an empty place next to your list.

+43
Jul 28 '12 at 4:08
source share

Make the anchor tag contain an addition, not li . Thus, it will occupy the entire area.

+12
Jun 19 '10 at 5:36
source share

Use the following:

 a { display: list-item; list-style-type: none; } 
+9
Aug 24 '15 at 20:19
source share

Super, super late for this party, but anyway: you can also anchor the anchor to the flex element. This is especially useful for dynamic size / ordered list items.

 a { /* This flexbox code stretches the link clickable * area to fit its parent block. */ display: flex; flex-grow: 1; flex-shrink: 1; justify-content: center; } 

(Caveat: flexboxes obvs is still not supported. Autoprefixer for rescue!)

+7
Jun 13 '15 at 12:13
source share

Or you can use jQuery:

 $("li").click(function(){ window.location=$(this).find("a").attr("href"); return false; }); 
+6
Apr 15 '14 at 2:36
source share

You should use this CSS property and value in your li :

 pointer-events:all; 

So, you can process the link using jQuery or JavaScript or use the a tag, but all the other elements of the tag inside li must have the CSS property:

 pointer-events:none; 
+1
May 12 '16 at 20:54
source share

Just apply the following CSS:

 <style> #nav ul li { display: inline; } #nav ul li a { background: #fff;// custom background padding: 5px 10px; } </style> 
0
Aug 28 '19 at 13:35
source share

You can always wrap the entire li tag with a hiperlink tag. Here is my solution:

 #nav { background-color: #181818; margin: 0px; overflow: hidden; } #nav img { float: left; padding: 5px 10px; margin-top: auto; margin-bottom: auto; vertical-align: bottom; } #nav ul { list-style-type: none; margin: 0px; background-color: #181818; float: left; } #nav li { display: block; text-align: center; float: left; width: 40px; padding: 25px 10px; } #nav li:hover { background-color: #785442; } #nav a { color: white; font-family: Helvetica, Arial, sans-serif; text-decoration: none; } 
 <div id="nav"> <img src="/images/renderedicon.png" alt="Icon" height="57" width="57" /> <ul> <a href="#"><li>One1</li></a> <a href="#"><li>Two</li></a> <a href="#"><li>Three</li></a> <a href="#"><li>Four</li></a> </ul> </div> <div> <h2>Heading</h2> </div> 

-one
Jul 25 '18 at 15:03
source share

Place the list item inside the hyperlink, not the other way around.

For example, with the code:

 <a href="#"><li>One</li></a> 
-2
Aug 18 '13 at 14:22
source share



All Articles