Why do I not have the 8px default margin on body? [duplicate] - html

This question already has answers here:
Why does styling the background of the body element affect the entire screen?
(7 answers)
Giving background-color to body applying whole page. Why?
(6 answers)
Closed 4 months ago.
When I set background-color, there are no white spaces as expected. My browser is chrome and it was a brand-new code, no other styles where applied in the CSS. Same thing happens with or without more components present in the HTML file. Added a div to try it out and same result.
body {
background-color: aquamarine;
}

In my experience, if you add any component or element inside , you would be offered a margin of 8px.
<body>
<div style="background-color:red;height: fit-content;"></div>
</body>

Did you set both margin and padding to 0 in css?
You can style the body's margin to 8px if you would like to have the space without color or you can style the body's padding to 8px if you would like to get space color same as body background color.

I would check the height and width of the component you are trying to see. Make sure neither of them are 0.
UPDATE: I think I understand your question better now. If you want to see the 8px margin on the body set by default in Chrome, run this code snippet.
html {
height: 100%;
}
body {
height: 100%;
}
.example {
background-color: aquamarine;
height: 100%;
}
<html>
<body>
<div class='example'/>
</body>
</html>

Related

Why is there space between my <header> and the border of the page? [duplicate]

This question already has answers here:
What are the default margins for the html heading tags (<h1>, <h2>, <h3>, etc.)?
(4 answers)
How wide is the default `<body>` margin?
(4 answers)
CSS margin terror; Margin adds space outside parent element [duplicate]
(7 answers)
Closed 20 days ago.
My page is just a tag with an in it. When I set the background-color of the to black, there is a weird gap between the black background and the page border. Setting the padding and margin on all sides to 0px does not help. Why is this?
#homeHDR {
background-color: black;
}
<header id="homeHDR">
<h1><span id="blueSPN">Papyrus</span>Note</h1>
</header>
The <body> and <h1> tags have a default margin, so resetting them will give you the expected result without any borders:
#homeHDR {
background-color: black;
}
body, h1 { /* Remove default margin for these guys */
margin: 0;
}
<header id="homeHDR">
<h1><span id="blueSPN">Papyrus</span>Note</h1>
</header>
Be careful though. You might only want to reset the margin for the h1 tag that's inside the header to get rid of the ugly border at the top, but not other h1 tags found inside the page (even though that's discouraged). So maybe, you want to have a more specific selector for that h1, e.g. #homeHDR h1 { margin: 0; }.
As Jon P correctly pointed out, the Inspector and Elements Panel in the DevTools are the only way to safely detect why these layout issues are happening, e.g. in your case which elements create these spaces, by hovering over the elements.
Margin from body:
Margin from h1:
There will be a duplicate out there somewhere for this.
body has a default margin, generally of 8px. Remove it and your are good
body {
margin: 0;
}
#homeHDR {
background-color: black;
}
<header id="homeHDR">
<h1><span id="blueSPN">Papyrus</span>Note</h1>
</header>
Somewhat more important than this code is getting to know the tools at your disposal. All desktop browsers these days have some variation of "Developer Tools", commonly accessed through f12. Through these you can inspect elements to see that styles have been applied to the elements. By inspecting the h1 you will see where the vertical spacing is coming from.

'border' makes 'background-color' works different [duplicate]

This question already has answers here:
Why does this CSS margin-top style not work?
(14 answers)
Closed 1 year ago.
This question seems not so significant as others but I'd like to understand how it works and why it works this way.
We have a very simple code pen here:
html, body {
margin: 0;
padding: 0;
}
.header {
background-color: #ccc;
/* border: 3px solid pink; */
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test1</title>
</head>
<body>
<div class="header">
<h1>Header</h1>
<h2>Subheader</h2>
</div>
</body>
</html>
When border is commented the pen works like this:
We can see that h1 and h2 margins are not colored by property 'background-color'. Here I thought that 'ok, it's because it's only what div.header contains.
But if we are removing comments and make a border for div.header it suddenly becomes behave different.
Now h1 and h2 margins became a part of div.header though border is only thing that changed. Can you help me explain this phenomenon. I know this has a logical explanation. Thank you very much.
The border doesn't have any (direct) effect on the background colour, it has an effect on the margins.
Without a border, the top and bottom margins of the child elements collapse through the edge of the element.
When you add a border, those margins stop at the border pushing the child elements away from it and increasing the size of the element inside the border (which is filled with the background colour).
See this article for further reading.

Why is my header element not covering the whole width of the page and leaves a gap on the top? [duplicate]

This question already has answers here:
How wide is the default `<body>` margin?
(4 answers)
Closed 2 years ago.
i´d like to make a header with position fixed that covers the whole width and starts at the very top. But when i type in the code below i get this: output .Is there a way i can solve this problem?
#header {
width: 100%;
height: 100px;
background-color: black;
}
<body>
<header id="header"></header>
</body>
This article right here will help you :
https://css-tricks.com/reset-all-margins-padding/
By default, pages load with some padding and margin. This is why, unless you remove this default padding, all elements will be a little off from the page edges.
The Universal selector * allows you to reset the border as such :
* {
padding: 0;
margin: 0;
}

How to extend div to the entire page? [duplicate]

This question already has answers here:
React force background color full height
(3 answers)
Percentage Height HTML 5/CSS
(7 answers)
Closed 3 years ago.
I am trying to set background color to the pages in my React app. I want to set a background color extending to the full page length and width but I cannot do that, for forms or tables extending beyond, I set height/width or min-height/min-width to 100% and I get the result for larger contents but for smaller contents,I get this:
I want to have the entire page of blue color.
This is my css file
.body
{
margin:0px 0px 0px 0px;
background-color:#4086ef;
padding:10px;
height:100%;
width:100%;
}
If I set height to 100vh, I get the undesired result but with contents going beyond the page.
(Content rendering is dynamic so I don't know when the content will go beyond and when not).
EDIT:
The table doesn't squeeze along when I compress the window and neither does the overflowing part follow the background color but the height follows the background color even when scrolled.
You just need to add height:100vh.So that it will cover your whole screen.
Try with this
* {
box-sizing: border-box;
}
body {
margin: 0;
}
div {
background: red;
color: white;
min-height: 100vh;
padding: 10px;
}
<div>
Test
</div>
You can use:
height: 100% !important;

Why is my background spread across the browser when I set body {width: 960px; margin: auto 0;}? [duplicate]

This question already has answers here:
Applying a background to <html> and/or <body>
(2 answers)
Closed 8 years ago.
So I have this very basic question that I was wondering about. I just set a background through css with
body {
width: 960px;
margin: 0 auto;
background-image: url("../img/background2.jpg");
}
Why is the image showing up at both sides as well and not only in the center 960 pixels? If I put my background image in a navigation class selector, it does work:
.container {
background: #099;
}
Why is that? Shouldn't the body image be restricted by the width that I set?
Here is my code: http://jsfiddle.net/nB22j/
Also, is there any use for the .container selector if I can just put everything in body {} ? (In this case I do want the background to fill the full browser so I can put my background in body {} but I'm just wondering...) I'm not sure anymore why I added the container div in the first place. Any reason for it to exist?
Because, if you set no background to HTML , body's background is applied to HTML too.
Just add this : DEMO
html {
background:#fff;/* or any color/image/gradient you want */
}
and your background for body will only be drawn where body stands.
See W3C for more infos.