0

I've heard that Google uses tables for layout because tables load faster. I find it hard to believe that tables could decrease page load time when compared to a CSS based layout with more minimal/semantic markup.

The heftier ones markup is the slower the load time right? Could someone please explain, and/or provide an example of how tables load faster?

Community
  • 1
  • 1
Web_Designer
  • 72,308
  • 93
  • 206
  • 262
  • 2
    I don't see any hard data on that plus that question is three years old. – j08691 Mar 08 '12 at 23:21
  • "Semantic" does not always mean "smaller". Just saying. – cHao Mar 08 '12 at 23:21
  • Oh...and if you look at http://google.com, there's one table in the whole page -- and it's empty. – cHao Mar 08 '12 at 23:24
  • I re-coded a single page from table layout to CSS layout once, and the total code size didn’t change. Depending on the page, it may not be until you’ve got several pages (or complicated layouts requiring many tables) that you get any page weight benefits from CSS as compared to tables. – Paul D. Waite Mar 08 '12 at 23:25
  • @cHao - I believe that table is used if you are logged in to google plus – Travis J Mar 08 '12 at 23:25
  • If you’re curious about the Google home page, why not grab the code, rewrite it to look the same without using tables, and see if you can get it any smaller? – Paul D. Waite Mar 08 '12 at 23:26
  • @Web_Designer - In response to heftier, the links states `impressively large`, which could mean a lot of fast rendering code. A small animal can be hefty just like a large airplane - note that your use of the word `heftier` is used as a "comparative" making it subject to opinion. – Travis J Mar 08 '12 at 23:28
  • From the top of my head, without having performed any measures at all, I would have guessed that using tables for layout would require more markup than when using div-elements with CSS, thus even if barely measurable the tables would increase the page load time rather than decrease it, due to increased file size. Independently, this is probably a low priority optimization compared to bundling/minimizing CSS and JavaScript, using image sprites and so forth. – Christofer Eliasson Mar 08 '12 at 23:29
  • 2
    This is sort of a loaded question. Where is the proof that table layouts load faster? I've seen plenty of websites championing the div/css cause and claiming that div/css loads faster. For instance - http://www.twoninemedia.com/blog/index.cfm/2007/4/19/TabledLayoutsvsCSSLayouts – Isaac Fife Mar 09 '12 at 01:23
  • 1
    @IsaacFife Upticked your comment as it is evidence based but OTOH where is the evidence to the contrary? One Web site that measured download time for one table renderered two ways. Doesn't go beyond anecdotal status on its own. – user207421 Mar 09 '12 at 05:20

3 Answers3

1

With current browsers there is not really a performance reason to use table instead of div tags.

For a long time HTML tables were noticeably faster then div+css tables, especial in IE6 with larger tables and large numbers of floating div tags. That is not to say a few floating divs were noticeable, usually pages had hundreds of floating divs before the difference was easily noted, but it was noticeable. In fact I remember one page which I use to frequent where the day they switch from tables to floating divs was quite noticeable. Of course it was a page I went to regularly (so I knew exactly how long it took to load) and had a listing of all the web safe colors and their values (so there were MANY floating divs). At the time this was explained by the fact that tables had custom code which ran faster then the equivalent div tags (and was easier to keep track of). Once floating div tags were fully implemented the difference was no longer noticeable (tables were slowed down by their new CSS implementations).

That being said, there are reasons to use tables instead of div+css.

  • I don't think the W3C standards intended to replace tables with div+css. Doing so goes against the sepreation of structure from style usually advocated by their standards. Also, HTML5 introduces new tags to replace commonly used div+css like "article", "aside", "footer", etc. If div+css was the way to go, why would the writers of the standers be discouraging its use?
  • Tables are more predictable when selecting. Have you ever started selecting some text in a table only to see the first column selected and everything under the table selected before being able to select the second column? I have, it REALLY bugs (note: I have not checked this in the last couple of months, but I expect it is still a problem with poorly written sites).
  • Tables are more recognizable to non-browsers. For example, copy/past into Excel. Tables always copy correctly, but div+css usually comes in with each cell in column A, or sometimes with the whole table in one cell.
  • Tables usually do the same thing a div+css tag does, but with more of the style predefined, resulting in simpler CSS.

