▲Wirig Diagram Hot
▲ ▲ ▼

Understanding OL and LI in HTML

By

In the realm of HTML, two related elements that often cause confusion are ol and li. They are integral components of lists, serving distinct but complementary roles. Let's delve into the world of HTML lists to understand these elements better.

The HTML ol tag represents an ordered list, where each item has a default numeric value. Ordered lists are typically used for steps in a process, rankings, or timed events. On the other hand, the li tag, short for 'list item,' is used to create individual items within a list, be it ordered or unordered.

Ordered Lists (ol)

Ordered lists, denoted by the ol tag, use numbers or custom markers to denote the order of items. The type attribute can be used to set the list marker type to '1' (for decimal numbers), 'A', 'a', 'I', 'i' (for lowercase and uppercase alphabets), and 'i' (for Roman numerals).

HTML Lists: <ul> vs <ol> – When to Use Which? 📝
HTML Lists: <ul> vs <ol> – When to Use Which? 📝
a screenshot of a computer screen with some type of text in the bottom right corner
a screenshot of a computer screen with some type of text in the bottom right corner
Learn to create links in HTML.
Learn to create links in HTML.

By default, HTML ordered lists start numbering from 1, and each subsequent item is incremented by 1. You can also set a starting number using the start attribute, allowing you to inject a list at any point in an ordered sequence.

Custom Markers in Ordered Lists

The type attribute allows for custom markers in ordered lists. For instance, setting type="A" will change the list markers to uppercase alphabets, while type="i" uses Roman numerals. This can be quite useful for avoiding repetition or adding a unique touch to your lists.

HTML Full Notes for Beginners 💻
HTML Full Notes for Beginners 💻
How to add link on image in HTML? | Website Development
How to add link on image in HTML? | Website Development
🔗 HTML Links | Learn Hyperlinks in HTML (Lesson 5)
🔗 HTML Links | Learn Hyperlinks in HTML (Lesson 5)

Here's an example of an ordered list with custom markers: ```html

  1. First item
  2. Second item
  3. Third item
``` You'll notice the list markers are now Roman numerals (I, II, III).

Starting Ordered Lists at Custom Positions

The start attribute in the ol tag lets you specify where the ordered list should start. This can be particularly useful when you're creating lists that don't start at the beginning of a process or event.

What Is HTML and How It Works (Beginner’s Guide for 2026) | Learn HTML Basics
What Is HTML and How It Works (Beginner’s Guide for 2026) | Learn HTML Basics
html
html
Lesson 7: HTML Lists (Ordered & Unordered Lists) 📚
Lesson 7: HTML Lists (Ordered & Unordered Lists) 📚
a screenshot of some type of web page with the text'html links '
a screenshot of some type of web page with the text'html links '
💻 Learn HTML for Beginners | Free Websites, HTML Basics & Coding Guide
💻 Learn HTML for Beginners | Free Websites, HTML Basics & Coding Guide
🚀 Day 10 of Learning HTML — Mastering HTML Tables ---
🚀 Day 10 of Learning HTML — Mastering HTML Tables ---

For example, consider a recipe that should start with Step 4: ```html

  1. Mix the ingredients...
  2. Bake for 30 minutes...
  3. Let it cool before serving...
``` In this case, the list will display as '4. Mix the ingredients...', '5. Bake for 30 minutes...', and '6. Let it cool before serving...'.

List Items (li)

List items in HTML are created using the li tag. They are enclosed within the ol or ul tags (for unordered lists) to form a complete list. Each li tag creates one item in the list, typically displayed as a new line.

an image of what are attributes in html?
an image of what are attributes in html?
What Is HTML? 💻 HTML for Beginners | Lesson 01
What Is HTML? 💻 HTML for Beginners | Lesson 01

The content of the li tag can be anything from simple text to complex HTML elements, including images, links, or even other lists. This flexibility makes the li tag a versatile part of web design.

Including Sub-lists with li

One powerful feature of the li tag is its ability to contain other lists, creating nested or sub-lists. This can be achieved by placing another ol or ul tag (and its associated li tags) inside an existing list item. This is especially useful for creating outlines or hierarchical lists.

Here's an example of a nested list: ```html

  • Fruits
    • Apples
    • Bananas
  • Vegetables
    • Carrots
    • Broccoli
``` In this example, 'Fruits' and 'Vegetables' are the main categories, each containing a sub-list of items.

Styling List Items with li

The li tag offers several attributes for styling list items, such as value to set the explicit list item value (only applicable to ordered lists), and style for inline CSS styling. Additionally, CSS can be used to customize the appearance of list items globally or individually.

For example, let's add a custom style to the first list item in an ordered list: ```html

  1. First item with a custom style
  2. Second item (no custom style)
``` In this case, 'First item with a custom style' will be displayed in blue, while the following items retain their default style.

Understanding the ol and li tags is crucial for creating engaging and well-structured content on the web. Whether you're listing steps for a recipe, outlining a report, or displaying product categories, using ordered and list item tags efficiently will enhance your website's presentation and usability. Happy coding!

💬 Comments ↗ Share ★ Save
AD