<?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>Forem: Peter Mungai</title>
    <description>The latest articles on Forem by Peter Mungai (@p1toah).</description>
    <link>https://forem.com/p1toah</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1341472%2F129f1df4-657b-46ac-9c78-cba9177c9b28.png</url>
      <title>Forem: Peter Mungai</title>
      <link>https://forem.com/p1toah</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/p1toah"/>
    <language>en</language>
    <item>
      <title>THE BOX MODEL AND BOX-SIZING.</title>
      <dc:creator>Peter Mungai</dc:creator>
      <pubDate>Wed, 13 Mar 2024 17:27:12 +0000</pubDate>
      <link>https://forem.com/p1toah/the-box-model-and-box-sizing-4ncn</link>
      <guid>https://forem.com/p1toah/the-box-model-and-box-sizing-4ncn</guid>
      <description>&lt;p&gt;The box-model and box-sizing are css elements that needs to be used together in many occasions if not all.&lt;/p&gt;

&lt;p&gt;Lets dive on what they are: &lt;/p&gt;

&lt;p&gt;-The box model is used for design and layouts that wraps around every html element.&lt;/p&gt;

&lt;p&gt;The box-model consist of the content, padding, border and margin as seen below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flk0zu51nkaofcavqlaah.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flk0zu51nkaofcavqlaah.png" alt="The box model" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The content area contains the content of the the box.&lt;/p&gt;

&lt;p&gt;Padding adds more background around the content.&lt;/p&gt;

&lt;p&gt;Border creates a visible border around the content and its padding.&lt;/p&gt;

&lt;p&gt;Margin creates more background outside the border.&lt;/p&gt;

&lt;p&gt;example in code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.content {
background-color: aqua;
padding: 2em;
border: 2px solid red;
margin: 2em;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One properties that is important to note when using the box model is width.&lt;br&gt;
The width sets the width of the content area only.&lt;br&gt;
This means when adding some units in padding and border they are not accounted for-they are added on top of the width of the content.&lt;/p&gt;

&lt;p&gt;i.e if width is 200px and padding-left and padding-right are 10px and the border is 5px the resulting width will be 230px.&lt;/p&gt;

&lt;p&gt;N/B: Margin is not included when calculating the width since it is outside the border.The primary use of margin is to separate elements.&lt;/p&gt;

&lt;p&gt;This is where the box-sizing can be useful.&lt;br&gt;
When we declare&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;* {
box-sizing: border-box;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The width accounts for the padding and the border which allows us to get the intended width we wanted.&lt;/p&gt;

&lt;h2&gt;
  
  
  MORE WIDTH TIPS
&lt;/h2&gt;

&lt;p&gt;The default width value is auto which lets the browsers to control the width of the elements.&lt;/p&gt;

&lt;p&gt;But it can be tricky when we set the width to 100% since this results to overflows issues.&lt;br&gt;
When we declare a width of 100% and add border and margin the content starts to overflow.This is because the content is set to 100% and takes all the space making the addition of border and margin make it to overflow.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.content {
width: 100%;
background-color: aqua;
padding: 2em;
border: 2px solid red;
margin: 2em;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using the box-sizing  helps a bit in this scenario-it includes the padding and border in it but the remedy is to leave the width to default-auto.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;* { box-sixing: border-box;}

.content {
background-color: aqua;
padding: 2em;
border: 2px solid limegreen;
margin: 2em;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thanks for reading this far I hope you enjoyed.&lt;/p&gt;

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