I am not saying one is right or wrong in particular situation.
I will say it is best practice to use the most appropriate tag for the job.

It has been noted by some that

The W3C HTML5 standard clearly and explicitly states that tables are to be used for tabular data only.

This is of course, incorrect, but only due to the use of the word "only". The link from @Isaac Fife states

Authors must not use elements, attributes, or attribute values for purposes other than their appropriate intended semantic purpose, as doing so prevents software from correctly processing the page. For example, the following document is non-conforming, despite being syntactically correct: [HTML for a page containing a table made of two rows with one cell each.]...because the data placed in the cells is clearly not tabular data ..." in reference to why a particular use of the table tag is wrong

While this is not the definitive rule, it is a good example of why improper use of tags can be a problem.

A more definitive answer can be found on the W3.org page for Tables (HTML4) which states:

The HTML table model allows authors to arrange data -- text, preformatted text, images, links, forms, form fields, other tables, etc. -- into rows and columns of cells.

...

Tables should not be used purely as a means to layout document content as this may present problems when rendering to non-visual media. Additionally, when used with graphics, these tables may force users to scroll horizontally to view a table designed on a system with a larger display. To minimize these problems, authors should use style sheets to control layout rather than tables.

For those wanting a little bit more (though not yet final) the W3.org page for Tabular data (HTML5) it states (NOTE: HTML5 is not standardized at the time of this writing, which is why their is the red box on the bottom of the page.):

The table element represents data with more than one dimension, in the form of a table.

...

Tables should not be used as layout aids. Historically, many Web authors have tables in HTML as a way to control their page layout making it difficult to extract tabular data from such documents. In particular, users of accessibility tools, like screen readers, are likely to find it very difficult to navigate pages with tables used for layout. If a table is to be used for layout it must be marked with the attribute role="presentation" for a user agent to properly represent the table to an assistive technology and to properly convey the intent of the author to tools that wish to extract tabular data from the document.

Note: There are a variety of alternatives to using HTML tables for layout, primarily using CSS positioning and the CSS table model.

From this it becomes very clear the the standards writes want to discourage the use of tables for page layout. That being said, they do not say using tables for page layout is strictly prohibited, as the role attribute can be used to indicate the table is filling a "presentation" role.

From the standards it seems best practice is to use tables for presenting tabular data, not for layout. A fairly good rule of thumb for when to use tables can be found at about.com

NOTE: Please remember that a table tag does represent structure or layout of information on a page. The standards are not trying to remove the use of tables completely, but to discourage the use of tables for formatting a page. It is ok to place the information you pulled from a database into a table, but do not structure you page in a table with the top header in one row, the left options bar and main page contents in two cells on the second row, and the footer on the bottom row.

An example of proper table use might be an ASCII look up chart or a web safe color chart.

An example of improper table use can be found at w3schools.com

Update 2012/03/12: added additional information and valid standards references to clarify when to use tables.

Community
  • 1
  • 1
