Explain Codes LogoExplain Codes Logo

Why should I use li instead of div?

html
semantic-html
accessibility
seo
Alex KataevbyAlex Kataev·Oct 20, 2024
TLDR

Use <li> instead of <div> to define list items for clear semantics and improved SEO dynamics. For accessibility tools, li implies a list item while <div> is general-purpose, lacking inherent meaning.

<!-- Mom always said life was like a box of chocolates. But she never told me coding was like a list of <li>s. -->
<ul>
  <li>Item 1</li>  <!-- Like a peanut butter cup. Sweet and a bit nutty. -->
  <li>Item 2</li>  <!-- Perhaps a caramel swirl. Smooth and complex. -->
  <li>Item 3</li>  <!-- And of course, the mystery piece! -->
</ul>

<li> provides self-explanatory keys to your "list contents", while <div> lacks such tangibility and can add complexity.

Semantic HTML: More than just tags

Semantic HTML, at its core, is how we deliver meaning and structure to web content. Instead of using arbitrary containers, items of a list, whether ordered or unordered, are best marked using <li> elements. This practice ensures clarity and streamlines functionality. These tags inform the browser, accessibility tools, and end-user about the content's hierarchy and relation.

Accessibility: The key to a friendly web

Accessibility tools, like screen readers, thrive with semantic tags for easier content interpretation. Using ul and li tags for list-related content improves the user experience by helping with accessibility navigation.

SEO: Playing nice with search engines

When it comes to SEO, structured content trumps beautiful chaos. Hierarchical and useful markup informs search engines about your content values. This practice not only increases your search relevance but also maintains readability in various user settings—providing a universal experience.

<div> tags: Not entirely the villain

While <div> tags are versatile, they're best for general content containers without implying a specific semantic structure. You can use <div> to segregate different content types while staying away from list-like structures.

Avoid confusion, embrace clarity

Using <div> for non-div content can lead to confusion, translating to a poor user experience. Moreover, putting <ul> and <li> tags where they don't semantically belong can misrepresent your content—making Mr. Google unhappy!

When to opt for <div>

For diverse content types, choose <div> where no conventional list exists. Ensuring semantic correctness prevents misinformation and gives accessible navigation for the users.

Stylistic flourishes

When adding style to your lists, remember it's always CSS's game. Keep the HTML structure semantic, carry the look and feel with CSS.