Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS: How can I get rid of the default window "padding"? An element set to 100% width doesn't reach the window's borders

So I have an element that is placed directly inside body:

<body>     <div id="header">Some stuff...</div>     Other stuff... </body> 

The following is the CSS used:

body{     text-align:center; } #header{     margin:auto; } 

So the #header div is set to 100% width (default) and is centered. Problem is, there's a "space" between the window border and the #header element... Like:

|  |----header----|   | ^window border        ^window border 

I tried adjusting it with javascript, and it successfully resizes the element to the exact window width, but it doesn't eliminate the "space":

$('#header').width($(window).width()); 

One solution seems to be to add the following CSS rules (and keep the javascript above):

#header{     margin:auto;     position:relative;     top:-8px;     left:-8px; } 

In my browser this "space" is 8px - but I'm not sure if that's the same across all browsers? I'm using Firefox on Ubuntu...

So what's the right way for getting rid of this space - and if it's what I used above, do all browsers act the same?

like image 811
Alex Avatar asked Dec 31 '10 12:12

Alex


People also ask

How do I get rid of default margin padding?

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 do I get rid of default padding and margin in CSS?

The right answer for this question is "css reset". It removes all default margin and padding for every object on the page, no holds barred, regardless of browser.

Does width 100% include padding?

No, element width does not include padding, margin, or border. The basic difference between padding and width is that in padding you define the space around the element and on the other hand in the width you define the space of the element.


1 Answers

body has default margins on all browsers, so all you need to do is shave them off:

body {     margin: 0;     text-align: center; } 

You can then remove the negative margins from #header.

like image 146
BoltClock Avatar answered Oct 13 '22 20:10

BoltClock