5

I'm trying to make an unordered list where the list items are links, not just the text inside them. But this is not valid. When checking my code with https://validator.w3.org/check I receive the message

Element a not allowed as child of element ul in this context.

This is the code:

<html lang="en">
<head>
    <meta charset="utf-8">
    <title>test</title>
    <style>
        ul {
            list-style:none;
        }
        a {
            text-decoration:none;
            color: #212121;
            font-size: 1em;
            font-family: Arial, Helvetica, sans-serif;
        }
        #container {
            padding:20px;
        }

        .valid {
            background-color:blanchedalmond;
        }

        .invalid {
            background-color:aquamarine;
        }

        .nav-item {
            width:120px;
            height:34px;
            margin:4px;
            line-height:34px;
            text-align:center;
            border: solid 1px #212121;
        }
    </style>
</head>
<body>

    <div id="container">
        <ul>
            <li class="valid nav-item"><a href="index.html">valid</a></li>
            <a href="index.html"><li class="invalid nav-item">invalid</li></a>
        </ul>
    </div>
</body>
</html>

The invalid arrangement of <a><li></li></a> is what I want to achieve behavior-wise with the entire <li> element being selectable as a link.

If I want to maintain valid code what is the best way to achieve a selectable <li>?

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
ryanr
  • 73
  • 1
  • 1
  • 6
  • 1
    possible duplicate of [Make whole
  • as link with proper HTML](http://stackoverflow.com/questions/13745347/make-whole-li-as-link-with-proper-html)
  • – Liglo App Jun 18 '15 at 10:52
  • you can add onClick event like this `
  • Click Here
  • ` – Mitul Jun 18 '15 at 10:55
  • possible duplicate of [Validation Error: Element a not allowed as child of element ul in this context. (Suppressing further errors from this subtree.)](http://stackoverflow.com/questions/22599801/validation-error-element-a-not-allowed-as-child-of-element-ul-in-this-context) – Liam Jun 18 '15 at 10:55
  • 6
    Five people managed to post the exact same answer while failing to answer the actual question. Incredible. The OP *knows* that
  • is the valid way of constructing elements - this is reflected as much in the original markup. The question is not how to nest the elements validly, it is how to achieve a certain layout (not performance) using valid markup. – BoltClock Jun 18 '15 at 10:59