Lesson 4 / 850%

Lesson 4

Lists and links

18 min

ul, ol, li and the a tag to navigate between pages.

Unordered lists

An unordered list (ul, "unordered list") displays its items with a bullet, when the order doesn't matter. Each item is written with<li>("list item").

ul.html
<ul>
  <li>HTML</li>
  <li>CSS</li>
  <li>JavaScript</li>
</ul>

Result (open in a browser)

unordered list example

Ordered lists

An ordered list (ol, "ordered list") automatically numbers its items, which is useful for steps to follow in order.

ol.html
<ol>
  <li>Open the editor</li>
  <li>Create the index.html file</li>
  <li>Open the page in the browser</li>
</ol>

<!-- You can change the starting point -->
<ol start="5">
  <li>Fifth step</li>
  <li>Sixth step</li>
</ol>

<!-- Or reverse the order -->
<ol reversed>
  <li>Last</li>
  <li>Second-to-last</li>
</ol>

Result (open in a browser)

ordered list example

Nesting lists

You can place a list inside an<li>to create subcategories, like a multi-level outline.

nested-list.html
<ul>
  <li>
    Frontend
    <ul>
      <li>HTML</li>
      <li>CSS</li>
      <li>JavaScript</li>
    </ul>
  </li>
  <li>
    Backend
    <ul>
      <li>Node.js</li>
      <li>Python</li>
    </ul>
  </li>
</ul>

Result (open in a browser)

nested list example

Description lists

Less well known, the description list (dl) associates a term (dt) with its definition (dd) — handy for a glossary or an FAQ.

dl.html
<dl>
  <dt>HTML</dt>
  <dd>Language for structuring web content.</dd>
  <dt>CSS</dt>
  <dd>Language for visual styling.</dd>
</dl>

Creating links

To create a link to another page or another site, you use the<a>tag ("anchor") with thehrefattribute ("hypertext reference").

links.html
<!-- External link -->
<a href="https://developer.mozilla.org">MDN documentation</a>

<!-- Relative internal link -->
<a href="/contact">Contact page</a>

<!-- Open in a new tab -->
<a href="https://example.com" target="_blank" rel="noopener noreferrer">
  Open in a new tab
</a>

<!-- Link to an email address -->
<a href="mailto:contact@example.com">Write to us</a>

<!-- Link to a phone number -->
<a href="tel:+15551234567">555 123 4567</a>

<!-- Link to an anchor on the same page -->
<a href="#contact-section">Go to the contact section</a>
<section id="contact-section">...</section>
  • target="_blank" opens the link in a new tab.
  • rel="noopener noreferrer" is recommended with target="_blank" for security and performance reasons.
  • A link that starts with / points to a page on your own site (absolute path from the root).
  • A link that starts with # jumps to an element with that id on the same page.

Common mistake

Never use <a href="#">on its own to trigger a JavaScript action: it scrolls the page back to the top. Use a <button>for actions, and keep <a>for navigation.

Exercise

Create a bulleted list of your 3 favorite websites, where each item is a clickable link that opens in a new tab.

Go further

Turn your list into a nested list: for each site, add a sublist of 2 specific pages on that site that you like. Also add amailto:link to your email address at the bottom of the page.