CodeBlanks Logo Icon CodeBlanks

Links in list items

HTML course · Beginner · Lesson 28/68

Place links inside <li> to build menus and content lists.

A link inside a list item

You create a link by placing the <a> tag — which you already know — inside an <li> item. Example: <li><a href="about.html">About</a></li>.

The order matters: the <a> tag must be inside the <li>. The reverse (putting <li> inside <a>) is invalid; the browser reads it wrong.

Navigation list

A site's top menu is usually a <ul> with linked <li> items inside. This is called a navigation list. Each item links to a page in the site.

Example: a menu holding Home, About, Contact. The visual styling is done with CSS; on the HTML side there is only the structure and the links.

Exercise Write a three-link menu

Add a <ul> menu to body. It should have three <li> items; each containing an <a href="..."> link. For example Home (index.html), About (about.html), Contact (contact.html).

Show example solution
<body>
  <ul>
    <li><a href="index.html">Home</a></li>
    <li><a href="about.html">About</a></li>
    <li><a href="contact.html">Contact</a></li>
  </ul>
</body>

Test yourself

  1. How is a link (<a>) written inside a list item?

    • <a><li>Link</li></a>
    • <li><a href="x.html">Link</a></li>
    • <li>Link</li><a href="x.html"></a>
    • <a href="x.html"></a><li>Link</li>
    Show answer

    <li><a href="x.html">Link</a></li> — Correct. a is written inside li; the reverse is invalid.

  2. Give the link a target

    <li><a ____>Home</a></li>
    Show answer
    <li><a href="index.html">Home</a></li>
  3. Match each attribute to its role

    href
    The link's target
    target
    Where the link opens
    rel
    Relation between pages
    id
    An id for in-page anchors

All lessons in Beginner