Is Using Lots of div Tags Really That Bad?

Share this article

Our latest book — the one with the controversial title — has caused much debate and more knee-jerk reactions than a bucket of frogs at a barn dance. A comment I’ve seen posted frequently is that “replacing table tags with divs that display as tables is no different; you may as well just use tables.” The argument is interesting because it sounds like the commenter is implying that the only reason not to use table tags is the number of tags involved.

I’m going to stick my neck out and say yes, even if you just replace all your table, tr, and td tags with div tags that display like those table elements, it’s better than using HTML tables for layout.

No one is negatively affected by the overuse of structural div tags. The same can’t be said for the use of HTML tables for layout.

A web page that uses nested tables for layout will be difficult to print, difficult to view on a mobile device, and difficult to navigate for users of screen readers. A screen reader application will often stumble over nested HTML tables, announcing something like “table with 5 rows and 2 columns” as it enters the table content and “table end” as it leaves. The same web page using nested div elements can be styled to stack neatly for print and viewing on a mobile device, and screen readers ignore div elements.

By using HTML tables you are implying that the contents of each cell relate to the contents of other cells in 2 dimensions: in the same row and in the same column. On the other hand a div implies no such relation with other div elements. It’s simply content scaffolding, and no meaning is derived from their use.

OK, blindly replacing all table-related tags with div elements may not be the correct approach to the problem, but to say that it is “just as bad” as using HTML tables for layout is wrong. HTML 4.01 defines the div as a “generic language/style container” or more specifically:

The DIV and SPAN elements, in conjunction with the id and class attributes, offer a generic mechanism for adding structure to documents. These elements define content to be inline (SPAN) or block-level (DIV) but impose no other presentational idioms on the content. Thus, authors may use these elements in conjunction with style sheets, the lang attribute, etc., to tailor HTML to their own needs and tastes.

So we can use div elements to define content blocks; sounds like using nested div elements for structuring content is perfectly valid. As long as you don’t use div elements in place of more appropriate ones like headings, paragraphs, blockquotes, lists, and of course, tables for tabular data, then you’re doing fine.

The approach you take when creating a layout using HTML table elements is to first lock-in the layout in HTML before you style your web page. This is wrong and flies in the face of established best practice: separate your presentation from your content.

This is completely different to the CSS layout approach, even when using a lot of div elements. For example, I could wrap all of the content that represents site branding in a div tag, and all of the content that represents additional site information in a another div tag. The fact that, in CSS, I may apply a style that places the site branding at the top of the web page and the additional site information at the bottom, is not important yet. At the layout stage I might do that, or I may use the CSS table-related display values to make them into columns or rows. The markup can be styled in multiple ways because the approach does not lock-in the layout in the HTML markup.

Frequently Asked Questions (FAQs) about Div Tags in HTML

What is the main purpose of using div tags in HTML?

The div tag in HTML is a container unit that encapsulates other page elements and divides the HTML document into sections. Web developers use div tags to group together HTML elements and apply CSS styles to many elements at once. It’s a way of organizing content and providing structure to your web layout.

Are there any alternatives to using div tags?

Yes, there are alternatives to using div tags. HTML5 introduced new semantic elements that can be used instead of div tags, such as

,
,
,
,

How can I style a div tag using CSS?

You can style a div tag in CSS by using the div tag as a selector. For example, if you want to set the background color of a div to red, you would write the following code:

div {
background-color: red;
}

Can I nest div tags within each other?

Yes, you can nest div tags within each other. This is often done to create complex layouts. When you nest a div, it becomes a child of the parent div. The child div can then be styled independently of the parent.

Is it bad to use too many div tags?

Using too many div tags can make your HTML document cluttered and difficult to read. It can also affect the performance of your website, as each div tag adds to the number of elements the browser has to process. It’s best to use div tags sparingly and only when necessary.

How does a div tag differ from a span tag?

The main difference between a div tag and a span tag is how they handle content. A div tag creates a block-level element, which means it will start on a new line and take up the full width available. A span tag, on the other hand, creates an inline element that only takes up as much width as necessary.

Can div tags affect SEO?

While div tags themselves do not directly affect SEO, the way they are used can. If div tags are used to create a clean, well-structured layout, it can make the content more accessible and readable to search engine bots, potentially improving SEO.

What is a class or id in a div tag?

A class or id in a div tag is used to identify that specific div. This can be useful for applying CSS styles or JavaScript functions. An id is unique and can only be used once per page, while a class can be used multiple times.

Can I use div tags in HTML emails?

Yes, you can use div tags in HTML emails. However, some email clients do not support all CSS properties on div tags, so it’s best to test your email in multiple clients to ensure it displays correctly.

How can I center content within a div tag?

To center content within a div tag, you can use CSS. If you’re centering text, you can use the text-align property. If you’re centering a block-level element, you can use the margin property. Here’s an example:

div {
text-align: center;
}

or

div {
margin: auto;
}

Andrew TetlawAndrew Tetlaw
View Author

iOS Developer, sometimes web developer and Technical Editor.

Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week