Remove space between header and the page - html

My header has a space above it. I want it to stay to the top without any space. I attached an image that shows the space.
Here is my code:
body {
background-color: red;
}
#example {
height: 75px;
background-color: #484848;
}
<header id="example">
example
</header>

The <body> element has a default margin. Remove it:
body {
background-color: red;
}
#example {
height: 75px;
background-color: #484848;
}
body {
margin-top: 0;
}
<header id="example">
example
</header>

Many elements come with default margins and/or padding.
This is due to the browser's default style sheet.
Add this to your elements where necessary:
margin: 0;
padding: 0;
Here is a sample default stylesheet browsers might use: https://www.w3.org/TR/CSS22/sample.html
EDIT (since you added more code)
In your case, you need to remove the margins from the body element.
body { margin: 0; }

If this is your complete HTML and CSS, the red margin can't be that wide, but anyway: Add
html, body {
margin: 0;
}
to your CSS

This solves your problem. element body have a margin by default.
body{
margin: 0;
}
in the top of the css document.

There are two things you could do. One is remove height: 75px so the div takes only it's natural height (the height of the text).
Or you could add vertical-align: top to the #example to move the text to the top - div stays the same size here.

Related

Div not being fully sized to 100%

My div is not being fully 100% as there is still a margin to it
output:
at the top there is still some whitespace
CSS code:
.topbar {
width: 100%;
height: 10%;
background-color: black;
}
thanks!
You have a margin at your body probably.
Body comes with a default margin. Try this.
body {
margin: 0;
}
If this not work for you, try to set margin: 0 to your other elements to find which one causing this.
Also you can use developer tools and see where comes from this margin.
html, body{
padding: 0;
margin: 0;
}
This may help you 😊.

Unwanted scrollbars when placing two divs below each other

I have two fullscreen divs which are placed relatively below each other. But when I'm visiting the page, the browser always shows me unwanted scrollbars and a width greater than 100vw. When there is only one div, the whole thing works like a charm. Would appreciate any help here :)
<html>
<head>
<link rel="stylesheet" type="text/css" href="normalize.css">
<style>
.section {
position: relative;
width: 100vw;
height: 100vh;
background-color: red;
}
.section.second {
background-color: green;
}
</style>
</head>
<body>
<div class="section">ASD1</div>
<div class="section second">ASD2</div>
</body>
</html>
This is a known issue.
According to https://caniuse.com/#feat=viewport-units,
"Currently all browsers but Firefox incorrectly consider 100vw to be the entire page width, including vertical scroll bar, which can cause a horizontal scroll bar when overflow: auto is set."
You can add following CSS style to fix it,
html, body {margin: 0; padding: 0; overflow-x:hidden;}
Example (JSBin)
Thats because BODY element has its own margins by default. You need to make it zero. You can check it here (jsfiddle example).
body { margin: 0; }
First of all, to remove unwanted margins and paddings, you should always perform a CSS reset (resets all browser specific properties to zero) or a CSS normalization (sets all properties to the same default value for every browser, but not zero). For debugging purposes it is enough to write the following:
* {
margin: 0;
padding: 0;
}
In a real project you should definitely use a better solution like Eric Meyer’s reset or Normalize.css.
Okay, now we managed to solve the spacing issue, but this still leaves us with the scrollbar issue. For a solution look at this post. It says
(...)the horizontal scroll is present because of the vertical scroll.
which you can solve by giving max-width: 100%.
Hence, this is the final solution:
* {
margin: 0;
padding: 0;
}
.section {
position: relative;
width: 100vw;
height: 100vh;
max-width: 100%;
background-color: red;
}
.section.second {
background-color: green;
}
<div class="section">ASD1</div>
<div class="section second">ASD2</div>

Why does this border appear?

