Boxes that overflow, styles that refuse to apply and gaps that appear from nowhere usually trace back to a few common CSS mistakes. Each one has a short fix, and you will find them below.
This guide is part of our series of coding tips for beginners.
| Item | Value |
|---|---|
| Default box-sizing value | content-box |
| Basic cases of margin collapsing | three |
| Recommended viewport meta content | width=device-width, initial-scale=1 |
| Zoom-blocking settings to avoid | maximum-scale=1 or user-scaleable=no |
| Where the viewport meta tag goes | in the head of the document |
The figures come from MDN’s box-sizing reference and web.dev’s responsive design basics. Browser documentation gets updated, so check those pages before you rely on the details.
Misunderstanding how box sizing works
The most frequent surprise for beginners is that a width does not mean what they expect. By default, content-box is the box-sizing behavior, so padding and border are added on top of the width you set.
MDN gives an example: if you set an element’s width to 100 pixels, the content box is 100 pixels wide, and the border and padding are added to the final rendered width. The element ends up wider than you planned, which is how a “full width” box pokes out of its container. You can read the explanation in MDN’s box-sizing reference.

The fix is to switch to border-box, which makes the width and height include the content, padding and border, though not the margin. MDN notes that it is often useful for laying out elements, and that browsers already use it for table, select and button elements.
- Apply border-box to every element, plus the generated ::before and ::after content, in one rule at the top of your stylesheet.
- Keep content-box where it helps: MDN says that with relative or absolute positioning, it keeps the positioning values independent of border and padding changes.
- Remember that border-box never includes margin, so spacing still needs its own plan.
Reaching for !important to win a specificity fight
When a style will not apply, the temptation is to add !important and move on. MDN’s cascade layers lesson says plainly that a developer may create a “quick fix” by adding an !important flag.
The trouble is that important declarations take precedence over everything else in the normal order. Once one exists, the only way to override it is another important declaration, and the stylesheet turns into an arms race. Details are in MDN’s cascade layers lesson.
Instead, find out why your rule loses. The usual causes are a more specific selector elsewhere, or the same specificity appearing later in the code. A few habits keep this under control:
- Keep selectors short and based on classes, rather than long chains of nested elements.
- Order your stylesheets deliberately, from general to specific.
- Try cascade layers: MDN describes them as explicit specificity containers that let you prioritize sections of CSS without fighting specificity.
One catch worth knowing is that layers reverse for important styles. MDN states that important styles in earlier declared layers have precedence over important styles in later layers, so avoid mixing the two ideas.
Being surprised by collapsing margins
You give two paragraphs generous vertical margins, and the gap between them is smaller than the sum. That is margin collapsing, and MDN’s guide to margin collapsing says it occurs in three basic cases.
- Adjacent siblings, where their margins merge.
- A parent and its descendants, when nothing separates them.
- Empty blocks, where a block’s top and bottom margins collapse together.
The parent case catches people out. MDN says the margin-top of a parent collapses with its first in-flow descendant unless the parent has a border-top, padding-top, inline content or clearance, so a heading’s top margin can seem to push the whole parent down.
Two facts make it easier to predict. MDN notes that the margins of floating and absolutely positioned elements never collapse. When you want predictable spacing, one common approach is to space siblings in one direction only, or to use a layout method that does not collapse margins.
Skipping the viewport tag and sizing everything in pixels
A page that looks right on your laptop can fall apart on a phone. web.dev says pages optimized for a variety of devices must include a meta viewport tag in the head of the document, and it shows this content value: width=device-width, initial-scale=1.
Without it, web.dev explains, a page loads very zoomed out, making the text hard to read. It also warns that forcing the user to scroll horizontally causes a poor experience, and lists two usual culprits: images with fixed dimensions, and layouts sized in pixels. Both points are in web.dev’s responsive design basics.
- Add the viewport tag to every page template.
- Let images shrink to fit their container instead of fixing their dimensions.
- Prefer relative units and flexible layouts over rigid pixel widths.
The same guide mentions a Lighthouse audit that checks your viewport tag automatically, which is a quick way to catch a missing one.
Blocking zoom and ignoring accessibility
Some beginners copy a viewport tag that disables pinch zoom. web.dev’s accessible responsive design guide says you can prevent zooming with maximum-scale=1 or user-scaleable=no, and then adds: avoid doing this, and let your users zoom in if they need to.
The reason is practical. The same guide describes a low-vision user who might zoom a page as much as 400%, and says a responsive design lets the whole site reflow as the user zooms. A layout built with flexible sizing handles that gracefully, while a rigid one breaks.
A simple routine for finding CSS mistakes
Most problems on this list can be spotted in a browser’s inspector in seconds. Our walkthrough on how to use chrome devtools shows how to inspect an element and read its box model.
MDN’s lesson adds a useful detail: the Styles panel shows crossed-out values that matched the element but were not applied because of the cascade, specificity or source order. A crossed-out rule is your clue to which of the mistakes above you are looking at.
Clean CSS also shows up in your work samples, so the habits here pair well with these web developer portfolio tips.
Frequently asked questions
Why is my element wider than the width I set?
Show answer
Padding and border are most likely being added on top of your width, because the browser default sizes only the content area. Setting border-box on the element makes the declared width cover them too.
Is it ever fine to use !important?
Show answer
MDN frames it as a quick fix, which suggests treating it as a last resort rather than a habit. If you reach for it, first check whether a simpler selector or a cascade layer would solve the conflict.
Why did my margin seem to disappear?
Show answer
Vertical margins of adjacent blocks can merge into one, so the visible gap is smaller than the sum of both. Adding a border or padding to a parent separates it from its first child and stops that parent-child merge.
Do I really need the viewport meta tag?
Show answer
Yes, if you want your page to display properly on phones. web.dev’s example uses width=device-width with initial-scale=1, and it advises against settings that stop people from zooming.



