What is the reason the BODY tag does not supersede the properties of the HTML tag?

After applying this css to my html file:

html {
  background-color: red;
}
body {
  background-color: yellow;
}

The page's background actually turns out to be red instead of yellow. Since the body comes after html in the code, it should override the red color specified for html. Why does this not occur? Thanks in advance.

Answer №1

When the body of your website has no content and is empty (0px height)

If we adjust the height and width of the body, you will see a difference (including margins/padding that can be reset)

html {
  background-color: red;
}

body {
  background-color: yellow;
  width: 100vw;
  height: 100vh;
}

When there is content:

html {
  background-color: red;
}

body {
  background-color: yellow;
}
<h1>Body no longer empty</h1>

Answer №2

The HTML and BODY elements are distinct entities, therefore the cascade rules do not apply in this scenario.

Your confusion likely stems from CSS having some specific rules for the HTML and BODY elements:

When the root element of a document is an HTML or XHTML element with 'transparent' background-color and 'none' background-image values, browsers will use the first HTML "BODY" or XHTML "body" child element's computed background properties for painting backgrounds on the canvas, excluding the background of that child element itself.

If you only set styling for one of them, that color will be applied.

For example, setting a red background for the HTML element:

html { background: red; }

In contrast, setting a yellow background for the BODY element results in a yellow background due to these special rules:

body { background: yellow; }

However, if both elements have been styled, these rules do not take effect.

Therefore, the HTML element remains red and the BODY element is yellow, although it may not be visible if there is no content in the body causing its height to be zero.

html { background: red; }
body { background: yellow; }
<p>Hello, world.
<p>With content in the body, the background is now visible as it no longer has zero height.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilize the power of Nuxt and Vue 3 to create sortable columns in a table, with the ability to only change the icons on the sorted column

I am facing a challenge with my sortable table icons. Whenever I click on an icon to sort a column, all the other icons also change direction. I understand that this is happening because I am toggling the visibility of icons based on the currentSortDir sta ...

What is the best way to incorporate text or characters into your website using CSS only, without using

Will it be able to function in IE? I am aware that IE does not support the content property for anything outside of what? ...

Tips for streaming a partially generated video file in a web browser

My current issue involves a program that generates a video in a specific file format, and I want to display this video file in popular browsers like Firefox, Chrome, or any other browser using an HTML5 tag. The problem arises when the video file is only 5 ...

Create a sleek and uniform design with Bootstrap by setting up three columns that maintain the same height

I am attempting to design a 3 column div that maintains equal height for all responsive rows, each containing an ICON and information. <div class="container"> <div class="row row-eq-height"> <div class="col c ...

Guide on navigating to a specific page with ngx-bootstrap pagination

Is there a way to navigate to a specific page using ngx-bootstrap pagination by entering the page number into an input field? Check out this code snippet: ***Template:*** <div class="row"> <div class="col-xs-12 col-12"> ...

How to open a PDF file in a new tab using Angular

Within my Angular 12 application, I am seeking to enable users to "view" a PDF file stored locally in the application within a new tab on Google Chrome. Currently, when implementing the code below in HTML, the file downloads rather than opening in a new ta ...

Dynamic background image that fills the entire webpage, adjusts to different screen sizes, and changes randomly

Currently, I am working on designing a web page with a dynamic background image that adjusts responsively in the browser without distortion. The challenge is to randomly select an image from an array using jQuery. Unfortunately, my knowledge of jQuery is ...

Leveraging HTML attributes in CMS source code

My website is built using Prestashop, and the content pages are created using: Prestashop > Preferences > CMS > Source-Code. For each CMS page, I utilize basic HTML in the source code. THE ISSUE I am attempting to add a widget to the site&apos ...

How to style a sublevel menu to appear next to its parent using CSS

I have a menu that is currently functioning well, but I would like to add a new sublevel onto it. However, if I directly add the new options, they end up hiding the existing ones on the menu. Here's an image for reference: I want the new options to ...

Tips for utilizing $.get information to update specific elements on a web page

I am currently utilizing jQuery 1.4.2 to smoothly navigate between similar web pages within Firefox 3. Upon clicking a link, a JavaScript script should load the new HTML, filter out the correct elements and replace them on the existing page. It appears th ...

Stop the automatic hiding of the sticky header when reaching the bottom of the page

I have implemented the position: sticky property for my website's header. However, I am facing an issue where the header becomes hidden when I scroll halfway down the page (bottom on mobile devices). Can anyone suggest a solution to fix this problem? ...

All I desire is to eliminate the term "class" from the text

Upon clicking the code, only the value 137 should display according to the image. However, the word "class" also appears. https://i.stack.imgur.com/HZhr4.png <a rel="facebox" href="portal.php?id='.$rowa["_id"].' class="icon-remove"></a ...

Leveraging Jsoup for HTML parsing

As a beginner in coding with Java and using Android Studio, I am currently working on a project involving Jsoup to extract HTML source code from a URL. The main objective now is to parse the HTML for a specific line that contains a link, however, only the ...

Personalizing the presentation of asp.net webforms code

Having recently entered the asp.net realm, I've been intrigued by the talk surrounding asp.net mvc and its superior ability to customize markup and css compared to webforms. Despite hearing that asp.net is easier to learn than asp.net mvc, I've o ...

Top scroll button malfunctioning on this page

Updated question for better understanding. In my current project, let's imagine I am on the following URL: http://127.0.0.1:8000/blog/ and within that page I have the following HTML: <a href="#top">Back to top</a> Upon hovering over tha ...

Issue with Colspan in Nested Tables in Bootstrap 4.1.3

I'm dealing with an issue related to colspan within a nested table. I've tried various solutions, including wrapping the table in a div, but nothing seems to be working as expected. My goal is to have the nested table span across the entire width ...

How can I upload a file using the following HTML script with Selenium in Python?

I attempted to utilize the send_keys() method directly on an attached path file, but encountered an error. Can anyone provide guidance on how to properly upload a file? Here is my Selenium Python script: from selenium import webdriver from selenium.webdri ...

Desktop sharing in HTML5/H.264 format for optimal performance and compatibility across all

Looking to showcase my desktop through live streaming over http to multiple users. The initial plan is to provide a real-time read-only view of the desktop for various users. In the future, we may consider granting users access to control the desktop using ...

Execute Jquery ajax only on the initial invocation

When I am using ajax post in jQuery to build a portion of a page, I am encountering an issue where it only runs once. Below is the code snippet I am working with: <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery ...

Integrate a three.js scene seamlessly into the background of a webpage

I have successfully created two separate HTML documents—one where a three.js scene is rendered and a basic HTML page. Now, my query is how to effectively combine them. My aim is to display a rotating green cube in the left corner and a moving red cube th ...