wrap anchor tag around li element
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.
Can wrapping a div or an anchor tag around a <LI> still be considered valid html structure
No, it is not valid, though it often works.
Yes, the reason most people do like this is to make it clickable.
There is many ways to make any element besides an anchor clickable, where wrapping an anchor around it is the most used.
This is valid for all non block level elements, but will likely work on all element level types because of event bubbling.
For block level elements (can also be used on inline elements), one can do like this, to make the whole element clickable
HTML
<div class="clickable"><a href='....'></a></div>
CSS
.clickable a {
display: inline-block; height: 100%; width: 100%; /* fill the parent */
}
An alternative when one just can't use an anchor and still need it clickable, would be by using a click handler, like this, using jQuery.
$( "li" ).click(function() {
// Do something here on click
});
Link to wrap around li with padding
You can use the hover
the li
instead of the a
to correct the background-color
applied on hover
by using:
li:hover a {
display: block;
}
li:hover {
background-color: rgb(245, 245, 245);
}
instead of:
a:hover {
display:block;
background-color:rgb(245,245,245);
}
See demo below:
ul li { float: left; line-height: 5em; padding: 0 2em;}a:link { display: block;}a:visited { display: block;}li:hover a { display: block;}li:hover { background-color: rgb(245, 245, 245);}a:active { display: block; background-color: rgb(245, 245, 245);}
<ul> <li><a href="1.html">Item 1</a> </li> <li><a href="2.html">Item 2</a> </li> <li>Item 3</li> <li>Item 4</li> <li><a href="5.html">Item 5</a> </li></ul>
How to wrap anchor link around shape in css
Assign the CSS you have to the link itself, not to the container. Please see example below.
a { height: 3rem; width: 3rem; border-radius: 50%; margin: 0 3rem !important; display: inline-block !important; cursor: pointer;}
.red { background-color: red;}
.blue { background-color: blue;}
.yellow { background-color: yellow;}
.green { background-color: green;}
ul {list-style: none;display: flex;width: 50%;}
<ul> <li> <a href="#" class="red"></a> </li> <li> <a href="#" class="blue"></a> </li> <li> <a href="#" class="yellow"></a> </li> <li> <a href="#" class="green"></a> </li></ul>
HTML anchor tag takes entire line
Regular anchor Tags are inline elements. You have to check if in your CSS, you already assign anchors globally to a block element. a { display: block;}
For fast fix:
Wrap your code anchor line (Breadcrumbs
) in a container and assign with a unique id or class Name. Then you can assign only for this line the anchors to a inline element.
a {
display: block;
}
.anchor-regular a {
display: inline;
}
<a href="">test block </a> <a href="">test block</a>
<div class="anchor-regular">
<a href="">First Link</a> / <a href="">Second Link</a> / <a href="">Third Link</a>
</div>
How do i wrap a <li></li> around each anchor tag dynamically?
$text = str_replace("<a", "<li><a", str_replace('</a>', '</a></li>', $text));
Should do it.
Related Topics
Bootstrap 4: Center Inline Form Inside a Jumbotron
Integrating CSS Star Rating into an HTML Form
How to Get a Flexbox to Have a Center Fixed and a Bottom Fixed Children Together
Font Size with Percentage Value (%) Not Scaling with Screen Size
What Does "Top: 0; Left: 0; Bottom: 0; Right: 0;" Mean
Do You Need to Close Meta and Link Tags in HTML
How to Keep Styles After Animation
How to Hide The Arrow That Is Displayed by Default on The HTML5 <Details> Element in Chrome
How to Make a 45 Degree Responsive Ribbon with Folded Corner
Subpixel Anti-Aliased Text on HTML5's Canvas Element
How to Remove Only Underline from A:Before
Is It Ok to Have Multiple HTML Forms with The Same Name
Old Google Form Redirect After Submission
Photoshop Mock Up Font Isn't Same as in HTML
Twitter Bootstrap: Column Re-Ordering for Full Width Divs
How to Semantically Provide a Caption, Title or Label for a List in HTML