div is arguably the most overused and misunderstood element of the HTML specification, so let’s try to kill a few common mistakes right off the bat:
- In CSS, anything you can do to a
divyou can do to any other tag.
divis not special in that regard. You do not need to use a
divin order to access advanced CSS such as positioning.
divshould almost never contain a single element.
Because anything you can do to a
divyou can do to any other element, wrapping a
divaround a single element is almost always redundant and unnecessary.
- A div is not a substitute for, or addition to, a block element.
An unordered list, for example, already has a container element:
<ul>. Wrapping a
- A div is not necessary to add an extra border to an element.
outlineproperty or a CSS3 trick with
box-shadowto achieve the effect instead.
- Avoid setting
heighton divs (in fact, avoid setting a
heighton all elements other than images).
Other than uses of
height: 100%, allow divs to find their own height, judged by their content.