The CSS Podcast

68: Why isn't the margin applying?

Aug 9, 2023
Ask episode
AI Snips
Chapters
Transcript
Episode notes
ANECDOTE

Child Margin Moving The Parent

  • A coworker saw a child element's large margin push its parent and wondered why the parent moved.
  • They fixed it by adding overflow:hidden to create a BFC and contain the child's margin.
ADVICE

Check Browser Defaults And Scope Styles

  • Check user agent styles and scope your custom margins to avoid unexpected defaults from parsed markdown.
  • If a child's margin appears to move its parent, create a new BFC (e.g., overflow:hidden) to contain it.
ADVICE

Don't Use Vertical Margins On Inline Or Table Cells

  • Avoid relying on vertical margins for inline or table-cell elements; they won't push in the block direction.
  • Use padding for inline elements and border-spacing or horizontal auto margins for table cells instead.
Get the Snipd Podcast app to discover more snips from this episode
Get the app