views:

241

answers:

10

Hey there

I sometimes find myself creating a div which serves no other purpose than to hold another element.

For example when creating a menu, I would create a div and assign it a background colour, position etc. Then within it I would create an unordered list.

I could surely assign all the properties I assigned to the DIV to the UL and not use a div at all.

Any ideas of what is best practice and reasons for it.

Thanks

Zenna

+4  A: 

DIVs can be useful for grouping semantically related elements. If you are simply wrapping a single element that is also a block element, then you are simply adding bytes to the file.

geowa4
and furthermore, grouping semantically is useful for style, right? (im a begginer)
djangofan
It also makes the DOM bigger. When you want to do javascript operations on it, less elements is better.
Dykam
it is useful for styling, definitely. but it still makes sense, when reading through the html, to see all of the comment blocks grouped together in a div.
geowa4
A: 

Div's are really a necessary evil without a more semantically rich set of tags.

Their purpose is to server as a generic container. Therefore, I suppose you could say they do their job well.

Zack
Hopefully we will see less use of divs in fashion when HTML 5 comes around.
shambleh
then canvas will become the new table.
geowa4
+1  A: 

I use often divs to keep child-padding/margins from ruining parent-width. But you need to be careful with this type of stuff - you could end up adding a bunch of nonsense.

Jonathan Sampson
Width? My only problem is that child padding ruins my parent height :P
Daniel
+1  A: 

The real issue is that we are using HTML in ways that its creators had never imagined. The need for 'all those divs' is because some really smart people have found some very creative ways to take a very old standard and do some very modern things with it.

Matthew Vines
That's not the reason the OP is adding unecessary DIV elements.
AmbroseChapel
+2  A: 

No, they are not. The purpose of a div element is to create block level structure in the document. If you can lose them just lose them. Never use divs to solve design purposes, css is for that. Use html elements each like list, data definitions or tables (which were overabused in the past and used as the divs are now for css purposes). The more diverse your HTML knowledge is the less you are using divs all over the place.

Elzo Valugi
A: 

Here's a good article that talks about overuse of divs and how to avoid it:

Divitis: what it is, and how to cure it

Kyralessa
A: 

If you can do what you're trying to do without the additional div, then leave it out. If it's an important part of your design that you can't fix with some clever CSS, then it's still a whole lot better than using tables...

Tom
A: 

I use DIVS primarily for at least one of two main reasons:

  • I need it to provide a new physical level of CSS in some way (either child padding inside a fixed width element, a shadow or a double border around an image, etc.)
  • It logically groups the elements it contains (for future portability and semantics in both HTML and CSS - I.E. "div#menu .label". This way I can use the "label" class several places but have a specific style applied to labels within the "menu" div).

For example, if you were to transfer all attributes to the UL, but then decided you wanted a caption or image above or below the menu but in the same location, you would have to create the DIV again to place the new element inside, and transfer half of the attributes back to it.

Renesis
+1  A: 

Best practice should be to use as few div elements as possible. If you've got a div elements with only one child, chances are it's a useless div. The div element should really only be used when you need a block element and there is no semantic pre-defined element at hand. This includes grouping elements as Renesis suggests.

You
A: 

In the case of a UL, yes, the DIV is unnecessary. They are both block elements, so anything you can do with a DIV wrapped around the outside you can do directly to the UL itself.

However, because of the Box Model problem with some IE browsers, some people tell you to add these DIVs as a workaround. When you combine padding and width, IE6 disagrees with other browsers about what the final size of the element will be. So one workaround is to put padding but no width on an inner element, and width but no padding on an outer element.

AmbroseChapel