Margins create space outside an element. They are useful but can become unpredictable with collapse and mixed conventions.
Key behaviors
- Vertical margins between block elements can collapse.
margin: 0 autocenters fixed-width blocks horizontally.- Negative margins are advanced and easy to misuse.
Pitfall
Mixing arbitrary margins across components creates spacing debt. Prefer spacing scales and layout wrappers.
Important interview questions and answers
- Q: What is margin collapsing?
A: Adjacent vertical margins merge into one margin equal to the larger value. - Q: Why avoid negative margins casually?
A: They can cause overlap bugs and fragile responsive behavior. - Q: Better alternative for list spacing?
A: Use parent stack/gap patterns when possible.
Pitfall: Check cascade order—author stylesheet loses to inline styles and !important surprises.