Module 8: HTML Lists

Topics in
this Module

Lists in HTML

In HTML, you can create both ordered lists and unordered lists using the <ol> and <ul> elements, respectively. These elements are used to structure content into lists, and they can contain list items specified with the <li> element. Here's an explanation of these elements, along with their attributes and values, along with examples for each:

Unordered Lists (Using <ul>)

An unordered list is used to create a list of items where the order does not matter, and typically, list items are displayed with bullet points.

Attributes for <ul>:

type:

This attribute specifies the type of bullet point used for list items. The possible values are:

  • disc (default): A filled circle.
  • circle: An unfilled circle.
  • square: A filled square.

Example:

<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>

You can also specify the type attribute to change the bullet style:

<ul type="square">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>

Ordered Lists (Using <ol>)

An ordered list is used to create a list of items where the order matters, and list items are typically displayed with numbers or letters.

Attributes for <ol>:

type:

This attribute specifies the type of numbering used for list items. The possible values are:

  • 1 (default): Arabic numerals (1, 2, 3, ...).
  • A: Uppercase letters (A, B, C, ...).
  • a: Lowercase letters (a, b, c, ...).
  • I: Uppercase Roman numerals (I, II, III, ...).
  • i: Lowercase Roman numerals (i, ii, iii, ...).

start:

This attribute specifies the starting value for the list. It is useful when you want to begin the list at a number other than 1.

Example:

<ol type="A">
<li>Item A</li>
<li>Item B</li>
<li>Item C</li>
</ol>

You can also use the start attribute:

<ol type="i" start="5">
<li>Item v</li>
<li>Item vi</li>
<li>Item vii</li>
</ol>

List Items (Using <li>)

The <li> element is used within both <ul> and <ol> elements to define individual list items.

Attributes for <li>:

The <li> element does not have any specific attributes, but it can contain various HTML elements and attributes to structure the content within list items.

Example:

<ul>
<li><a href="#">Link 1</a></li>
<li><strong>Item 2</strong></li>
<li><em>Item 3</em></li>
</ul>

In the example above, each list item contains different HTML elements, such as links, strong text, and emphasized text, to format the content within the list items.

These HTML list elements (<ul>, <ol>, and <li>) are essential for organizing and presenting content in a structured manner on web pages. You can use their attributes and values to customize the appearance and behavior of lists as needed for your specific design and content requirements.



Nested Lists

In HTML, you can create nested lists of various kinds, including nested unordered lists (ul), nested ordered lists (ol), and combinations of both. Nested lists allow you to organize content hierarchically, making it easier to represent structured information. Here, I'll explain how to create nested lists and provide multiple examples for each type.

Nested Unordered Lists (ul)

You can nest unordered lists by placing one <ul> element inside another <li> element. Each nested <ul> represents a sub-list within the parent list item. Here's an example:

<ul>
<li>Main Item 1
<ul>
<li>Sub-item 1.1</li>
<li>Sub-item 1.2</li>
</ul>
</li>
<li>Main Item 2
<ul>
<li>Sub-item 2.1</li>
<li>Sub-item 2.2</li>
</ul>
</li>
</ul>

In the example above, you have a main unordered list with two list items, each containing a nested unordered list with two sub-items.

Here is what that would look like in your web browser:

  • Main Item 1
    • Sub-item 1.1
    • Sub-item 1.2
  • Main Item 2
    • Sub-item 2.1
    • Sub-item 2.2

Nested Ordered Lists (ol)

Creating nested ordered lists is similar to nested unordered lists. You nest <ol> elements within <li> elements. Here's an example:

<ol>
<li>Main Item A
<ol>
<li>Sub-item A.1</li>
<li>Sub-item A.2</li>
</ol>
</li>
<li>Main Item B
<ol>
<li>Sub-item B.1</li>
<li>Sub-item B.2</li>
</ol>
</li>
</ol>

In this example, you have a main ordered list with two list items, each containing a nested ordered list with two sub-items.

Here is what that would look like in your web browser:

  1. Main Item A
    1. Sub-item A.1
    2. Sub-item A.2
  2. Main Item B
    1. Sub-item B.1
    2. Sub-item B.2

Nested Combination of Lists (ul and ol)

You can also create nested combinations of ordered and unordered lists. Here's an example:

<ul>
<li>Main Item X
<ol>
<li>Sub-item X.1</li>
<li>Sub-item X.2
<ul>
<li>Sub-sub-item X.2.1</li>
<li>Sub-sub-item X.2.2</li>
</ul>
</li>
</ol>
</li>
<li>Main Item Y
<ul>
<li>Sub-item Y.1</li>
<li>Sub-item Y.2</li>
</ul>
</li>
</ul>

In this example, you have a main unordered list with two list items. The first list item contains a nested ordered list with one of its items containing a nested unordered list.

Here is how the browser would render that code:

  • Main Item X
    1. Sub-item X.1
    2. Sub-item X.2
      • Sub-sub-item X.2.1
      • Sub-sub-item X.2.2
  • Main Item Y
    • Sub-item Y.1
    • Sub-item Y.2

Remember that you can style nested lists using CSS to control their appearance, such as indentation and bullet/number styles, to ensure they are visually distinguishable. Nested lists are a powerful way to represent structured information on your web pages and enhance the readability of your content.

Videos for Module 8: HTML Lists

There are no videos yet this term for this Module. Check back soon!