Skip to content

Lists

HTML provides several elements for creating lists, which are ideal for grouping related items together. Using the correct list type helps structure your content semantically, making it more understandable for users, browsers, and search engines.

Unordered Lists (<ul>, <li>)

Use an unordered list when you have a collection of items where the order does not matter. Think of shopping lists, feature lists, or navigation menus.

  • <ul> (Unordered List): This block-level element acts as the container for the entire list.
  • <li> (List Item): Each individual item within the list must be wrapped in an <li> element. You place <li> elements directly inside the <ul>.

Browsers typically render unordered list items with a bullet point (like a disc, circle, or square) by default. The specific bullet style can be controlled using the CSS list-style-type property.

Example:

html
<h2>Programming Languages I Use:</h2>
<ul>
    <li>HTML</li>
    <li>CSS</li>
    <li>JavaScript</li>
    <li>Python</li>
</ul>

<h2>Shopping List:</h2>
<ul>
    <li>Milk</li>
    <li>Eggs</li>
    <li>Bread</li>
</ul>

Ordered Lists (<ol>, <li>)

Use an ordered list when the sequence or order of the items is important. Think of step-by-step instructions, rankings, or legal outlines.

  • <ol> (Ordered List): This block-level element acts as the container for the list where order matters.
  • <li> (List Item): As with unordered lists, each individual item must be wrapped in an <li> element, placed directly inside the <ol>.

Browsers typically render ordered list items with sequential markers, usually numbers (1, 2, 3...), by default.

The <ol> element supports several attributes to modify the numbering:

  • type: Specifies the type of marker to use. Common values:
    • 1: Decimal numbers (default)
    • A: Uppercase letters
    • a: Lowercase letters
    • I: Uppercase Roman numerals
    • i: Lowercase Roman numerals (Note: While functional, using CSS (list-style-type) is generally preferred for controlling list appearance nowadays).
  • start: Specifies the number/letter where the list should start counting from. Must be an integer, regardless of the type.
html
<ol start="5"> 
  <li>Fifth item</li>
  <li>Sixth item</li>
</ol>
  • reversed: A boolean attribute. If present, the list markers will count down instead of up.
html
<ol reversed> 
  <li>Item one</li>
  <li>Item two</li>
  <li>Item three</li>
</ol>

Example:

html
<h2>Website Deployment Steps:</h2>
<ol>
    <li>Write HTML, CSS, and JavaScript code.</li>
    <li>Test thoroughly in multiple browsers.</li>
    <li>Choose a hosting provider.</li>
    <li>Upload files to the server.</li>
    <li>Configure DNS settings.</li>
</ol>

<h2>Top 3 Browsers (Example):</h2>
<ol type="I" start="1">
    <li>Chrome</li>
    <li>Firefox</li>
    <li>Safari</li>
</ol>

Definition Lists (<dl>, <dt>, <dd>)

Definition lists are used to represent lists of terms and their corresponding descriptions or definitions. They are ideal for glossaries, key-value pairs (like item metadata), or dialogue transcripts.

  • <dl> (Definition List): This block-level element acts as the container for the entire list of terms and descriptions.
  • <dt> (Definition Term): Represents the term or name being defined. You can have one or more <dt> elements for each item.
  • <dd> (Definition Description): Represents the description, definition, or value associated with the preceding <dt>. You can have one or more <dd> elements for each term.

The structure within a <dl> is typically one or more <dt> elements followed by one or more <dd> elements. Browsers usually render the <dd> indented below the <dt>.

Example:

html
<h2>Web Technology Glossary:</h2>
<dl>
  <dt>HTML</dt>
  <dd>HyperText Markup Language. Used for structuring web content.</dd>

  <dt>CSS</dt>
  <dd>Cascading Style Sheets. Used for styling the appearance of web content.</dd>
  <dd>It controls layout, colors, fonts, etc.</dd> 

  <dt>JS</dt>
  <dt>JavaScript</dt> <dd>A programming language used to create dynamic and interactive web content.</dd>
</dl>

Nesting Lists

You can create hierarchical lists by placing one list inside a list item (<li>) of another list. This is called nesting. You can nest any list type (<ul>, <ol>, <dl>) inside another.

The key is that the entire nested list structure (<ul>...</ul> or <ol>...</ol>) must be placed inside the <li> element of the parent list it belongs to.

Example:

html
<h2>Course Outline:</h2>
<ol>
    <li>
        Introduction
        <ul> 
            <li>What is HTML?</li>
            <li>History of HTML</li>
            <li>Basic Structure</li>
        </ul>
    </li>
    <li>
        Core Concepts
        <ul> 
            <li>Elements and Tags</li>
            <li>Attributes</li>
            <li>Nesting</li>
        </ul>
    </li>
    <li>
        Text Content
        <ol type="a"> 
            <li>Headings</li>
            <li>Paragraphs</li>
            <li>Formatting</li>
        </ol>
    </li>
    <li>Conclusion</li>
</ol>

Browsers typically apply different bullet styles or numbering schemes and add indentation for nested lists automatically.