Why does this white border always appear around the box? How can I get it to fit the whole page (horizontally) without using 'position:absolute' ?
http://jsfiddle.net/yag79aLt/
.footer-block {
height: 250px;
width: 100%;
background: #000;
}
<div class="footer-block">
Add the following to your CSS:
body {
margin: 0;
}
This will set the page's margin to zero, thus removing the white border around your JSFiddle.
Often there is a small margin around the body by default. In most major browsers, the default margin is 8px on all sides. It is defined in pixels by the user-agent-stylesheet your browser provides. Some browsers add padding too.
I start by adding this in all of my projects to override that:
body {
margin: 0;
padding:0;
}
If you have a large project you could consider using normalize.css. It resets a lot of default values to be consistent across browsers.
http://necolas.github.io/normalize.css/
You should always make margin and padding 0 of body before design.It will make your design perfect..good luck...:)
CSS CODE:
body {
margin: 0;
padding: 0;
}
.footer-block {
height: 250px;
width: 100%;
background: #000;
}

Space <article> <header> position fixed

I have following problem.
I'd like to have header (position:fixed) and some text in article section and between this to section some space.
My CSS code :
header
{
position: fixed
margin-bottom: 10px; // <-- space I'd like to have
}
My HTML code:
<body>
<header> HEADER </header>
<article> SAMPLE TEXT</article>
</body>
But now it looks like header and article are overlapped.
http://jsfiddle.net/LvKSm/embedded/result/
So How to make this space/margin between this to section ?
You need to use margin on article tag
header {
background: #ddd;
position: fixed;
top: 0;
/* width: 100%; */ /* You would probably need this */
}
article {
margin-top: 50px;
}
Demo
Demo (Updated demo of the fiddle you created)
Some Explanation, you are using margin-bottom on header tag, which is a fixed position element, now when you make any element fix, it just gets out of the document flow, and your margin won't have any affect on any element on your document whatsoever.
Also remember to use top: 0; for the header element or when you will use margin-top on article, it will also take header element along.
P.S Commenting using // is invalid, you need to use /* Comment goes here */
This is probably what you want. You need to set a margin on the element following header with a value that equals the header + 10. You have to do this, because the element header has a position fixed which "removes" it from the natural flow of the document, i.e. it will sit on top of the rest of the content, and the other content ignores it. Therefore, article ignores the position of header and takes up its place.
header {
position: fixed;
height: 100px;
width: 100%;
background: red;
top: 0;
}
article {
width: 100%;
margin-top: 110px; /* height header + 10px */
background: blue;
}
If the height of the header is dynamic, you can set the margin-top of the article with jQuery (or JS). Fiddle.
$("article").css({
"margin-top": $("header").height() + 10
});
Edit: I edited your Fiddle, which now works just fine.
The best thing to do here would be applying padding-top to your body tag. SEE THE DEMO
body {padding-top: 120px;}
While also not forgetting to give top: 0; to your header tag since it has a fixed position which is also the reason why margin-bottom won't work for it.

Body height 100% displaying vertical scrollbar

