Avoiding div soup

Written by That Web Guy on 5th June 2009. 8 comments

Avoiding div soup
  • Avant Innovations
  • Advertise Here

When I first made the transition from tables to divs for layout (yeah, I was that guy once upon a time!), I clearly remember being frustrated at how difficult it was to achieve my goals without over using the div tag. My main problem aside from still finding my CSS feet was I had completely ignored what the div tag was intended for.

I was using <div> as if it were the holy grail of achieving all my design goals. These days most of us know better but I will concede that until HTML5 and CSS 3 are commonplace we sometimes have to use a div for the 'wrong' purpose. When I say 'wrong' purpose I mean there are times when we have to break the rules because of cross browser issues, and I'm OK with that for now.

Div, if you had forgotten, is short for division, and as the name implies is meant to be used to divide your mark-up into appropriate sections, such as the header, content, footer etc. Aside from those, your goal should be to reduce your reliance on div's as much as possible and make use of existing HTML tags.

The following part of this article will show some of the more common mistakes I've seen some designers do using divs (including yours truly) along with appropriate solutions.

The navigation

How it's sometimes done

<div class="nav">
<ul>
<li><a href="">First item</a>
<li><a href="">Second item</a>
<li><a href="">Third item</a>
</ul>
</div>

Div-less solution

<ul class="nav">
<li><a href="">First item</a>
<li><a href="">Second item</a>
<li><a href="">Third item</a>
</ul>

See what I did there? The only reason you contained it within a div in the first place was so you could position and style it. Why not position and style the <ul> instead?

The logo

How it's sometimes done

<div id="logo"><img src="mylogo.jpg" alt="CompuGlobalHyperMegaNet" /></div>

Div-less solution

<h1 id="logo">CompuGlobalHyperMegaNet</h1>

...with some CSS...

#logo {
width:200px;
height:100px;
background-image:url(mylogo.jpg);
text-indent:-9999em;
}

The width and height are required to be the size of mylogo.jpg otherwise the div will simply collapse on itself and you won't see the background image. The text-indent will move the actual text off-screen so it's not visible on top of your logo.

The headings

How it's sometimes done

<div class="larger_header">Welcome to CompuGlobalHyperMegaNet</div>

Div-less solution

<h1>Welcome to CompuGlobalHyperMegaNet</h1>

That particular one is rare but I've seen it enough times in the past to justify mentioning it here.

Forms

How it's sometimes done

<div id="theform">
<div>Personal Information</div>
<form action="send.php" method="post">
<div>Name:</div><input type="text" id="name">
<div>Age:</div><input type="text" id="age">
<div><input type="submit" value="Submit" name="Submit" /></div>
</form>
</div>

Div-less solution

<form action="send.php" method="post">
<legend>Personal Information</legend>
<label for="name">Name: </label> <input type="text" id="name">
<label for="age">Age: </label> <input type="text" id="age">
<input type="submit" value="Submit" name="Submit" />
</form>

Conclusion

There are plenty of HTML tags at your disposal and the chances are you can accomplish your design goal by taking advantage of them. Try to limit divs to what they were intended for but as mentioned earlier there may be times when you just have to use an empty div. Those days are numbered but for now we can only do the best we can.

Is this worth sharing?

That Web Guy

About That Web Guy

That Web Guy (Mikey to his friends) is a veteran web designer based in Perth, Western Australia, and currently Design Director at Perth Web Design. When he's not XHTML'ing or messing around in Photoshop, Mikey can usually be found preaching web standards evangelism onto unsuspecting victims.

Feel free to send That Web Guy a message some time, follow him on Twitter, or make a donation.

Comments

kHa

kHa

man you don't believe how much time i'll save by just sending a link of this post instead of explain all over again to the confused minds...

Saturday 6th June 2009 | 11:20 AM Reply Comment URL Profile Back to top

kHa

kHa

did i forget to say thanks ;)

Saturday 6th June 2009 | 11:21 AM Reply Comment URL Profile Back to top

Harun Smrkovic

Harun Smrkovic

This gave me a good advice. I would always choose

Saturday 6th June 2009 | 02:53 PM Reply Comment URL Profile Back to top

Harun Smrkovic

Harun Smrkovic

OOK.. Something strange happened to my comment. Your system just trimmed out the last part of the message.

Anyway, I just wanted to say that I would always choose Transitional before Strict, and now you got me thinking.

Saturday 6th June 2009 | 07:35 PM Reply Comment URL Profile Back to top

That Web Guy

That Web Guy

Responding to this comment by Harun Smrkovic

Hi Harun. Glad I got you thinking :-) Like everything else in HTML there is a particular purpose to everything.

Not sure why your comment cut out - unless you tried to insert some code in which it would have been stripped.

Sunday 7th June 2009 | 07:45 AM Reply Comment URL Profile Back to top

Not a Member

Cameron Olivier

Hi!
This is a great post and gives some good examples and alternatives to simply replacing table-code with divs. :)

I have one or two questions though, as there's some things I mull over, and it's good to have another opinion :)

Navigation
I understand what you're saying, although I think a div, as a division, for me looks cleaner around the ul, if I have divs for header, content, etc, although I understand it's pretty redundant, unless needed as a wrapper, to allow for styling, which is sometimes also required.

The Logo
Why would a [p] be better than a [div]? A logo really isn't a paragraph of text either. I usually make it an H1, tbh. I see the need for a logical tag as opposed to a generic one though, just not sure the [p] tag is the best choice?

Headings
I fully agree!! [div] or [span] or even [p] for that matter is simply bad use of html. Headings should be defined as such. BTW, what do you use for slogans or buylines? I usually go with a [p] but again, it isn't a paragraph of text. A span maybe be better, but it's predominantly an inline tag, and so it feels strange having it float amongst block tags like H1 or UL (presuming logo and nav are in the header)

Forms
I agree with you here too :D - but what are your thoughts on keeping the label and input together? - often one needs a wrapper tag around the label and input, and although I tend to go with the [p] tag, as noted above, I feel it's not really the right tag for the job either. I've considered using a [div] for this, and on occassion I have - as in theory, it's less specific than the [p] tag. Also, ideally, the form should have a [fieldset] surrounding the logical divisions within the form.

Just wondering 'out loud' really, as it's things I've been pondering for a while, and as you brought it up, thought I'd mention it :)

Thursday 11th June 2009 | 08:41 AM Reply Comment URL Back to top

Not a Member

cameron olivier

one more thing.. your humanity test is occasional ambiguous . I entered 'clark' for superman's first name and was bounced. but maybe that's just me trying to too clever for my own good... some of these are hard man! haha

Thursday 11th June 2009 | 08:43 AM Reply Comment URL Back to top

That Web Guy

That Web Guy

Responding to this comment by cameron olivier

Thanks Cameron. That's a fair call. Somone once mentioned that "kal-el" should be included as well (Superman's real name) so I added it. I've added 'clark' (and 'clarke' for mis-spellers) now as well.

Friday 12th June 2009 | 07:34 PM Reply Comment URL Profile Back to top

FYI: You are currently not logged in. It's cool though - you can still comment. But only members get a profile page, access to the download section and they can pimp their own web sites. Feel free to register or Login now!

Your name:

Your comments:

Note: HTML tags are automatically stripped from comments.

Are you human?
Turing

Sorry, I have to ask. So what sort of animal is this? (Hint: you don't have to be perfectly specific)

Back to top

Login to That Web Guy Blog

Login

Not registered? | Forgot your Password? Cancel Login