A sidebar drops below the main content and will not sit beside it. You spend an hour in the stylesheet: floats, widths, flex settings, margins. Nothing works, because nothing is wrong with the CSS. The sidebar markup sits inside the wrong wrapper, so the browser is laying out a different page than the one in your head. Bugs like this survive for hours when the markup is an unreadable one-line block, and they take minutes when the nesting is visible.
That is the case for formatting first. Before you debug a layout, run the markup through the free HTML Formatter on Ovatly and read the structure it shows you. This guide explains why nesting depth is where layout bugs hide, how to do the formatting pass, and how to read the result like a map of the page.
Contents
- Quick answer
- Layout bugs are often structure bugs
- What your eye cannot do with unformatted markup
- How to format the markup, step by step
- Worked example: the footer inside the wrong parent
- Limitations: what formatting cannot tell you
- Common mistakes
- FAQs
- Conclusion
Quick answer
When a layout will not behave, format the HTML before touching the CSS. Paste the markup into the HTML Formatter, choose an indent of 2 or 4 spaces, and press Format HTML. Then read down the left edge of the output: every element's indent shows exactly which parent it belongs to. If a block sits one level deeper than it should, you have found a structure bug that no stylesheet edit could fix. Correct the markup, and only then go back to the CSS for whatever remains.
Layout bugs are often structure bugs
CSS never sees your intentions. It sees a tree of elements, and every rule applies to that tree exactly as written. A footer placed inside a main element inherits the width, padding and background of that parent. A card placed outside the grid container gets none of the grid's behaviour. A wrapper that closes too early quietly adopts every sibling after it, and those siblings then pick up styles meant for a much smaller box.
From the stylesheet side, all of these look like CSS failures. The rules are correct, the selectors match, the values are sane, yet the page is wrong. The mismatch is between the tree you think you wrote and the tree the browser actually built. Debugging that gap from inside the stylesheet is slow, because the stylesheet is not where the gap lives.
What your eye cannot do with unformatted markup
Copied or generated markup usually arrives as one long line or with random indentation. In that form, nesting depth is invisible. A section that opens at the start of a line might close three hundred words later, and nothing on screen tells you which elements fell inside it along the way. You end up counting opening and closing tags by hand, losing your place, and guessing.
Formatting converts depth into position. With a consistent indent, a child sits visibly inside its parent, one step deeper per level. A closing tag that lands at the wrong indent stands out immediately. A block that was meant to be a sibling of the sidebar but renders as its child shows up as one extra level of indentation. You stop counting tags and start reading the page's skeleton, which is the thing the browser lays out.
How to format the markup, step by step
- Copy the section of markup that contains the misbehaving layout, or the whole document if it is small enough to read comfortably.
- Open the HTML Formatter and paste the markup into the input box.
- Choose an indent size of 2 or 4 spaces. For deep nesting, 2 spaces keeps long lines on screen, which makes depth easier to compare.
- Press Format HTML.
- Read the output from the problem area outward: find the element that looks wrong on the page, check its indent, then check its parent's indent, and work up until the structure matches what you intended.
- Copy the result or download it as a text file, fix the nesting in your real file, and reload the page before changing any CSS.
Worked example: the footer inside the wrong parent
A page footer is rendering narrow, squeezed to the width of the article column instead of stretching across the page. The CSS for the footer sets no width at all, so the stylesheet looks innocent. The copied markup, on one line, looks like this:
<div class="page"><div class="sidebar"><p>Menu</p></div><div class="main"><p>Article text</p><div class="footer"><p>Copyright</p></div></div></div>
One formatting pass with a 2-space indent returns:
<div class="page">
<div class="sidebar">
<p>Menu</p>
</div>
<div class="main">
<p>Article text</p>
<div class="footer">
<p>Copyright</p>
</div>
</div>
</div>
The bug is now plain: the footer block sits inside main, at the same depth as the article paragraph. Every width and padding rule on main applies to it. The fix is a markup fix, moving the footer block out so it sits directly inside page as a sibling of main. No CSS change was needed, and an afternoon of stylesheet experiments would never have found this, because the stylesheet was never the problem.
Limitations: what formatting cannot tell you
Formatting fixes indentation only. It does not repair invalid HTML, close missing tags or validate your markup. If a wrapper is genuinely unclosed rather than just closed in the wrong place, the formatter cannot invent the correct structure; mismatched tags may still produce messy output, and a validator is the right tool for that job.
Formatting also says nothing about the CSS itself. Once the tree is correct, a layout can still be wrong because of a bad value, a specificity fight or a missing rule. The formatting pass earns its place by ruling structure in or out quickly, so the CSS debugging that follows is aimed at the right layer. And if you minify as part of your checks, remember that removing whitespace between tags can very occasionally matter for inline elements separated by a space, so glance at tight inline layouts afterwards.
Common mistakes
- Starting in the stylesheet. An hour of CSS changes against a wrongly nested tree produces nothing but confusion. Check the structure first; it takes minutes.
- Formatting the whole site when one section is broken. Paste the region around the problem. A short, relevant fragment is faster to read than a full page.
- Reading the formatted output too quickly. The value is in the indents. Slow down at the problem element and trace its ancestors one level at a time.
- Assuming odd output means the formatter failed. If the formatted result still looks tangled, the markup itself may be mismatched. That is a signal to validate, not to keep formatting.
- Fixing the copy instead of the source. The formatted output is a diagnostic view. Apply the nesting fix in your real file, or the bug returns with the next build.
FAQs
Can formatting itself fix a layout bug? No. Indentation changes do not affect rendering in normal HTML. Formatting helps because it makes the real structure visible, so you can spot the element that sits in the wrong place and fix the markup itself.
What kinds of problems does a formatting pass reveal? Elements nested one level deeper than you expected, blocks that close at an unexpected indent, and sections that turn out to live inside a parent they were never meant to have. Those are the structure problems behind many layout bugs.
Should I format before or after editing the CSS? Before. Five minutes checking the structure can save an hour of stylesheet changes that were never going to work, because the CSS was being applied to a different tree than the one you imagined.
Does formatting change how my page renders? Indentation changes do not affect rendering in normal HTML. Minifying removes whitespace between tags, which can very occasionally matter for inline elements separated by a space, so check pages that rely on exact inline spacing.
Is my HTML uploaded anywhere? No. Formatting and minifying run entirely in your browser.
Conclusion
Layout debugging goes faster when you debug the layer where the bug actually lives. Format first, read the nesting, fix the tree, and let the CSS work on the structure you meant to build. When the markup you are debugging arrived as a copied mess rather than your own code, the companion guide on how to fix messy copied HTML walks through that cleanup pass in detail, and the rest of this series lives in the Developer Tools guides archive.