Why is using DIVs or spans tags “better” than using a table layout? [duplicate]

风格不统一 提交于 2019-12-04 22:21:50

It's mostly about the semantics, I think. A <table> is built to represent tabular data, and using it to lay out elements violates that. Also, anything that can be done with <table> can be done just as easily or easier with CSS 99.9% of the time. It's not that much easier in the other cases, and like you say, it makes markup ugly and hard to follow. It also violates the separation of content, behaviour, and presentation fundamental to web development.

Best answer I can give is that a browser does not know how wide a table column has to be until it completely processes the page, which can result in a marked delay in the page visibly loading. By using a div the content will display immediately and if another element forces it to resize, it will, but there is no lag in things being displayed. So it's a better user experience.

Use tables when the need arises for them. When data needs to be neat, clean, and in a specific order that repeats. You should not avoid tables just for the sake of it.

Its just correct HTML semantics to use DIVs and SPANs for laying out a page and using TABs to represent tabular data, as in datasheets, user lists etc... Plus DIVs allow more flexibility with placement and styling

There are some advantages that you can't achieve using tables. Think about thumbnails - you can show as many as possible in one row, if there's no more space the others will break to the next line (on the fly).

See also:

http://www.csszengarden.com/ -> and choose a different design in the "choose a design" sidebar

Or a better approach for menus/navigation bars: http://css.maxdesign.com.au/listamatic/

One good reason is separation of concerns. HTML is for expressing information. CSS is for dealing with presentation and JavaScript is for behaviour. By seperating these concerns the content can be better interpreted as a the markup says - namely that a table is expressing tabular data, where as divs and spans are expressing a block of content and a span of text.

If you want some more good reasons:

  • Disabled users who use software such as a screen reader can understand you content better. Otherwise, your whole page could be interpreted as tabular data. You'll make it more accessible by using divs and spans.
  • Browsers may be able to render your page faster.
  • Making adjustments can be a lot easier.
  • It's easier to be consistent throughout your website.

People hate tables because it decreases SEO (Search Engine Optimization).

Mostly, it's an issue of semantics. A table is meant to display tabular data - data that needs to be laid out in a tabular format. Using it for anything else is like using Excel to format your Brochure - there might be the odd time it works, but really, use Word. Newer HTML5 elements, as well as some of the older HTML4 elements are just better semantically suited to the occasion - a division of content calls for a div, a section for a section, an article for an article, navigation for nav. Other elements beside tables also do allow for greater flexibility and can be even easier than tables when rightly used. Ultimately, it comes down to practicability. If it's somewhat tabular dataish in nature, use a table. But if it's columns, or navigation - use li or floated divs or sections.

EDIT: Compare the HTML required for two identical situations, btw: http://jsfiddle.net/gbXFG/3/ vs. http://jsfiddle.net/uJ2LG/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!