{
  "id": 8552876,
  "title": "Stop Your CSS Layout From Breaking: Understand `box-sizing",
  "url": "https://urgent.news/2026/09/19/stop-your-css-layout-from-breaking-understand-box-sizing",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-09-19T22:45:28.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/codeandseek/stop-your-css-layout-from-breaking-understand-box-sizing-3ac0"
  },
  "original_language": "en",
  "account": "Many web developers have encountered the frustration of CSS elements expanding beyond expected widths. This occurs because of the default CSS box-sizing behavior. When an element is set to a specific width, like 300px, it often turns out to be wider due to additional space taken by padding and border.\n\nThe CSS box model breaks down an HTML element into four components: content, padding, border, and margin. Understanding this model is crucial for preventing layout issues such as overflowing cards, inputs, or buttons. By default, the box-sizing property is set to content-box. This means the declared width only includes the content, and padding and borders add extra space outside of it.\n\nFor example, consider a CSS rule for a class named card:\n```\n.card {\nwidth: 300px;\npadding: 20px;\nborder: 2px solid #333;\n}\n```\nWithout adjusting the box-sizing property, the total width of the card element becomes 344px. This is calculated as follows:\n- Content: 300px\n- Padding: 20px + 20px\n- Border: 2px + 2px\n- Total width: 344px\n\nThis discrepancy can lead to layout problems, especially when the containing element only has space for a 300px element. Adding overflow: hidden; to the parent container can mask the problem instead of resolving it.\n\nA better approach is to use border-box. By setting box-sizing: border-box;, the padding and border are included within the declared width. The revised CSS would be:\n```\n.card {\nbox-sizing: border-box;\nwidth: 300px;\npadding: 20px;\nborder: 2px solid #333;\n}\n```\nIn this setup:\n- Declared width: 300px\n- Final width: 300px\n\nThis approach simplifies element sizing and eliminates unexpected overflow. Applying border-box globally to all elements in a project can be achieved with the following CSS:\n```\n*, *::before, *::after {\nbox-sizing: border-box;\n}\n```\nThis ensures consistent and predictable sizing for elements like cards, inputs, buttons, and other UI components. A handy debugging trick involves temporarily adding outlines to all elements:\n```\n* {\noutline: 1px solid red;\n}\n```\nThis visual cue helps identify elements that extend beyond their intended boundaries, making it easier to troubleshoot layout issues. Once the problem is identified, the debugging outline can be removed.\n\nUnderstanding the difference between content-box and border-box is essential. Remember, content-box measures the content width only, while border-box includes content, padding, and border, making it the preferred choice for most layouts.\n\nFor further learning, check out these resources:\n- MDN: box-sizing - https://developer.mozilla.org/en-US/docs/Web/CSS/box-sizing\n- MDN: CSS Box Model - https://developer.mozilla.org/en-US/docs/Learn/web_development/Core/Styling_basics/Box_model\n\nThese references provide deeper insights into the CSS box model and box-sizing properties.",
  "summary": "If you've ever set an element to width: 300px and wondered why it ends up wider than 300px, you're not alone. One of the most important CSS concepts for beginners is the box model . Understanding it can save you a lot of time debugging overflowing cards, inputs, buttons, and layouts. The CSS Box Model Every HTML element can be thought of as a box made up of four main parts: Content Padding Border…",
  "key_points": [],
  "editors_take": null,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}