<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Code and Seek</title>
    <description>The latest articles on DEV Community by Code and Seek (@codeandseek).</description>
    <link>https://dev.to/codeandseek</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4133463%2Fdd4ca44e-92a6-49a0-99b8-eb2959facf8e.jpg</url>
      <title>DEV Community: Code and Seek</title>
      <link>https://dev.to/codeandseek</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/codeandseek"/>
    <language>en</language>
    <item>
      <title>Stop Your CSS Layout From Breaking: Understand `box-sizing</title>
      <dc:creator>Code and Seek</dc:creator>
      <pubDate>Sat, 19 Sep 2026 22:45:28 +0000</pubDate>
      <link>https://dev.to/codeandseek/stop-your-css-layout-from-breaking-understand-box-sizing-3ac0</link>
      <guid>https://dev.to/codeandseek/stop-your-css-layout-from-breaking-understand-box-sizing-3ac0</guid>
      <description>&lt;p&gt;If you've ever set an element to &lt;code&gt;width: 300px&lt;/code&gt; and wondered why it ends up wider than 300px, you're not alone.&lt;/p&gt;

&lt;p&gt;One of the most important CSS concepts for beginners is the &lt;strong&gt;box model&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Understanding it can save you a lot of time debugging overflowing cards, inputs, buttons, and layouts.&lt;/p&gt;

&lt;h2&gt;
  
  
  The CSS Box Model
&lt;/h2&gt;

&lt;p&gt;Every HTML element can be thought of as a box made up of four main parts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Content&lt;/li&gt;
&lt;li&gt;Padding&lt;/li&gt;
&lt;li&gt;Border&lt;/li&gt;
&lt;li&gt;Margin&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The important part is understanding what happens when you give that box a width.&lt;/p&gt;

&lt;p&gt;Consider this CSS:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.card&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;300px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;20px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;border&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2px&lt;/span&gt; &lt;span class="nb"&gt;solid&lt;/span&gt; &lt;span class="m"&gt;#333&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You might expect the card to be exactly &lt;code&gt;300px&lt;/code&gt; wide.&lt;/p&gt;

&lt;p&gt;But with the default CSS box-sizing behavior, it isn't.&lt;/p&gt;

&lt;p&gt;The browser calculates:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Content: 300px
Padding: 20px + 20px
Border: 2px + 2px

Total width: 344px
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So your &lt;code&gt;300px&lt;/code&gt; element actually takes up &lt;strong&gt;344px&lt;/strong&gt; horizontally.&lt;/p&gt;

&lt;p&gt;That's because the default value is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;box-sizing&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;content-box&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Why This Can Break Your Layout
&lt;/h2&gt;

&lt;p&gt;Imagine the card is inside a container that only has enough space for a 300px element.&lt;/p&gt;

&lt;p&gt;Your additional padding and border can cause the card to overflow its parent.&lt;/p&gt;

&lt;p&gt;A common reaction is to add:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;overflow&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;hidden&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But that can simply hide the symptom instead of fixing the sizing problem.&lt;/p&gt;

&lt;p&gt;A better solution is often &lt;code&gt;border-box&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use &lt;code&gt;border-box&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Change the element to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.card&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;box-sizing&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;border-box&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;300px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;20px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;border&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2px&lt;/span&gt; &lt;span class="nb"&gt;solid&lt;/span&gt; &lt;span class="m"&gt;#333&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the padding and border are included &lt;strong&gt;inside the declared 300px width&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;So:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Declared width: 300px
Final width: 300px
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This makes element sizing much easier to reason about.&lt;/p&gt;

&lt;h2&gt;
  
  
  Apply It Globally
&lt;/h2&gt;

&lt;p&gt;Instead of adding &lt;code&gt;box-sizing&lt;/code&gt; to every component individually, you can apply it across your project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="o"&gt;*,&lt;/span&gt;
&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nd"&gt;::before&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nd"&gt;::after&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;box-sizing&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;border-box&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now your regular elements and their &lt;code&gt;::before&lt;/code&gt; and &lt;code&gt;::after&lt;/code&gt; pseudo-elements use predictable sizing.&lt;/p&gt;

&lt;p&gt;This is especially useful for:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.card&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="nt"&gt;input&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It can make forms, cards, buttons, and other UI components much easier to align.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Quick CSS Debugging Trick
&lt;/h2&gt;

&lt;p&gt;When you don't know which element is causing unexpected spacing or overflow, temporarily add:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;outline&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1px&lt;/span&gt; &lt;span class="nb"&gt;solid&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This gives every element a visible outline.&lt;/p&gt;

&lt;p&gt;You can quickly inspect where an element extends beyond its expected boundaries.&lt;/p&gt;

&lt;p&gt;Once you've found the problem, remove the debugging rule.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;content-box&lt;/code&gt; vs &lt;code&gt;border-box&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Here's the simple way to remember the difference:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;content-box&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Your declared width applies to the content. Padding and borders are added outside it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;border-box&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Your declared width includes the content, padding, and border.&lt;/p&gt;

&lt;p&gt;For most everyday layouts, &lt;code&gt;border-box&lt;/code&gt; makes sizing much more predictable.&lt;/p&gt;

&lt;h2&gt;
  
  
  Watch the Video
&lt;/h2&gt;

&lt;p&gt;I also made a short video demonstrating this visually, along with more practical CSS tips for beginners.&lt;/p&gt;

&lt;p&gt;🎥 &lt;strong&gt;YouTube:&lt;/strong&gt; &lt;a href="https://www.youtube.com/watch?v=GIqqMHN0Ifc" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=GIqqMHN0Ifc&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you're learning HTML, CSS, JavaScript, React, and web development, you can also find more tutorials on my channel:&lt;/p&gt;

&lt;p&gt;📺 &lt;strong&gt;Code and Seek:&lt;/strong&gt; &lt;a href="https://www.youtube.com/@CodeAndSeek?sub_confirmation=1" rel="noopener noreferrer"&gt;https://www.youtube.com/@CodeAndSeek?sub_confirmation=1&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Useful References
&lt;/h2&gt;

&lt;p&gt;If you want to explore the concepts further:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;MDN: &lt;code&gt;box-sizing&lt;/code&gt; — &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/box-sizing" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/CSS/box-sizing&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;MDN: CSS Box Model — &lt;a href="https://developer.mozilla.org/en-US/docs/Learn_web_development/Core/Styling_basics/Box_model" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Learn_web_development/Core/Styling_basics/Box_model&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;What CSS concept confused you the most when you first started learning?&lt;/p&gt;

&lt;p&gt;Let me know in the comments.&lt;/p&gt;

&lt;h1&gt;
  
  
  css #webdev #beginners #tutorial
&lt;/h1&gt;

</description>
      <category>beginners</category>
      <category>css</category>
      <category>frontend</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
