What causes certain properties of html and body elements to behave in this way?

I'm on a quest for understanding some mysteries.

First riddle:

Why does the body have a mysterious margin-top?

html {
  height: 100%;
  background-color: red;
}
body {
  height: 100%;
  margin: 0;
  background-color: yellow;
}
<h1>Hello Plunker!</h1>

Using dev tools in Chrome reveals an intriguing phenomenon where the top margin of h1 extends beyond the boundary of body:

https://i.sstatic.net/sgvnq.jpg

Second enigma:

In this peculiar scenario, why does the yellow hue spill outside the confines of body?

I anticipated that the yellow color would stay contained within the body as per the overflow property declaration:

body {
  height: 100px;
  background-color: yellow;
  margin: 0;
  overflow: hidden;
}

The third conundrum

If a mystical background-color is bestowed upon the sacred html element, it miraculously contains the yellow aura within the realm of body, but why?

html {
  background-color: red;
}
body {
  height: 100px;
  background-color: yellow;
  margin: 0;
  overflow: hidden;
}

Answer №1

Your Initial Query

What is the reason behind the margin-top on the body?

The Explanation Is The margin-top on the body element is due to the h1 tag, as it applies margins from both the top and bottom. Your observation regarding the display of html in red color is noted. This behavior is default but can be adjusted by adding a float property to the h1 element.

h1{float: left;}  

Your Follow-Up Question

I expected the yellow color to be contained within the body element only, considering I set the overflow property

The Response Is

The overflow property only functions when a fixed width/height is applied to a tag or class. Applying overflow hidden to html/body does not have the desired effect, possibly due to default browser behavior such as in Firefox. The same experience has been encountered by others as well.

The answer to the third question is also summarized within the response to the second question.

Hopefully, this information proves useful. Thank you

Answer №2

Simply changing the CSS to add padding instead of setting margin: 0 on h1 should fix the problem you're experiencing.

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

Design a background image that is optimized for both high-resolution retina displays and standard non-ret

Scenario I am working on a web page where I want the background image to be fixed, covering the entire screen or window (excluding tablets and smartphones). The background image has been created using ImageShack. Everything is running smoothly so far. T ...

Issue with alignment in the multiselect filter of a React data grid

In React Data Grid, there is a issue where selecting multiple filter options messes up the column headers. Is there a solution to display selected filter options above a line in a dropdown rather than adding them to the column header? The column header siz ...

Can you control the order of rendering for specific divs in a ReactJS application?

I need assistance with developing a mobile app using ReactJS and react bootstrap that can dynamically resize itself based on the screen size. One specific part of the app requires calculations to determine its dimensions based on the remaining space on the ...

Include a picture and a division element within the dropdown element

I've encountered a few challenges while trying to personalize a profile dropdown component using Semantic and React. First, I am facing an issue where my application crashes when I attempt to place a div inside the Dropdown.Menu with the error message ...

Automate the process of filling up and activating a bootstrap dropdown upon a click, and clearing it out when it is

Is there a method to reveal the dropdown content only upon clicking instead of displaying all content at once and increasing the lines of HTML code? Perhaps using an onclick attribute on the button and incorporating a function inside? var data = [{ " ...

Having trouble locating and scrolling through elements while webscraping with Python Selenium, encountering the 'cannot focus element' error

I have been trying to extract ticket information from the VividSeats website. The URL I am working with is '' My tools of choice are Selenium and Python. I managed to navigate to the page using Chrome WebDriver and interact with the pop-up tha ...

My efforts to resolve the issues in my code have been ongoing, but unfortunately, I have been unable to find a solution

I'm struggling to insert my contact form code into the contact tab without it interfering with the tab bar. I want the form to appear in the middle of the page when the tab is clicked. I've tried various placements for the code but none seem to ...

What is the best way to activate a JQ function with my submit button?

Is there a way to trigger a JQ function when clicking the submit button in a form? I managed to make Dreamweaver initiate an entire JS file, but not a particular function. ...

Element remains hidden until the developer console is activated

On my website, I've noticed that certain LayerSlider elements are remaining invisible until: The window is resized I disable the Bookmarks bar in Chrome (quite strange, right?) I switch on the Chrome debugger tools This issue is not exclusive to Ch ...

Is a responsive mega menu with rollover effects available for Bootstrap?

Looking to develop a responsive megamenu for bootstrap 3, I recently came across one that caught my eye: I decided to implement it into my own project at However, I encountered a major issue with the menu functionality on desktop devices, as it requires ...

How can I determine the specific quantity of XPATH links with unique identifiers in Selenium?

Seeking automation with Python3 and selenium to streamline searches on a public information site. The process involves entering a person's name, selecting the desired spelling (with or without accents), navigating through a list of lawsuits, and acces ...

What is the best way to make HTML adjust to SVG scaling?

After applying a scale transform to an SVG, I noticed that the surrounding HTML does not adjust its size accordingly. Here is the SVG in question: <div> <svg height="300" width="300" viewbox="0 0 300 300" transform="scale(1.55)" xmlns= ...

What is the maximum amount of information that can be stored in a data attribute within the DOM?

Occasionally, I find myself wanting to include a substantial amount of data on the webpage in order to minimize additional AJAX calls for dynamic content. However, I am concerned about potential performance implications. Is there a penalty I should be aw ...

The single answer input field is not displaying as a readonly text input

Here is a jsfiddle link Please pay attention to Question 2 in the jsfiddle table, as it contains only one answer. In the jquery function, I have attempted to make the text input under the Marks Per Answer column readonly if a question has only one answer ...

The NG8002 error has occurred, as it is not possible to connect to 'matDatepicker' because it is not a recognized attribute of 'input'

I've come across an issue while working on my Angular 15 application with Angular Material. I'm trying to incorporate a date picker, but after adding the code snippet below, I encountered an error. <mat-form-field appearance="outline" ...

Extracting Tik Tok videos from their URLs using PHP web scraping

I'm attempting to save videos from the following URL: Original: https://api.tiktokv.com/aweme/v1/playwm/?video_id=v09044340000bq0vmd39if221ld9o5n0 The link then changes to this: http://v16m.tiktokcdn.com/e50056249a925719f71ce8618053e173/5eee4e39/vid ...

What steps can I take to resolve the issue in my code? I keep receiving a type error stating that it cannot read

I seem to be encountering an issue when running my code where I receive a 'cannot read property 'age' of null'. This error breaks my code, and I'm trying to figure out how to implement a check to ensure it only runs when I am signe ...

Transferring the AJAX response into a JavaScript variable

New to AJAX and JS here. I am implementing an AJAX code that fetches data from a php service. I am trying to figure out how to store the returned data in a JavaScript variable, which I can then display on the UI. Below is my AJAX code snippet: <script ...

Is there a way to have only the <a> tag be clickable in an unordered list, without making the entire list item clickable?

I just need the text to be made clickable instead of the entire list item block. <a class="menu_head">Hello</a> <ul class="menu_body"> <li><a href="#">Sub-menu 1</a></li> <li><a href="#">Sub-menu 2 ...

What is the best way to create stylish corners on my website?

I'm struggling with creating a corner like the one shown in the image. As a beginner in frontend development, I attempted to write the following code but now I'm stuck. The Snippet: * { margin: 0; padding: 0; } body { height: 100vh; ...