Web Development - If You Don't Want Margins, You Need To Let The Browser Know (CSS)

Sometimes, you may want the ability to put content right up against the edges of the browser's viewport. While you can force this to happen using absolute positioning, you may have found it difficult when such a workaround isn't what you want.

The reason why content doesn't start right at the top-left edge is because of margins. If you don't set the top and left margins for the <body> element, you may think this is equivalent to setting them to 0px. But that's not actually true. Browsers have built-in stylesheets that cause the <body> element to display as if you had set some margins. For example, in Microsoft Internet Explorer, you actually get a default left margin of 10px and a top margin of 15px. If you need to override these defaults, you can do so with the following code:

  • <style>
  •        body {margin-left:0px; margin-top:0px;}
  • </style>

Go back