How to Remove Margins from the Browser
In this article
To remove the default margins around a web page in a browser, set the CSS margin of the body element to 0 and add a reset for other common elements. This is a code change, not a browser setting: browsers apply an 8px margin to the body by default, and you override it in your stylesheet. You will need access to the site's HTML and CSS files, or a browser extension that injects custom CSS for pages you do not own.
What You'll Need
- A text editor or code editor for editing HTML and CSS files.
- Access to the website's source files, or a browser extension such as Stylus for injecting custom CSS on sites you do not control.
- A modern browser with developer tools (Chrome, Edge, Firefox, Safari) for inspecting and testing changes.
Step-by-Step
- Open your stylesheet. Locate the main CSS file linked in your HTML
<head>, or open the<style>block if styles are inline. If you are using a site builder, open its custom CSS panel. Menu labels vary by version and platform, but the concept is the same: find where global styles are defined. - Add a CSS reset at the top. Insert the following rule before any other styles so it applies globally:
The universal selector removes margins and padding from every element, and* { margin: 0; padding: 0; box-sizing: border-box; }box-sizing: border-boxkeeps widths predictable. If you only want to remove the page margin, targetbodyinstead:body { margin: 0; } - Check for conflicting rules. Search your CSS for other
margindeclarations onbody,html, or wrapper elements. Later rules with equal specificity override earlier ones, so a rule further down the file may reintroduce the margin. Remove or adjust those rules. - Test in the browser. Save the file and reload the page. Open developer tools (F12 or right-click > Inspect) and select the
bodyelement. The computed margin should read0px. If it does not, check the Styles panel for the rule that is winning. - Handle full-width sections. If some sections still have side gaps, inspect them for their own
marginorpadding. Common culprits are container classes withmax-widthandmargin: 0 auto. To make a section truly edge-to-edge, set its width to100%and remove horizontal margins. - Apply to sites you do not own. Install a userstyle manager extension from your browser's official add-on store. Create a new style for the site, add
body { margin: 0 !important; }, and save. The!importantflag ensures your rule wins over the site's own CSS. This only affects your browser, not other visitors. - Verify on mobile and other browsers. Resize the window or use device emulation in developer tools. Some browsers add their own default margins or scrollbar spacing, so confirm the result in at least two browsers. On mobile, check that the viewport meta tag is present:
<meta name="viewport" content="width=device-width, initial-scale=1">.
Common Mistakes
- Forgetting specificity. A rule like
body { margin: 0; }will lose tohtml body { margin: 8px; }or an inline style. Use developer tools to see which rule is applied and increase specificity or add!importantonly as a last resort. - Removing all padding. The universal reset also removes padding from buttons, inputs, and lists, which can make them look cramped. If you only need to remove the page margin, target
bodyalone. - Ignoring wrapper elements. Many templates wrap content in a
<div class="container">with its own margins. Removing the body margin will not remove those; inspect and adjust the wrapper too. - Using
margin: 0onhtmlonly. The visible margin usually comes frombody, nothtml. Set both to zero if you want to be thorough. - Expecting a browser setting. There is no built-in browser toggle to remove page margins. It is always a CSS change or an extension.
Tips
- Use a CSS reset like Normalize.css or a minimal reset as a starting point; it handles cross-browser inconsistencies beyond margins.
- Keep a backup of your stylesheet before making global changes, especially if you are editing a live site.
- Test with the browser's device toolbar to catch mobile-specific margins added by some frameworks.
- If you use a userstyle extension, scope the style to the specific domain so it does not affect every site you visit.
- Remember that removing margins can make text touch the screen edge; add padding to inner containers for readability.
Troubleshooting
If the margin persists after adding body { margin: 0; }, open developer tools and inspect the body element. In the Styles panel, look for any rule with a margin value that has a checkmark next to it. That is the rule winning the cascade. You can temporarily disable rules with the checkbox to identify the source, then edit your CSS accordingly.
If the page still shows a gap on the left or right, check for a scrollbar. A vertical scrollbar can reduce the viewport width and create the appearance of a margin. You can test by adding overflow-y: scroll to html to force a consistent scrollbar, or by hiding overflow if appropriate.
If you are using a userstyle extension and the margin remains, ensure the style is enabled for the correct URL pattern and that the rule includes !important. Some sites load CSS dynamically after page load; in that case, the extension may need to reapply the style. Check the extension's documentation for options like "apply to iframes" or "run at document start".
If you are editing a site you own and changes do not appear, clear the browser cache or hard-reload (Ctrl+F5 or Cmd+Shift+R). Also confirm you edited the correct file; many sites use minified CSS generated from source files, so edit the source and rebuild if necessary.
Frequently Asked Questions
Can I remove margins from any website without editing its code?
Yes, by using a browser extension that injects custom CSS, such as Stylus. You add a rule for that site, and it applies only in your browser.
Does removing the body margin affect the whole page?
It removes the default space around the entire page content. Individual elements may still have their own margins unless you reset them too.
Why does my page still have a margin after setting body margin to 0?
Another CSS rule with higher specificity or an !important flag is likely overriding it. Use developer tools to find the winning rule.
Is there a browser setting to remove margins?
No. Browsers do not provide a built-in margin removal setting; it is controlled by the page's CSS.
Will removing margins break my layout?
It can, especially if your design relies on the default spacing. Test thoroughly and add padding to inner containers as needed.
Can I remove margins only on mobile?
Yes, use a media query in your CSS to apply margin: 0 only below a certain screen width.