Home ยป HTML Lists Tutorial

HTML Lists Tutorial

by shedders

Lists are an essential part of HTML and are used to display items in an organized way. HTML provides three types of lists: ordered lists, unordered lists, and definition lists. This tutorial will walk you through the basics of each list type, including advanced formatting and nesting.

1. Unordered Lists

Unordered lists display items with bullet points. They are created using the <ul> tag, and each list item is enclosed in <li> tags.

Example 1: Basic Unordered List

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Unordered List</title>
</head>
<body>
    <h1>Unordered List Example</h1>
    <ul>
        <li>Apples</li>
        <li>Bananas</li>
        <li>Cherries</li>
    </ul>
</body>
</html>

Output:

  • Apples
  • Bananas
  • Cherries

2. Ordered Lists

Ordered lists display items with numbers, letters, or Roman numerals. They are created using the <ol> tag.

Example 2: Basic Ordered List

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Ordered List</title>
</head>
<body>
    <h1>Ordered List Example</h1>
    <ol>
        <li>Step 1: Prepare ingredients</li>
        <li>Step 2: Mix ingredients</li>
        <li>Step 3: Bake in the oven</li>
    </ol>
</body>
</html>

Output:

  1. Step 1: Prepare ingredients
  2. Step 2: Mix ingredients
  3. Step 3: Bake in the oven

3. Nested Lists

Lists can be nested to represent hierarchical data. You can combine unordered and ordered lists within each other.

Example 3: Nested List

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Nested List</title>
</head>
<body>
    <h1>Nested List Example</h1>
    <ul>
        <li>Fruits
            <ul>
                <li>Apples</li>
                <li>Bananas</li>
            </ul>
        </li>
        <li>Vegetables
            <ol>
                <li>Carrots</li>
                <li>Broccoli</li>
            </ol>
        </li>
    </ul>
</body>
</html>

Output:

  • Fruits
    • Apples
    • Bananas
  • Vegetables
    1. Carrots
    2. Broccoli

4. Definition Lists

Definition lists are used for terms and their descriptions. They are created using <dl> (definition list), <dt> (definition term), and <dd> (definition description).

Example 4: Definition List

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Definition List</title>
</head>
<body>
    <h1>Definition List Example</h1>
    <dl>
        <dt>HTML</dt>
        <dd>The standard language for creating web pages.</dd>
        <dt>CSS</dt>
        <dd>A stylesheet language used to style HTML elements.</dd>
        <dt>JavaScript</dt>
        <dd>A programming language used to create dynamic web content.</dd>
    </dl>
</body>
</html>

Output:
HTML
The standard language for creating web pages.

CSS
A stylesheet language used to style HTML elements.

JavaScript
A programming language used to create dynamic web content.

5. Customizing List Styles

You can style lists with CSS to change bullet points, numbering styles, or indentation.

Example 5: Custom List Styles

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Custom List Styles</title>
    <style>
        ul {
            list-style-type: square; /* Changes bullet points to squares */
        }
        ol {
            list-style-type: upper-roman; /* Roman numerals for ordered list */
        }
        .custom {
            list-style-type: none; /* Removes bullets */
        }
    </style>
</head>
<body>
    <h1>Custom List Styles</h1>
    <ul>
        <li>Item 1</li>
        <li>Item 2</li>
    </ul>
    <ol>
        <li>First</li>
        <li>Second</li>
    </ol>
    <ul class="custom">
        <li>No bullet 1</li>
        <li>No bullet 2</li>
    </ul>
</body>
</html>

Output:

  • Square Bullets for the first unordered list.
  • Roman Numerals for the ordered list.
  • No Bullets for the ul with the custom class.

6. Horizontal Lists

You can create horizontal lists by using CSS to display list items inline.

Example 6: Horizontal List

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Horizontal List</title>
    <style>
        ul {
            list-style-type: none; /* Remove bullet points */
            padding: 0;
        }
        li {
            display: inline;
            margin-right: 20px;
        }
    </style>
</head>
<body>
    <h1>Horizontal List Example</h1>
    <ul>
        <li>Home</li>
        <li>About</li>
        <li>Contact</li>
    </ul>
</body>
</html>

Output:
Home | About | Contact (displayed in a single line with spaces in between).

7. Accessible Lists

For better accessibility, ensure:

  • Use <ul> or <ol> appropriately to match the content hierarchy.
  • Use proper semantics for screen readers to interpret the structure correctly.
  • Avoid excessive nesting to maintain readability.

8. Combining Lists

You can mix and match different types of lists for complex structures.

Example 7: Combined Lists

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Combined Lists</title>
</head>
<body>
    <h1>Combined Lists Example</h1>
    <ol>
        <li>HTML Basics
            <ul>
                <li>Tags</li>
                <li>Attributes</li>
            </ul>
        </li>
        <li>CSS Basics
            <dl>
                <dt>Selectors</dt>
                <dd>Used to select elements on a page.</dd>
                <dt>Properties</dt>
                <dd>Define how elements are styled.</dd>
            </dl>
        </li>
    </ol>
</body>
</html>

Output:

  1. HTML Basics
    • Tags
    • Attributes
  2. CSS Basics
    Selectors: Used to select elements on a page.
    Properties: Define how elements are styled.

Conclusion

In this tutorial, you learned:

  1. How to create unordered, ordered, and definition lists.
  2. How to nest lists for hierarchical data.
  3. How to style lists with CSS for custom appearances.
  4. How to make lists horizontal for menus or navigation.

Experiment with these examples and use lists effectively to structure your webpage content

You may also like