Neal Grosskopf Dot Com A NG Designs Site

Neal Grosskopf Dot Com Return To Neal Grosskopf's Home Page a NG Designs Site

Toolbar

  • Skip To
    • Menu
    • Content
  • Validate
    • HTML
    • CSS
    • WAI
  • Font Size
    • Small
    • Medium
    • Large
    • Extra Large
  • Account
    • Create Account
    • Login

Interact

Navigation

  • Home
  • Interact
    • Blog
    • Message Board
    • Open Links
    • Open Videos
    • Photo Gallery
    • Polls
    • Reviews
    • Geek Speak
  • Designs
    • View All Designs
    • Professional
    • Corporate
    • Templates
    • Personal
    • Club
    • NG Designs
    • Web Guides
  • Files
    • Artwork
    • Wallpapers
    • My File Downloads
  • Extras
    • Bookmarklets
    • The Magic 8-Ball
    • Halo Statistics
    • My Web Footprint
    • Career Builder RSS Feed Generator
    • Monster.com RSS Feed Generator

HTML Vs. BODY Element: What's the Difference?

  • Actions
    • View All
    • View Article Statistics
    • View User Statistics
    • Search
    • Subscribe to RSS Feed

Recent Articles:

  • Don't Like The New Facebook Home Page? Fix It With 5 Lines of CSS
  • Find Unused CSS Selectors With CSS Usage
  • A List of New CSS Features In Firefox 3.6
  • Create An Icon Bouncing Effect With jQuery & CSS
  • A jQuery Tool That Generates External Stylesheets From Your HTML Structure
View All

Subscribe To Feed
loading Subscribers

I'm On Twitter loading

May 10

I've always been interested in the subtle differences between the HTML and BODY elements when it comes to CSS. Many designers reference the two elements interchangeably in their stylesheets. Often times it's perfectly ok to place properties in either one, but there are certain instances where you'll want to chose one over the other.

Default Rendering of HTML and BODY Elements

By default, all major browsers assign the BODY element a margin of 8px. We often see this in CSS resets, setting the margin back to 0px. As far as I'm aware there is no other default CSS applied to either elements other than your usual font-size, color etc. The HTML element, in comparison, has a margin set to 0px. Also neither of the two elements have padding, which is often seen in CSS resets as well.

html { margin: 0px; } body { margin: 8px; }

Overflow/Scrollbars

Another thing I like to place in my stylesheets is some CSS to always display browser scrollbars. I do this because it annoys me when I browse to one page that has a lot of content and then browse to another page that has little content, and my main content DIV is bouncing back and forth. In order to fix this you need to place 'overflow-y: scroll;' on your HTML element. If this property is assigned to the BODY element, nothing happens and it will not work, thus another big difference between the two elements.

html { overflow-y: scroll; }

Scrollbar Colors: HTML or BODY?

In Internet Explorer the scrollbar color properties need to be applied to the HTML element rather than the BODY element in order to work. Safari has also added scrollbar styling in their recent build, so we'll have to see how they implemented this as well.

html { scrollbar-face-color: #000; scrollbar-highlight-color: #ccc; scrollbar-3dlight-color: #999; scrollbar-darkshadow-color: #666; scrollbar-shadow-color: #555; scrollbar-arrow-color: #fff; scrollbar-track-color: #ddd; }

Background Images and Colors

Another major difference between the HTML element and the BODY element is background-colors and background-images. The BODY element mimics the default behavior of a DIV element. It's height relies on the content within it. The HTML element on the other hand, spans the entire viewport regardless of the content within it. If you set a background-image to the BODY element and position it bottom, it may not have the desired effect you intended, while the HTML element's background-image will fall at the bottom of the screen regardless of the content within it.

BODY Vs. HTML Element Background Colors

In Conclusion

Probably the biggest take-away from this article would be background-images. I almost always use both the HTML and BODY elements to apply background-images to, and it's good to remember that the BODY element may not always hit the 'bottom' of the viewport.

Know of any other differences? Share your thoughts below...

Tags: html ( Add Tag )

Bookmark This Page:
  • Digg loading
  • Delicious loading
  • Re-Tweet loading
  • StumbleUpon
  • DZone loading
  • Snipplr
  • Script & Style

Replies:

  • Kevin Mario
    Kevin Mario
    #1

    Tue. May 12, 8:23:09 PM

    You can always set both heights to 100% and thus making BODY as tall as HTML, thus it will always hit the 'bottom' of the viewport won't it ?

  • Neal Grosskopf
    Neal Grosskopf
    #2

    Tue. May 12, 9:27:56 PM

    Kevin, that will work, but the element needs to have it's position set to absolute.

  • Dele BA
    Dele BA
    #3

    Wed. June 10, 9:30:35 AM

    How can you get both images in the Html & Body elements to show in IE6 - I have been trying this for ages without success - Only one or the other will show but not both.

  • Neal Grosskopf
    Neal Grosskopf
    #4

    Wed. June 10, 8:41:52 PM

    @Dele: It might depend if you set your images to repeat-y or not. The BODY background-image will cover up the HTML background-image in the layer stacking. If your BODY image is set to repeat-y it will by default cover up most if not all of your HTML image.


Reply:

B U I Link Font Color Edit Preview ?
Characters: 0 Textbox Size: [+] [-]
   

  • New Comment
    #5

    Tue. February 9, 8:44:16 PM

Font Size:

  • A
  • A
  • A
  • A

Search:

Places:

  • Blog
  • Message Board
  • Chat Room
  • Geek Speak
  • Open Videos
  • Reviews
  • Tag Cloud
 

Site Links

Site

  • About
  • Contact Me
  • Site Map
  • Site Search

Interact

  • Blog
  • Geek Speak
  • Message Board
  • Open Links
  • Open Videos
  • Photo Gallery
  • Polls
  • Reviews

Random Stuff

  • Hosting by FatCow
  • © Copyright 2005 - 2010
  • Modified: Friday, January 22, 2010
  • Load Time: 0.45 Seconds

Site Coded And Designed By Neal Grosskopf Using No Crappy CMS Or Themes