Out of curiosity, considering the example below, why does having the margin on the #container div cause a vertical scrollbar to appear in the browser? The container is much smaller in height than the body height which is set to 100%.
I have set the padding and margins to 0 for all elements except the #container. Note that I have deliberately omitted absolute positioning on the #container div. In this case how is the browser calculating the height of the body and how is the margin affecting it?
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
* { padding:0; margin:0;}
html, body { height:100%; }
#container
{
padding:10px;
margin:50px;
border:1px solid black;
width: 200px;
height: 100px;
}
</style>
</head>
<body>
<div id='container'>
</div>
</body>
</html>
Example also on JSFiddle
If you paint the backgrounds of html and body (giving each its own color), you'll quickly notice that body is being shifted down along with #container, and #container itself isn't offset from the top of body at all. This is a side effect of margin collapse, which I cover in detail here (although that answer describes a slightly different setup).
It's this behavior that's causing the scrollbar to appear, since you've declared body to have 100% the height of html. Note that the actual height of body is unaffected, as margins are never included in height calculations.
Based upon #BoltClock♦'s answer, I fixed it by zeroing the margin...
so
html,body, #st-full-pg {
height: 100%;
margin: 0;
}
works where id "st-full-pg" is assigned to a panel div (which further contained panel-heading and panel-body)
A bit late, but maybe it helps someone.
Adding float: left; to #container removes the scrollbar, as W3C says:
•Margins between a floated box and any other box do not collapse (not even between a float and its in-flow children).
html,body {
height: 100%;
margin: 0;
overflow: hidden;
}
This worked for me
adding float:left; is nice, but will interfere with central horizontal positioning using margin:auto;
if you know how big your margin is, you can account for that in your height percentage using calc:
height: calc(100% - 50px);
browser support is good, but only IE11+
https://caniuse.com/#feat=calc
/*removes default margin & padding*/
html, body{
padding: 0px !important;
margin: 0px !important;
}
/*sets body height to max; and allows scrollbar as page content grows*/
body{
min-height: 100vh;
}
I have found a solution: add padding: 1px 0; to body prevents vertical scrollbars to appear
For those who are coming here for an easier to understand answer that even includes code samples, this answer (copied from here) is for you.
No JavaScript or definite pixel values (such as 100px) are required, just, pure CSS and percentages.
If your div is just sitting there on its own, height: 50% will mean 50% the height of the body. Normally, the height of the body is zero without any visible content, so 50% of that is just, well, zero.
This is the solution (based on this) (uncomment the background lines to get a visualisation of the padding):
/* Makes <html> take up the full page without requiring content to stretch it to that height. */
html
{
height: 100%;
/* background: green; */
}
body
{
/*
100% the height of <html> minus 1 multiple of the total extra height from the padding of <html>.
This prevents an unnecessary vertical scrollbar from appearing.
*/
height: calc(100% - 1em);
/* background: blue; */
}
/* In most cases it's better to use stylesheets instead of inline-CSS. */
div
{
width: 50%;
height: 50%;
background: red;
}
<div></div>
The above was written so that there would still be the usual padding. You could set the dimensions of the red div to 100% and still see padding on each side/end. If you don't want this padding, use this (although it doesn't look nice, I recommend you stick with the first example):
/* Makes <html> take up the full page without requiring content to stretch it to that height. */
html, body
{
height: 100%;
}
/* You can uncomment it but you wouldn't be able to see it anyway. */
/*
html
{
background: green;
}
*/
body
{
margin: 0;
/* background: blue; */
}
/* In most cases it's better to use stylesheets instead of inline-CSS */
div
{
width: 50%;
height: 50%;
background: red;
}
<div></div>
I saw this problem fixed before where you put all the contents of body in a div called wrap. Wrap's style should be set to position: relative; min-height: 100%;. To position #container div 50px from the top and left put a div inside wrap with a padding set to 50px. Margins will not work with wrap and the div we just made, but they will work in #container and everything inside it.
here's my fix on jsfiddle.
you can add non-breaking space into the body tag.
<body> <othertags>...</body>
html, body {
height: 100%;
overflow: hidden;
}
If you want to remove the body scrolling add the following style:
body {
height: 100%;
overflow: hidden;
}
Inspired by #BoltClock, I tried this and it worked, even when zoom out and in.
Browser: Chrome 51
html{
height: 100%;
}
body{
height: 100%;
margin: 0px;
position: relative;
top: -20px;
}
I guess body was shifted down 20px.
It works for me:
html,
body {
height: 100%;
height: -webkit-fill-available; // Chrome
}
// Firefox
#-moz-document url-prefix() {
body {
box-sizing: border-box;
margin: 0;
padding: 1px;
}
}
Add overflow: hidden; to html and body.
html, body {
height: 100%;
overflow: hidden;
}
I found a quick solution: try set height to 99.99% instead of 100%