Trisped
  • 5,705
  • 2
  • 45
  • 58
  • The W3C HTML5 standard clearly and explicitly states that tables are to be used for tabular data only. See section 3.2.1. http://dev.w3.org/html5/spec/Overview.html#semantics-0 – Isaac Fife Mar 09 '12 at 01:08
  • 2
    Also, `div` was meant to be used when there's not a semantically appropriate block tag. It's not meant to be used the way a lot of people use it (for everyfreakingthing in the page). If there ends up being a lot of ` – cHao Mar 09 '12 at 14:43
  • @cHao I am not sure why you reposted the content from my post, I already stated that HTML 5 added new tags to replace commonly used div tags and linked to an article which states " The W3C mined billions of existing webpages to discover the IDs and class names that developers were already using. Once they threw out div1, div2, etc., they came up with a list of rich descriptive elements that were already being used, and made those the standards." Or was my answer too long to read? – Trisped Mar 13 '12 at 00:14
  • I don't read linked articles half the time. If the answer's not here, it doesn't count. :) But the majority of the article (when i posted that comment) came off as a defense of table layouts. Then you're like "I don't think the W3C standards intended to replace tables with div+css", which when posted right under "there are reasons to use tables instead...". Basically, the wording made it look like something else. – cHao Mar 13 '12 at 02:08
  • @cHao If you read the whole bullet it also says "Also, HTML5 introduces new tags to replace commonly used div+css like "article", "aside", "footer", etc. If div+css was the way to go, why would the writers of the standers be discouraging its use?" As far as defending tables, yes I am defending tables. They are tags that should be used where appropriate, not replaced with div+css in all cases. – Trisped Mar 13 '12 at 17:41
  • When i read the whole bullet, div+css looks like a strawman. That wasn't in the question -- and it seems your only purpose for bringing it up is "see? Even W3C thinks divs suck!" Which, while arguably true, does not bother to address the question. You present the argument as divs vs tables, and the semantic argument -- that is, *the question itself* -- gets all but tossed aside in the conflict. I'd like to give you some credit and assume that wasn't intentional, but it happened nonetheless. – cHao Mar 13 '12 at 18:07
0

I have not seen any data on tabular layouts vs. divs+css with regard to page performance; and honestly I believe that whatever difference there is would be a micro-optimization not worth your time unless you were a major player like Google. That being said, there are a couple scenarios where I can conceive of tabular layouts having better performance, and it has to do with the fact it doesn't require CSS for layout, and thus can reduce overall bandwidth consumption (this would only work if the removed bulk of css positioning code outweighs the added bulk required for table layouts). Additionally, if you can eliminate CSS altogether (and embed whatever's left) you can eliminate a single GET request (again, a micro-optimization that only someone like Google might care about).

However, just like the answers to your linked question from 2009, all I can do is speculate as to the real reason why Google used tabular layouts. Performance may not have even been a factor in their decision! You have to remember that back in 2009 the movement to divs+css was still in progress, there were a significant number of major sites that had not yet adapted, and not everyone was fully convinced that it was worth the effort due to numerous bugs and compatibility issues.

For all we know, Google's choice to use tables might have solely to preserve compatibility with ancient browsers which had a lot of problems with CSS (for instance IE5/6). That, not performance, seems much more likely to me. Remember, I never established empirically that tables are faster, just theoretically in certain situations.

Moses
  • 9,033
  • 5
  • 44
  • 67
  • In 2009 the move to div+CSS was still in progress? I thought CSS had been the preferred layout method over tables for like 10 years at that point. – Chuck Mar 09 '12 at 00:02
  • @Chuck had it been the preferred layout it is today, you wouldn't of heard so many people claiming CSS will die because IE8 would be introducing `display:table`. People were so attached to the idea of table-based layouts, SitePoint made a book lauding the use of `display:table` to achieve table-like structure without using html tables (which is arguably worse than using html tables to begin with)! – Moses Mar 09 '12 at 00:28
0

Do not read it as

Why a fewer lines of code in CSS is slower than the large makeup in HTML such as table?

What is important is how a one line CSS can create a lot of work for browser than 10 lines of HTML can create very little work for the browser. So the work generated by the piece of code is important rather than the lengh of the code that is create the work.

As far loading, the loading time is about the same weather you use CSS or html tags. The difference is if you use external CSS, this will create another hit on the server (could cause perf issue on a very busy server like google). Plus we have seen often here on stackoverflow, the contents are loaded but the CSS styling is not. This type of issue can be avoided if inline css is used.

So I think it matters only on very busy server where optimization is name of the game. For our general use, separate CSS file is fine and more desirable since it enhances code readability and maintainability and there is no need to embedded everything in HTML.

TheTechGuy
  • 16,560
  • 16
  • 115
  • 136