When to use DIV tags and when not to

A common question in #web on Freenode IRC, goes something like this:

“Should I use <div>‘s or tables for my layout?”

The answer is, neither.

What most people call “using <div>’s” is actually, CSS-P – CSS Positioning. This is the correct way to markup a page for styling using CSS, rather than putting all the display information into the page using tables. You should always use CSS-P for styling your website, this does not mean that you should never use tables, but that you should only use tables for tabular data.

You should also never use a <div> when another tag will do. Think about the following examples:

Using the following CSS, the following two HTML examples display the same:

So why – you ask – does it matter which I use? Well, just looking at the two examples, the second one obviously denotes the text a 1st level header. The top one doesn’t give any information about the text contained in it.

This doesn’t mean you should never use a <div> there are situations where you are styling something where you have no content (say, a drop-shadow on a paragraph which itself is marked up using <p> and has a different style).

I know I haven’t gone into much depth here, but thats the long and short of it as I see it. I hope that helps someone.

– Davey


Warning: file_get_contents(index.php): failed to open stream: No such file or directory in /nfs/c03/h02/mnt/170398/domains/daveyshafik.com/html/wp-includes/plugin.php on line 441