Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8078097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:41:01+00:00 2026-06-05T15:41:01+00:00

$(#toggle).click(function(){ $(html).toggleClass(bg); }); html.bg { background: blue; } body { background: yellow; } <script

  • 0
$("#toggle").click(function(){
    $("html").toggleClass("bg");
});
html.bg {
    background: blue;
}

body {
    background: yellow;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!DOCTYPE html>
<html class="bg">
<head>
</head>

<body>
    Test
    <br>
    <button id="toggle">Toggle HTML background</button>
</body>
</html>

I found that if you apply a CSS background to body, it takes up the whole page (no matter what the actual height or width of body is).

However, if you apply a CSS background to both html and body, the background for body does not take up the whole page.

Is this discrepancy expected behavior?

How would I go about superimposing two fullscreen backgrounds (say, a background color and a semi-transparent image?)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T15:41:03+00:00Added an answer on June 5, 2026 at 3:41 pm

    This is correct behavior.1 In standards mode, body, as well as html, doesn’t immediately take up the entire height of the viewport, even though it appears so when you only apply a background to the latter. In fact, the html element will take on the background of body if you don’t give it its own background, and html will pass this on to the canvas:

    The background of the root element becomes the background of the canvas and its background painting area extends to cover the entire canvas, although any images are sized and positioned relative to the root element as if they were painted for that element alone. (In other words, the background positioning area is determined as for the root element.) If the root’s ‘background-color’ value is ‘transparent’, the canvas’s background color is UA dependent. The root element does not paint this background again, i.e., the used value of its background is transparent.

    For documents whose root element is an HTML HTML element or an XHTML html element: if the computed value of ‘background-image’ on the root element is ‘none’ and its ‘background-color’ is ‘transparent’, user agents must instead propagate the computed values of the background properties from that element’s first HTML BODY or XHTML body child element. The used values of that BODY element’s background properties are their initial values, and the propagated values are treated as if they were specified on the root element. It is recommended that authors of HTML documents specify the canvas background for the BODY element rather than the HTML element.

    That said, however, you can superimpose any background image over a background color on a single element (either html or body), without having to rely on two elements — simply use background-color and background-image or combine them in the background shorthand property:

    body {
        background: #ddd url(background.png) center top no-repeat;
    }
    

    If you wish to combine two background images, you need to rely on multiple backgrounds. There are chiefly two days to do this:

    • In CSS2, this is where styling both elements comes in handy: simply set a background image to html and another image to body which you wish to superimpose over the first. To ensure the background image on body displays at full viewport height, you need to apply height and min-height respectively as well:

      html {
          height: 100%;
          background: #ddd url(background1.png) repeat;
      }
      
      body {
          min-height: 100%;
          background: transparent url(background2.png) center top no-repeat;
      }
      

      Incidentally, the reason why you have to specify height and min-height to html and body respectively is because neither element has any intrinsic height. Both are height: auto by default. It is the viewport that has 100% height, so height: 100% is taken from the viewport, then applied to body as a minimum to allow for scrolling of content.

    • In CSS3, the syntax has been extended so you can declare multiple background values in a single property, eliminating the need to apply backgrounds to multiple elements (or adjust height/min-height):

      body {
          background: url(background2.png) center top no-repeat, 
                      #ddd url(background1.png) repeat;
      }
      

      The only caveat is that in a single multi-layered background, only the bottommost layer may have a background color. You can see in this example that the transparent value is missing from the upper layer.

      And don’t worry — the behavior specified above with propagating background values works exactly the same even if you use multi-layered backgrounds.

    If you need to support older browsers, though, you’ll need to go with the CSS2 method, which is supported all the way back to IE7.

    My comments under this other answer explain, with an accompanying fiddle, how body is actually offset from html by default margins even though it looks like it’s being padded out instead, again owing to this seemingly strange phenomenon.


    1 This may have its roots in setting the HTML background and bgcolor attributes of body causing the background attribute to apply to the entire viewport. More on that here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

$(.trigger).click(function () { $(this).next(.toggle).slideToggle(fast); $(this).toggleClass(active); return false }); HTML: <input type=checkbox class=trigger> <div class=toggle>content</div>
$('.showall').css(cursor,pointer).click(function() { $('.value-container').toggle(); $('.dim-header').toggleClass($('.dim-header').toggleClass() == 'dim-header' ? 'dim-header active' : 'dim-header'); $('.showall').html($('.showall').html() == '+
JQUERY: $(li h2).click(function() { $(this).toggleClass(active).siblings().removeClass(active); $(this).next(div).slideToggle(fast).siblings(div).slideUp(fast); }); HTML: <ul> <li> <h2>headingA</h2> <div>contentA</div> <h2>headingB</h2> <div>contentB</div>
I have found this code (jQuery): $('.toggle').click(function() { $('.container').eq($(this).index()).toggle('fast'); }); This is my HTML:
I have the following code $(document).ready(function() { $('a#slick-toggle').click(function() { $('#slickbox0').toggle(400); return false; }); });
I have the following code: <script> $(document).ready(function() { $('.toggle_section').click(function(e){ parent = $(e.target).closest(div) objChild =
The code like this: $(.hide).click(function(){ $(.online-contact).animate({width: 'toggle',height: '100%'}, fast); $(this).hide(); $(.show-class).show(); }) I want
I've got this piece of code to toggle a side panel: $(.example_wrapper_panel_link).click(function() { $(.example_wrapper).addClass('example_wrapper_active');
Really quick jQuery question... I have this function: $(document).ready(function() { $('a#show1').click(function() { $('.item1').toggle(1000); return
Um, why no worky? Trying to toggle a ul with this: $(#others).children(.btn).click(function(){ if ($(this).children(ul).is(:hidden))

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.