HTML Unordered List

HTML unordered lists are used to display a collection of items that do not require any specific order. Each item in the list is marked with a bullet (default), but this can be customized using CSS. Unordered lists are commonly used for lists of items where the sequence is not important, such as a list of ingredients, features, or bullet points in a presentation.

Basic Syntax

The <ul> element is used to define an unordered list, and each list item is defined using the <li> element.

Example:

html
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Basic Unordered List</title>
</head>

<body>
    <h2>Basic Unordered List Example</h2>
    <ul>
        <li>First item</li>
        <li>Second item</li>
        <li>Third item</li>
    </ul>
</body>

</html>

Changing the List Bullet Type

HTML allows you to change the bullet type of unordered lists using the list-style-type property in CSS. The possible values for the list-style-type property are:

  • disc (default): solid circle
  • circle: hollow circle
  • square: solid square
  • none: no bullets

Example:

html
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Unordered List with Different Bullet Types</title>
    <style>
        ul.disc {
            list-style-type: disc;
        }
        ul.circle {
            list-style-type: circle;
        }
        ul.square {
            list-style-type: square;
        }
        ul.none {
            list-style-type: none;
        }
    </style>
</head>

<body>
    <h2>Unordered List with Different Bullet Types</h2>

    <h3>Default Bullets (Disc)</h3>
    <ul class="disc">
        <li>First item</li>
        <li>Second item</li>
        <li>Third item</li>
    </ul>

    <h3>Circle Bullets</h3>
    <ul class="circle">
        <li>First item</li>
        <li>Second item</li>
        <li>Third item</li>
    </ul>

    <h3>Square Bullets</h3>
    <ul class="square">
        <li>First item</li>
        <li>Second item</li>
        <li>Third item</li>
    </ul>

    <h3>No Bullets</h3>
    <ul class="none">
        <li>First item</li>
        <li>Second item</li>
        <li>Third item</li>
    </ul>
</body>

</html>

Nested Unordered Lists

You can nest unordered lists inside other lists to create multi-level lists. This is useful for representing hierarchical structures, such as subcategories or nested bullet points.

Example:

html
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Nested Unordered List</title>
</head>

<body>
    <h2>Nested Unordered List Example</h2>
    <ul>
        <li>Item 1
            <ul>
                <li>Subitem 1.1</li>
                <li>Subitem 1.2</li>
            </ul>
        </li>
        <li>Item 2
            <ul>
                <li>Subitem 2.1</li>
                <li>Subitem 2.2</li>
            </ul>
        </li>
        <li>Item 3</li>
    </ul>
</body>

</html>

Customizing Bullets with CSS

You can use CSS to customize the appearance of the bullets in unordered lists. This includes changing the color, size, or even replacing the bullets with custom images or icons.

Example:

html
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Styled Unordered List</title>
    <style>
        ul.custom-bullets {
            list-style-type: none;
            padding-left: 0;
        }
        ul.custom-bullets li {
            position: relative;
            padding-left: 20px;
        }
        ul.custom-bullets li::before {
            content: "\2022"; /* Unicode for bullet */
            color: red; /* Bullet color */
            font-size: 20px; /* Bullet size */
            position: absolute;
            left: 0;
        }
    </style>
</head>

<body>
    <h2>Styled Unordered List Example</h2>
    <ul class="custom-bullets">
        <li>First item</li>
        <li>Second item</li>
        <li>Third item</li>
    </ul>
</body>

</html>

In this example, the bullets are customized to be red and larger than the default size.

Using Images as Bullets

You can replace the default bullets with custom images using the list-style-image property in CSS.

Example:

html
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Unordered List with Custom Image Bullets</title>
    <style>
        ul.image-bullets {
            list-style-image: url('bullet.png');
        }
    </style>
</head>

<body>
    <h2>Unordered List with Custom Image Bullets</h2>
    <ul class="image-bullets">
        <li>First item</li>
        <li>Second item</li>
        <li>Third item</li>
    </ul>
</body>

</html>

Conclusion

HTML unordered lists are a versatile and essential part of web development, used for displaying items without a specific order. By using various CSS properties, you can customize the appearance of your unordered lists to match the design and style of your website. Whether you need simple bullet points or complex nested lists with custom styling, understanding how to use and style unordered lists will enhance the presentation of your content.

tools

Computer Science

Related Articles