Explain Codes LogoExplain Codes Logo

Should ol/ul be inside <p> or outside?

html
best-practices
responsive-design
accessibility
Alex KataevbyAlex Kataev·Jan 9, 2025
TLDR

No-go for nesting <ol> or <ul> in <p>. They're like bananas and peanut butter - both good but don't always mix.

<p>Your paragraph goes here.</p> <ul> <li>Cool list item</li> <li>Another cooler list item</li> </ul> <p>And if you really want, another paragraph here.</p>

Yup, just like that! Keeps the accessibility and semantics all neat and tidy.

Element categories in HTML

In the HTML world, meetup groups are sorted into phrasing content (think sentences and phrases) and flow content (like ordered and unordered lists). <p> tags hang out in the phrasing content jamboree, and <ol>/<ul> enjoy company in the flow content gathering.

To dive a little deeper, <p> is for content like <span> and <strong>, while <ol>/<ul> are multi-item holding areas wrapped in <li> tags.

Browsers tend to see things this way too - stick a block (flow content) in a paragraph (phrasing content), and they'll give you the cold shoulder (or in their language - auto-close the tag).

Accessibility and HTML organization

HTML isn't just about getting things to work; it's also about being a good** content Samaritan**. When embarking on any HTML escapade, remember two faithful companions - organization and accessibility. Misguided content hierarchy can easily lead to UX nightmares.

In cases when you want to buddy-up <p> and <li>, let <p> be the visitor:

<ul> <li> A list item wanting to say more. <p>"I have so much space for activities!"</p> <!-- Step Brothers reference, anyone? --> </li> </ul>

HTML best practices: a quick reminder

  • Stay loyal to HTML standards: improved organization, readability, and maintainability will be your rewards.
  • Learn the browser's lingo: understanding how different browsers react to your HTML can save a bucketload of hair-pulling moments.
  • Treat content structure like your first-born: Good SEO, UX, and accessibility spring from it.

Visualization

A little aspiration to make this fun - our HTML architecture:

HTML document aspiration for architectural greatness: - `<p>`: Wall clocks 🕰️ - perfectly aligned, singular, eye-pleasing. - `ol/ul`: Bookshelves 📚 - orderly, accessible, good to hold a bunch of stuff.

Welcome Home 🏠:

Our humble home 🏠: Wall clock (🕰️): Bookshelf (📚⏺⏺⏺) - ✅ Every bookshelf hangs gloriously on our wall, NOT hidden behind a clock.

In a parallel universe where clocks hide bookshelves 🌌:

Our parallel universe home 🏚️: Ghost wall clock (⏲️ now you see me, now you don't 🔄): - ⛔️ Our poor bookshelf got sucked into a wall clock vortex!

Moral of the visualization - Lists sit pretty beside p elements, never inside.

HTML compliance: Dos and Don'ts

  1. Lists inside paragraphs: Big NO. Scrap it. You're messing up the matrix.
  2. Paragraphs inside list items: All good, carry on.
  3. Block vs inline: Remember the dance floor rule - give each other space.

Design considerations when using lists

Using lists really can spice up the design and layout of your content:

Surviving the list wilderness:

  • Bulleted lists: Ideal for bite-sized, key takeaways.
  • Numbered lists: When sequence matters more than Jack Sparrow's compass.
  • Nested lists: Your secret weapon to tackle complex information hierarchies.