Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove the space between div and top of page?

Tags:

html

css

margin

This has probably been asked a million and one times, but I would appreciate it if someone could explain the behavior of the divs to me..

I have a container div which I am aligning in the center of the page, which has a gap between the top and the top of the page. I want it to be flush against the top of the page. I am assuming that there is some sort of margin or padding that I need to remove but I can't think what it could be. Even with nothing in the div there is still a gap.

    <body>
    <div id='mainContent'>

    </div>
</body>


body
{
    background-color:black;
    background-image:url("img/background.jpg");
    background-repeat:repeat;
}

#mainContent
{
    width:1200px;
    height:500px;
    background-color:#FFFFFF;
    margin-left:auto;
    margin-right:auto;
    margin-top:0px;
}

Here is a JSFiddle to give you an idea of what I mean.

Can someone please explain why the div is pushed down as it is? Is there a robust solution that doesn't affect any content that is put in the div??

NOTE: If the screen width is smaller than the div width, there will be a gap on the left hand side aswell.

like image 838
user2630210 Avatar asked Nov 06 '13 15:11

user2630210


People also ask

How do I remove space between header and body?

To check and adjust:Use File > Page Setup to access your layout settings. Click Customize for your header/footer document to edit it. While editing the document, click Insert > Header to access that area. Reduce any blank or empty space in your header that is below your information.

How do I reduce the space at the top in HTML?

Adjusting the Margin Size of an HTML Element With CSS You can remove this margin by setting the top and left margin to zero. Like the padding and border, the sizes of specific sides of the margin can be set using margin-left , margin-right , margin-top , and margin-bottom .

How can I remove space between div elements?

There are two methods for removal of space between two divs or two elements. First one is, assign the font size of the parent of the inline block element to 0px and then assign the proper font-size to the inline block element. and Second is, Make the display of the parent element to flex.


1 Answers

You need to reset the default margin of the body that is 8px aprox.

body,html {
  margin:0;
  padding:0;
}

The Demo http://jsfiddle.net/H76bq/3/

For default all elements has some properties:

http://www.w3.org/TR/CSS21/sample.html

You can reset this in your own css.

like image 93
DaniP Avatar answered Sep 30 '22 17:09

DaniP