zenna
zenna

Reputation: 9186

Are these DIVs really necessary?

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

Upvotes: 3

Views: 324

Answers (9)

AmbroseChapel
AmbroseChapel

Reputation: 12097

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.

Upvotes: 0

You
You

Reputation: 23814

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.

Upvotes: 1

Matthew Vines
Matthew Vines

Reputation: 27581

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.

Upvotes: 1

Elzo Valugi
Elzo Valugi

Reputation: 27876

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.

Upvotes: 2

Sampson
Sampson

Reputation: 268424

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.

Upvotes: 1

Nicole
Nicole

Reputation: 33207

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.

Upvotes: 0

Tom
Tom

Reputation: 5835

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...

Upvotes: 0

Zack Marrapese
Zack Marrapese

Reputation: 12090

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.

Upvotes: 0

geowa4
geowa4

Reputation: 41833

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.

Upvotes: 4

Related Questions