Why Using Semantic Elements like is Important (And Better than <div>)

Gili Yaniv
5 min readOct 12, 2024

--

In the early days of web development, developers often relied heavily on <div> tags for virtually every layout and structural purpose. However, HTML has evolved to provide us with a rich set of semantic elements, which are designed to give meaning and context to content. Despite this, many developers still default to using <div> elements for things like text, where more appropriate alternatives like <span> and <p> exist.

In this blog post, we’ll explore why using semantic elements like <span> and <p> tags for text elements is crucial, and why they’re better than using <div> tags for text-related content. The examples are mostly for text elements but the same applies for all semantic elements,

What Are Semantic Elements?

Semantic elements are HTML tags that clearly describe their meaning both to the browser and to developers. For instance:
- <p> stands for paragraph, which means a block of text.
- <span> is used for inline text or small parts of text that need to be styled.
- <header> represents a container for introductory content or navigational links.
- <footer> marks a footer for the nearest sectioning content.

The beauty of semantic elements is that they convey information about the role and structure of the content to browsers, developers, and assistive technologies, like screen readers.

On the other hand, the <div> tag is a non-semantic element. It simply defines a division or section in a document and doesn’t provide any information about the content inside it.

Why You Should Use <p> and <span> Instead of <div> for Text

1. Accessibility and Screen Readers
Semantic elements play a significant role in making web content accessible. Screen readers rely heavily on these tags to identify and interpret the structure of a webpage.

- When you use a <p> tag, screen readers understand it as a block of text, allowing users to navigate easily between paragraphs. Using a <div> instead of <p> may confuse screen readers, causing them to announce content in ways that aren’t expected or logical for visually impaired users.

- Similarly, the <span> tag is ideal for small pieces of inline content, like applying style to part of a sentence, while <div> creates a block-level element. Misusing <div> where a <span> would suffice can lead to a confusing flow for assistive technology.

2. Improved SEO (Search Engine Optimization)
Search engines like Google prioritize well-structured content. Using semantic HTML elements helps search engines better understand the content on your page, which can improve your site’s SEO performance.

- <p> elements signify that the enclosed content is a paragraph of text, which search engines recognize and index properly. If you use <div> for paragraphs, it doesn’t provide search engines with any meaningful clues about the importance or relevance of the content.

- By correctly marking up text with <span> for inline portions or <p> for paragraphs, you help search engines categorize and rank your content more effectively, giving your website a potential edge in search results.

3. Better Readability for Developers
When you revisit or share your code with other developers, using semantic elements makes your code much more readable and understandable. Proper use of tags like <p> and <span> helps developers quickly grasp the purpose of different elements.

- A <div> for everything forces developers to dive deeper into CSS or other content to understand its purpose. Semantic elements, on the other hand, are self-descriptive, making it easier to maintain and extend your codebase.

For example, it’s instantly clear that <p> represents a paragraph, whereas a <div> requires more investigation.

4. Consistent Default Styling
Different HTML elements come with their own default styling, which is useful for consistency in displaying content.

- <p> has built-in margins and line breaks before and after, making it ideal for longer text blocks.
- <span>, being an inline element, won’t disrupt the flow of text.

When you use a <div> for text, you’ll need to manually style it to add spacing or inline behavior, which leads to extra CSS work and can result in inconsistencies if overlooked.

5. Logical Content Structure
Using semantic tags contributes to a logically structured and well-organized document. This is essential not only for SEO and accessibility but also for progressive enhancement and graceful degradation.

- For instance, the use of <p> for paragraphs helps structure text content in a meaningful way. In contrast, a <div> doesn’t provide this structure and can lead to a messy document flow.

6. Best Practices and Modern Standards
HTML has come a long way, and using semantic elements is considered a best practice for building modern, maintainable websites. By using the right tags for the right purposes, you align your project with web standards and best practices that are likely to be future-proof.

While <div> still has its place for generic containers and layout purposes, misusing it for text elements goes against these modern standards.

When Should You Use <div>?

While <span> and <p> are great for handling textual content, <div> isn’t bad by itself; it just has a different use case. You should use <div> for generic containers, layout, or when wrapping non-textual elements that don’t fit other semantic tags.

For example:
- <div> can be useful for structuring sections of a page, such as grids or flexbox containers.
- It’s perfect for grouping other elements for styling purposes.

However, when it comes to pure text, especially inline content or paragraphs, the use of semantic elements like `<p>` and `<span>` is the correct choice.

Conclusion

Using semantic elements like <span> and <p> instead of <div> for text content is not only a best practice but also brings a host of benefits, including better accessibility, improved SEO, cleaner and more readable code, and a well-structured document. While <div> serves an important role in layout and container purposes, text elements should always be marked up with appropriate semantic tags.

By adopting semantic HTML in your web projects, you’re not only enhancing the user experience but also ensuring that your code is future-proof and easy to maintain. In the modern web, the right tag matters!

Follow me on Twitter, Medium, and Linkedin to read more!

--

--

No responses yet