Table Layouts vs Div Layouts and New Web-Design Concept (HTML 5,CSS 3)
Over the last several years, developers have moved from table-based website structures to div-based structures. Hey, that’s great. But wait! Do developers know the reasons for moving to div-based structures, and do they know how to? Often it seems that people are moving away from table hell only to wind up in div hell.
This article covers common problems with layout structure in web design. The first part goes through what table and div hells are, including lots of examples. The next section shows how to write cleaner and more readable code. The final part looks at what features await in future. Please join us on this journey from hell to heaven.
Image by lpinc.1988.
Table Hell
You’re in table hell when your website uses tables for design purposes. Tables generally increase the complexity of documents and make them more difficult to maintain. Also, they reduce a website’s flexibility in accommodating different media and design elements, and they limit a website’s functionality.
MAMA (Metadata Analysis and Mining Application) is a structural Web page search engine from Opera Software that crawls Web pages and returns results detailing page structures. If we look into MAMA’s key findings, we see that the average website has a table structure nested three levels deep. On the list of 10 most popular tags, table, td and tr are all there. The table element is found on over 80% of the pages whose URLs were crawled by MAMA.
Semantically speaking, the table tag is meant for listing tabular data. It is not optimized to build structure.
Ease of use
Using tables to build structure is quite intuitive. We see tabular data every day, and the concept is well known.
And the existence of table attributes makes for a rather flat learning curve because the developer doesn’t have to use a separate style sheet. With divs, the developer must use the style attribute or an external style sheet, because the div tag doesn’t have any attributes attached to it.
Also, tables don’t break when the content is too wide. Columns are not squeezed under other columns as they are in a div-based structure. This adds to the feeling that using tables is safe.
Maintainability
Table contains different tags: the table tag being the wrapper, tr for each row and td for each cell. The thead and tbody tags are not used for structural purposes because they add semantic meaning to the content. For readability, each tag is normally given its own line of code, with indention. With all of these tags for the table, several extra lines of code are added to content. The colspan androwspan attributes make the code even more complex, and any developer maintaining that page in future has to go through a lot of code to understand its structure.
- <table cellpadding=“0″ cellspacing=“0″ border=“0″>
- <tr>
- <td colspan=“3″ height=“120px”>…. td>
- tr>
- <tr>
- <td class=“menu” valign=“top”>…td>
- <td class=“content” valign=“top”>…td>
- <td class=“aSide” valign=“top”>…td>
- tr>
- <tr>
- <td colspan=“3″>…td>
- tr>
- table>
- <div id=“header”>…div>
- <div id=“menu”>…div>
- <div id=“content”>…div>
- <div id=“aSide”>…div>
- <div id=“footer”>…div>
As we see from the example, the table-based layout contains more code than the div-based version. Imagine now if this difference in size stays consistent as the code base grows (by a ratio as much as 2:1). In a div-based structure, it is also possible to skip the menu div and use an unordered list (ul) as a container instead.
Nested tables are code smell that a website is stuck in table hell. The number of lines of code is endless, and the complexity is overwhelming. Tables are far from clean code and don’t bring anything semantic to the content unless you’re dealing with actual tabular data. And if you’ve happened to inherit the maintenance of a website that has poor readability, it’s a nightmare. Nested tables are a poor substitution for semantically meaningful, block-level elements.
Another drawback to tables is that they make it harder to separate content from design. The border,width, cellpadding and cellspacing tags are used in about 90% of all websites that use tables, according to MAMA. This adds code to the HTML that should instead go in the style sheet.
Excess code slows down development and raises maintenance costs. There’s a limit to how many lines of code a programmer can produce per hour, and excess code is more complicated for others to understand. Developers may not even understand their own code after a while.
More lines of code mean larger file sizes, which mean longer download times. Developers should keep in mind new media, such as mobile devices, which usually have low bandwidth. Websites will have to support media other than traditional monitors in future, and bad code limits the possibilities. A large code base has more bugs than a small one. Developers tend to produce a certain number of bugs per line of code. Because tables increase the code base, such structures likely contain more bugs than layouts with less code lines.
Websites with tags that properly describe content are easily identifiable. Disabling the style sheet oncraigslist shows us a table-based layout. Headlines are marked only with the bold tag, and all links could easily be put in individual lists.
Flexibility with media
In an ideal world, the same markup would be used for printers, mobile devices, screens and other media. Using tables for structure provides less flexibility in moving columns around and hiding entire columns. Your user may want to put the side column at the bottom or view a printer-friendly version. With tables, this would require a separate page for each media, which means extra costs during development and higher maintenance costs compared to a div-based website that separates content and design.
Further reading:
- MAMA: The Average Web Page
Facts about the average website.
- MAMA: Key findings
MAMA’s most interesting findings.
- Google Web Authoring Stats
An analysis of Web authoring statistics from 2005.
- MAMA: Usage of tables
Usage of tables.
- Why tables for layout is stupid
Why tables for layout is stupid. A classic.
- CSS layouts vs. Table layouts
Alternate browsers and accessibility issues.
- Table layouts: pros and cons
Advantages and disadvantages of tables for layout.
Websites currently in table hell:
- http://www.artsforeveryone.com/
745 nested tables!
- http://www.amazon.com/
- http://www.walmart.com/
- http://www.craigslist.org/
Div Hell
Websites in div hell have more div tags than are necessary. This is also known as “divitis.”
The div tag is a block-level element that defines a section within a document. Divs are thus suitable for building the structure of Web pages. The div tag is also used to describe content that cannot be properly described by other more semantic tags. When developers do not understand the semantic meaning of other block-level elements, they often add more div tags than are needed.
Some programmers mistakenly believe that using a lot of divs is just fine. Maybe this photo bymy_ladyhawk makes a good point. Is overuse of divs the best of the worst?
Ease of use
Div-based structures have a much steeper learning curve than table-based structures. The developer must know CSS and understand the difference between block-level elements and inline elements, when to use floats and when to use absolute positioning and how to solve browser bugs.
The div element isn’t visual like the table element. Everyone knows what a table looks like, but divs are not as obvious. The good thing about a div, though, is that it’s only one element. It’s not wrapped in a parent element the way td tags are in tables. The container, then, is more flexible and doesn’t have the limitations of its parent tag.
Using a div for structure can make a page more fragile when content is pushing the div to its limit. It can also force columns to fall under each other. But this is usually only true for older browsers (particularly IE6); newer browsers make content flow to the next column.
Dealing with browser bugs can be a little tricky at first, but with experience developers can identify and fix them. Browser support for W3C standards is getting better and better. With the growing popularity of Firefox and Safari and the introduction of Google Chrome, we are seeing a big fight over market share, which inevitably makes for better browsers.
Continue Reading | Source: Smashing Magazine
Related posts:
- How to Create Professional Website Layouts – Website Development Tutorial
- Convert the Layout of Website Into HTML Template – Website Development Tutorial
- How to Plan Your Website and Design Its Layout Accordingly – Website Development Tutorial
- Sexy Facebook Layouts For Your Profile
- A Beginners Guide To Develop Web Applications in PHP





