Extra space in HTML almost never comes from one bug. It comes from whitespace collapsing in text, margin and padding on block elements, leftover default styles, inline-block gaps, flex and grid gaps, line-height, or stray characters in the source. The reliable way to remove it is to inspect the element in devtools, see which of those is actually producing the gap, and fix that one cause rather than scattering margin: 0 everywhere. This guide walks through the whole chain in order, from invisible whitespace between tags to layout-level spacing, so you can shrink or eliminate each kind of gap without breaking the parts of the design you want to keep.
The key condition: decide whether the space is text whitespace (collapsed by the browser), box spacing (margins and padding), or layout spacing (gaps between inline or flex items). The fix differs for each, and applying the wrong one wastes time.
First, identify what kind of space you're removing
Before touching code, open the page in a browser's developer tools and right-click the gap. The inspector shows computed margins and padding as colored overlays (typically orange for margin, green for padding) on the box model. If nothing lights up between two elements, the gap is likely text whitespace, an inline-block artifact, a flex/grid gap, or line-height.
That diagnostic step matters because HTML treats whitespace subtly. Per the CSS Text specification, sequences of whitespace characters collapse into a single space in normal-flow text, and leading/trailing whitespace around block-level content is generally ignored. So a newline and ten spaces between two words render as one space, but a newline between two inline elements can render as a real, visible space. Understanding that one principle explains most "mystery" gaps.
The practical breakdown:
Whitespace between inline elements (links, images, s, buttons laid out inline or inline-block) creates a real word space. Block element margins and default styles on , –, , , , and create vertical gaps. Padding inside an element creates internal space. Line-height creates extra space above and below text lines. Flex/grid gap and justify-content distribute space between items. Stray characters — non-breaking spaces, tabs rendered oddly, empty elements, or artifacts from a CMS — produce space you can't remove with CSS margins.
Each has a distinct fix. Below, the steps run in roughly the order that solves most pages fastest.
Step-by-step: the troubleshooting sequence
Prerequisites: you can edit the HTML/CSS (in a file, a CMS custom-code field, or devtools for testing) and can reload the page. Test changes in devtools first, then move them into your stylesheet so they persist.
1: Find the element producing the gap. Use the inspector and hover the suspect elements. Watch the box-model overlay. If a margin shows up, note which side and which element owns it. Success check: you can name the element and the property responsible before writing any fix.
2: Check whether it's text whitespace. Select the text around the gap. If the gap sits between two inline or inline-block elements and shrinks when you delete the newline/spaces in the HTML source, it's whitespace collapsing. Success check: removing the whitespace between tags removes the gap.
3: Remove default block margins if the gap is vertical and comes from defaults. Headings and paragraphs have browser-default margins. A rule like p, h1, h2, h3, h4, h5, h6 { margin: 0; } (or a targeted reset) removes them. Success check: the element's margin overlay disappears; re-add spacing intentionally where you still want it.
4: Fix inline-block gaps. If you have inline-block items with an unwanted gap, either remove the whitespace in the source, set the parent's font-size: 0 and restore font size on the children, or switch the layout to flex/grid where whitespace between items doesn't render.
5: Inspect flex/grid spacing. If the container is display: flex or display: grid, check gap, justify-content, and align-content. Set gap: 0 or adjust alignment. Success check: the distributed space collapses to the edges or vanishes.
6: Check line-height and font metrics. Extra vertical space around text often comes from line-height larger than needed, or from the font's ascent/descent. Tightening line-height reduces it. Success check: the text block's height shrinks without clipping glyphs.
7: Eliminate stray characters and empty elements. Look for , empty
, used for spacing, and whitespace-only text nodes in unusual places. Remove or replace them. Success check: the gap disappears when the character or empty node is deleted.
8: Confirm no layout breakage. After each fix, check adjacent elements and responsive widths. Stop and reconsider if a fix causes overlap or clipping — that signals you removed structural spacing the layout depended on.
This order works because it moves from the cheapest, most reversible checks (inspecting, deleting a newline) to broader CSS changes.
Removing whitespace between inline and inline-block elements
This is the classic head-scratcher. Write two s or s on separate lines in your HTML and the browser inserts one space between them, because the newline is whitespace and whitespace between inline content collapses to a single space. Three reliable fixes: Delete the whitespace in the source. Put the tags directly adjacent: . Works, but makes HTML harder to read. Use HTML comments to swallow the whitespace: . The comment occupies the gap and the whitespace around it collapses away. Readable and safe. Set the parent's font-size: 0 and restore the size on the children: .parent { font-size: 0; } .parent > * { font-size: 1rem; }. Effective, but you must remember to restore font size, or text inside children inherits zero. Change the layout. A modern approach is to make the container display: flex. Flex items aren't affected by whitespace between them in the same way, so the gap disappears without source gymnastics. For a row of navigation links or image thumbnails, flex is usually the cleanest long-term answer, because it also gives you gap for intentional spacing. Killing default margins and padding Browsers ship default styles. A has a top and bottom margin (commonly 1em), headings have larger margins, has a margin (commonly 8px), and lists have padding plus margins. These defaults create gaps that look like bugs to anyone expecting a blank canvas. Two levels of fix: Targeted reset: remove margins only where you need it. For example, h1, h2, h3, p { margin: 0; } then add back spacing with a utility class or a consistent vertical rhythm. Global reset or normalize: a broad reset like * { margin: 0; padding: 0; box-sizing: border-box; } flattens everything, then you rebuild spacing deliberately. This is common in modern CSS setups but heavier-handed; it removes spacing you may want and forces you to re-specify it. A middle path is to reset only the elements that commonly cause surprise gaps (body, headings, paragraphs, lists, figure, blockquote) and leave the rest. Then control spacing with a small set of spacing utilities or component styles. One caution: box-sizing: border-box changes how width and padding interact and is generally desirable, but it's a layout decision, not a space-removal trick by itself. Treat it separately. Flexbox and grid: where "extra space" is often intentional In flex and grid containers, space between items is usually controlled by gap, justify-content, align-items, and align-content. If items look too far apart, check these before assuming a bug. gap: 0 removes the explicit spacing between flex/grid items. justify-content: flex-start (the default) packs items to the start; space-between, space-around, and space-evenly deliberately distribute space and will look "extra" if you didn't intend them. align-content affects multi-line flex containers and grid rows; stretch is a common default that can make tracks look padded. If the gap remains after setting gap: 0, look at margins on the individual items. Flex and grid don't remove item margins; a child with margin-right: 1rem still produces space even with gap: 0. The success check is a computed gap of zero and item margins of zero where you don't want separation. The vertical-space suspects: line-height, font-size, and empty nodes Text blocks often carry more vertical air than expected. Three culprits: line-height larger than the text needs adds space above the first line and below the last (the "half-leading"). Setting line-height: 1 removes most of it, but can make multi-line text feel cramped, so tune per use. Inline element spacing from font metrics — even with line-height: 1, the font's ascent and descent can leave a sliver of space inside tightly sized elements, which is common when fitting text into buttons or badges. Empty elements and used as spacers. An empty still has margins in many browsers, and adds a line break. Remove them; use margins on real elements instead. A practical check: temporarily set an outline on all elements (* { outline: 1px solid red; }) to see empty boxes and spacing you didn't know existed. Remove the outline afterward. Source-level whitespace: line breaks, tabs, and CMS artifacts Sometimes the space is baked into the HTML string. Rich-text editors and CMS templates frequently insert (non-breaking space), extra newlines, or wrapper tags that render as gaps. Because does not collapse like ordinary whitespace, it produces a stubborn space that margins and resets won't touch. Fixes: Search the rendered HTML (devtools Elements, or view-source) for and empty , , or wrappers. In a CMS, use a "code" or "HTML" view rather than the WYSIWYG editor to clean stray markup, or adjust the editor's paragraph settings. If output is generated by a template, fix the template so it doesn't emit whitespace-only markup around inline elements. Be careful with regex-based cleanup of HTML — naive patterns can strip meaningful content inside attributes or pre/code blocks. If you must script it, target specific known patterns and review the diff. When the space comes from images, tables, and replaced elements Images and other replaced elements sit on the text baseline by default, which leaves a small gap below them (the space reserved for descenders). Setting display: block on the image removes that gap in most layouts. Alternatively, vertical-align: middle or top changes it. Tables have their own spacing controls: border-spacing (with border-collapse: separate) and cell padding. Setting border-collapse: collapse and border-spacing: 0 removes inter-cell gaps; then control padding per cell. These are common in legacy layouts and emails, where the "extra space" is often an image baseline gap or table cell padding rather than a CSS margin. Troubleshooting symptoms and likely causes A quick symptom-to-cause map helps you skip dead ends: Gap between two inline links or images → whitespace in source, or image baseline gap. Consistent vertical gap around every heading or paragraph → browser default margins. Gap only on one side of a block → a margin or padding on that element, often from a component class. Space distributed across a row → flex/grid gap or justify-content. A single stubborn space that won't shrink → or non-collapsing whitespace. Gap below text inside a button → line-height or baseline alignment. For each, apply the reversible fix first (devtools toggle, delete one character, set one property to zero), confirm the cause, then make the change permanent in source or stylesheet. Stop condition: if removing space causes overlapping elements, clipped text, or broken responsive behavior, and you can't identify why the layout depends on that spacing, pause and consult the front-end developer or framework documentation. Blindly zeroing margins across a large codebase can create subtle accessibility and layout failures that are harder to diagnose later. Preventing the problem before it appears The most durable way to remove extra space is to stop generating it. A few habits cut most cases: Adopt a consistent spacing system (for example, spacing tokens or utility classes) so gaps are intentional and traceable. Prefer flex/grid with gap over margins for spacing between items; it avoids whitespace and margin-collapse surprises. Use a small, deliberate reset for headings, paragraphs, and body, then add spacing on purpose. Keep inline-element markup tight or use comment-swallowing where the gap matters. Sanitize CMS output and template whitespace so and empty wrappers don't leak into render. Whitespace collapsing and margin behavior are stable, well-specified parts of CSS, so the principles above hold across modern browsers. What changes with frameworks is where defaults come from — many CSS frameworks and component libraries set their own margins and gaps — so when a gap persists, check the framework's stylesheet before assuming browser defaults. In short, extra space in HTML is usually one of four things: collapsed text whitespace, a margin or padding, a flex/grid gap, or a stray character. Find the owner in devtools, fix that property, and verify nothing else moved. That methodical loop removes the gap and leaves the rest of the layout intact.