How to make the HTML link activated by clicking on the <li>?

HtmlCssMenuHtml ListsAnchor

Html Problem Overview


I have the following markup,

<ul id="menu">              
    <li><a href="#">Something1</a></li>
    <li><a href="#">Something2</a></li>
    <li><a href="#">Something3</a></li>
    <li><a href="#">Something4</a></li>
</ul>

The <li> is a bit big, with a small image on its left, I have to actually click on the <a> to activate the link. How can I make a click on the <li> activate the link?

Edit1:

ul#menu li
{
    display:block;
    list-style: none;
    background: #e8eef4 url(images/arrow.png) 2% 50% no-repeat;
    border: 1px solid #b2b2b2;
    padding: 0;
    margin-top: 5px;
}

ul#menu li a
{

    font-weight: bold;
    text-decoration: none;
    line-height: 2.8em;
    padding-right:.5em;
    color: #696969;
}

Html Solutions


Solution 1 - Html

#menu li { padding: 0px; }
#menu li a { margin: 0px; display: block; width: 100%; height: 100%; }

It may need some tweaking for IE6, but that's about how you do it.

Solution 2 - Html

As Marineio said, you could use the onclick attribute of the <li> to change location.href, through javascript:

<li onclick="location.href='http://example';"> ... </li>

Alternatively, you could remove any margins or padding in the <li>, and add a large padding to the left side of the <a> to avoid text going over the bullet.

Solution 3 - Html

Just to throw this option out there:

<ul id="menu">
    <a href="#"><li>Something1</li></a>
    <a href="#"><li>Something2</li></a>
    <a href="#"><li>Something3</li></a>
    <a href="#"><li>Something4</li></a>
</ul>

This is the style I use in my menus, it makes the list item itself a hyperlink (similar to how one would make an image a link).
For styling, I usually apply something like this:

nav ul a {
    color: inherit;
    text-decoration: none;
}

I can then apply whatever styling to the <li> that I wish.

Note: Validators will complain about this method, but if you're like me and do not base your life around them, this should work just fine.

Solution 4 - Html

Just add wrap the link text in a 'p' tag or something similar and add margin and padding to that element, this way it wont affect the settings that MiffTheFox gave you, i.e.

<li> <a href="#"> <p>Link Text </p> </a> </li>

Solution 5 - Html

This will make whole <li> object as a link :

<li onclick="location.href='page.html';"  style="cursor:pointer;">...</li>

Solution 6 - Html

The following seems to work:

ul#menu li a {
    color:#696969;
    display:block;
    font-weight:bold;
    line-height:2.8;
    text-decoration:none;
    width:100%;
}

Solution 7 - Html

jqyery this is another version with jquery a little less shorter. assuming that the <a> element is inside de <li> element

$(li).click(function(){
    $(this).children().click();
});

Solution 8 - Html

Or you can create an empty link at the end of your <li>:

<a href="link"></a>

.menu li{position:relative;padding:0;}
.link{
     bottom: 0;
     left: 0;
     position: absolute;
     right: 0;
     top: 0;
}

This will create a full clickable <li> and keep your formatting on your real link. It could be useful for <div> tag as well

Solution 9 - Html

Anchor href link apply to li:

#menu li a {
       display:block;
    }

Solution 10 - Html

a {
  display: block;
  position: relative;
}

I think that is all you need.

Solution 11 - Html

You could try an "onclick" event inside the LI tag, and change the "location.href" as in javascript.

You could also try placing the li tags within the a tags, however this is probably not valid HTML.

Solution 12 - Html

> How to make the HTML link activated by clicking on the <li> ?

By making your link as big as your li: just move the instruction

display: block;

from li to a and you are done.

That is:

#menu li
{
	/* no more display:block; on list item */

	list-style: none;
	background: #e8eef4 url(arrow.gif) 2% 50% no-repeat;
	border: 1px solid #b2b2b2;
	padding: 0;
	margin-top: 5px;
}

#menu li a
{
	display:block; /* moved to link */
	font-weight: bold;
	text-decoration: none;
	line-height: 2.8em;
	padding-right:.5em;
	color: #696969;
}

Side note: you can remove "ul" from your two selectors: #menu is a sufficient indication except if you need to give weight to these two rules in order to override other instructions.

Solution 13 - Html

Use jQuery so you don't have to write inline javascript on <li> element:

$(document).ready(function(){
	$("li > a").each(function(index, value) {
		var link = $(this).attr("href");
		$(this).parent().bind("click", function() {
			location.href = link;
		});
	});
});	

Solution 14 - Html

I found a easy solution: make the tag " li "be inside the tag " a ":

<a href="#"><li>Something1</li></a>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSanView Question on Stackoverflow
Solution 1 - HtmlMiffTheFoxView Answer on Stackoverflow
Solution 2 - HtmlEricView Answer on Stackoverflow
Solution 3 - HtmlDuncanView Answer on Stackoverflow
Solution 4 - HtmlAdamView Answer on Stackoverflow
Solution 5 - HtmlDejanRView Answer on Stackoverflow
Solution 6 - HtmlSinan ÜnürView Answer on Stackoverflow
Solution 7 - HtmlMijailView Answer on Stackoverflow
Solution 8 - HtmlMrFloView Answer on Stackoverflow
Solution 9 - HtmlKhandoker HirokView Answer on Stackoverflow
Solution 10 - HtmlAbaas SkillsView Answer on Stackoverflow
Solution 11 - HtmlMarineioView Answer on Stackoverflow
Solution 12 - HtmlFelipeAlsView Answer on Stackoverflow
Solution 13 - HtmlIgnacio PascualView Answer on Stackoverflow
Solution 14 - Htmlrafaelsimoes85View Answer on Stackoverflow