<?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: Jesus Perez</title>
    <description>The latest articles on Forem by Jesus Perez (@jesushperez).</description>
    <link>https://forem.com/jesushperez</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%2F135582%2F0a48e871-4e13-468f-af82-b83d9588df71.jpg</url>
      <title>Forem: Jesus Perez</title>
      <link>https://forem.com/jesushperez</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/jesushperez"/>
    <language>en</language>
    <item>
      <title>Using the Box Model to debug CSS (an introduction)</title>
      <dc:creator>Jesus Perez</dc:creator>
      <pubDate>Thu, 16 Jun 2022 19:37:23 +0000</pubDate>
      <link>https://forem.com/jesushperez/using-the-box-model-to-debug-css-an-introduction-3djm</link>
      <guid>https://forem.com/jesushperez/using-the-box-model-to-debug-css-an-introduction-3djm</guid>
      <description>&lt;p&gt;&lt;em&gt;NOTE: This post is only an introduction to debugging CSS with the box model. It's not a definitive guide but it can help you get started if you're learning CSS.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;Everything displayed by CSS is a box.&lt;/p&gt;

&lt;p&gt;Have you seen those art drawings made using only HTML and CSS?&lt;/p&gt;

&lt;p&gt;&lt;iframe class="tweet-embed" id="tweet-1534050575989428224-348" src="https://platform.twitter.com/embed/Tweet.html?id=1534050575989428224"&gt;
&lt;/iframe&gt;

  // Detect dark theme
  var iframe = document.getElementById('tweet-1534050575989428224-348');
  if (document.body.className.includes('dark-theme')) {
    iframe.src = "https://platform.twitter.com/embed/Tweet.html?id=1534050575989428224&amp;amp;theme=dark"
  }



&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(Go give Pratham a &lt;a href="https://twitter.com/Prathkum" rel="noopener noreferrer"&gt;follow on Twitter&lt;/a&gt; if you're into web dev.)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Yes, every element in that drawing is a box. They may look like different shapes, but as far as the web page is concerned, they are all boxes. Understanding this principle is important to learning the CSS box model.&lt;/p&gt;

&lt;p&gt;In this blog post, I am going to do three things. First, I am going to explain why you should learn the box model. Second, I'm going to describe the properties related to the box model. And third, I am going to cover two "weird" behaviors you may encounter in CSS and we'll explore how to understand and resolve them with the CSS box model.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why learn the CSS Box Model?
&lt;/h2&gt;

&lt;p&gt;Programming languages have rules built into them. When you break a rule, the text editor or IDE will (usually) tell you when something went wrong. It will even tell you where the bug originated from and possible fixes.&lt;/p&gt;

&lt;p&gt;CSS is not like that. It has rules, but it won't try to fight you when you use it. At best, you may see squiggly lines in your text editor if you leave out a colon, a semi-colon, a curly bracket, or when you try to use a property that doesn't exist.&lt;/p&gt;

&lt;p&gt;However, if you write CSS that breaks your site layout, CSS will not help you identify the buggy code. Unlike programming languages, it will not tell you why your page layout broke. Similarly, if an element collapses on itself or if content overflows, CSS will not tell you why that happened or how to fix it.&lt;/p&gt;

&lt;p&gt;Why is this the case? Because CSS assumes you know what you're doing.&lt;/p&gt;

&lt;p&gt;There are several tools that web developers can use to debug CSS. One of those tools is the CSS box model.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is the CSS Box Model?
&lt;/h2&gt;

&lt;p&gt;Everything displayed by CSS is a box. The CSS box model describes how the boxes work and their four properties: content, padding, border, and margin.&lt;/p&gt;

&lt;p&gt;The image below shows how these four properties work together to make up a box that says "Hello World."&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5h0zmq4ek6fjjti622eu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5h0zmq4ek6fjjti622eu.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Margin Box
&lt;/h3&gt;

&lt;p&gt;The orange area represents the margin box. The margin is the space surrounding the box. Increasing the size of the margin will push the box in certain directions.&lt;/p&gt;

&lt;p&gt;For example, adding margin to the left of the box will push the box to the right.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;p {
  margin-top: 15px;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Border Box
&lt;/h3&gt;

&lt;p&gt;The black outline between the green and orange areas is the border box. It can defined like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;p {
  border: 5px solid black;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Padding Box
&lt;/h3&gt;

&lt;p&gt;The padding box is the area shown in green. It's the space between the content box and the border. When you set a background to an element, the padding box's background changes along with the content box. Padding is useful because it lets the content box breathe with whitespace.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;p {
  padding: 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Content Box
&lt;/h3&gt;

&lt;p&gt;Finally, there's the content box shown in blue. This is the inner most box where your content lives. When you set the width and height of your element, you are actually setting the width and height of your content box.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;p {
  width: 5px;
  height: 2px;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Default values
&lt;/h3&gt;

&lt;p&gt;Some browsers give certain elements default styles. For example, Google Chrome gives &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; elements a default margin top and bottom of 16px. It's usually a good idea to use libraries that reset the default browser styles. This will ensure that your webpage looks the same across browsers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Debugging CSS with the box model
&lt;/h2&gt;

&lt;p&gt;People sometimes get frustrated with CSS because it doesn't do what they want it to do. But CSS has rules. Understanding these rules will help you enjoy CSS a bit more.&lt;/p&gt;

&lt;p&gt;Let's examine two weird behaviors that people usually encounter in CSS. We'll go over how to use the box model to investigate these weird behaviors, and we'll learn the CSS rules that cause these behaviors.&lt;/p&gt;

&lt;h3&gt;
  
  
  The alternative box model
&lt;/h3&gt;

&lt;p&gt;Let's start with a simple HTML document:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="container&amp;gt;
  &amp;lt;div class="box-1"&amp;gt;Box 1&amp;lt;/div&amp;gt;
  &amp;lt;div class="box-2"&amp;gt;Box 2&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To distinguish the elements, we'll give them different background colors and change the text color to white so make it readable. We'll also give them widths and heights.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.container {
  background-color: pink;
  width: 500px;
  height: 500px;
  margin: 25px;
  color: white;
}

.box-1, .box-2 {
  width: 50%;
  height: 50%;
}

.box-1 {
  background-color: blue;
}

.box-2 {
  background-color: red;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The web page should look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnbimpnpriuc61v3cqmve.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnbimpnpriuc61v3cqmve.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You know what? It drives me nuts when text is right up against the edge of its element, so let's add padding to the two boxes. Let's add 15px of padding all around:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.box-1, .box-2 {
  width: 50%;
  height: 50%;
  padding: 15px;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That should be better:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1bb3eajwxx9xi6gky427.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1bb3eajwxx9xi6gky427.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What happened?? The boxes are overflowing out of their container. That shouldn't happen. We set both boxes to have a height of 50% of their parent container. That means that, together, the height of the two child elements should add up to 100% of the parent's height, not more or less.&lt;/p&gt;

&lt;p&gt;This is where we can look under the hood to see what's going on. Right-click somewhere on the web page and select "Inspect" from the options. The dev tools will appear on the side of the page.&lt;/p&gt;

&lt;p&gt;Here, I am going to use the Google Chrome dev tools since Chrome is the browser I use. From the Chrome dev tools, locate the menu bar with the following tabs: "Styles," "Computed," Layout," and so on. Click the "Computed" tab to see the box model.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F43ty2tx6fohyeafhd2zi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F43ty2tx6fohyeafhd2zi.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The values in the box model update when you select an element from the Elements window. For example, here I clicked the container &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; and the box model updated to show that it's content box is 500x500. It also shows that it has a margin of 25px on each side, exactly what we defined earlier. Looks good. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1hgt313kg8osi5bup524.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1hgt313kg8osi5bup524.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Additionally, when you hover over an element in the elements window, the element is highlighted in the webpage and you can see some other information about it. Here, the window tells us the element is 500 x 500, exactly as it appears in the box model.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg6b5a3fh5orrlsuilvmc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg6b5a3fh5orrlsuilvmc.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This means that if we click on Box 1 or Box 2, the box model should say the elements are each 250x250 with a padding of 15px all around. And that's exactly what we see when we click Box 1.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flrirb7opkiun8dx9zkrk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flrirb7opkiun8dx9zkrk.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But if we hover over Box 1 in the Elements window and look at the webpage, we'll see that it says the element is 280 x 280. Hover over Box 2 shows the same thing.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi8lwmmjpc5wfpjupcrnj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi8lwmmjpc5wfpjupcrnj.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;How is that possible?&lt;/p&gt;

&lt;p&gt;Well, when you set the width and height of an element, you're applying those properties to the content box only. In order to get the &lt;strong&gt;full&lt;/strong&gt; height of the box, we need to add up the borders, padding, and content heights.&lt;/p&gt;

&lt;p&gt;In our case, we have 0px for the top and bottom margins, 15px for both top and bottom paddings, and 250px for the element height:&lt;/p&gt;

&lt;p&gt;0 + 15px + 250px + 150px + 0 = 280px total height&lt;/p&gt;

&lt;p&gt;The same logic applies to the height. Notice that the margin does not count in our calculation of the total width and height of our box. This is because margin lies outside of our box.&lt;/p&gt;

&lt;p&gt;So this is why Box 2 is overflowing. Box boxes have a combined height of 560px which is taller than the parent container which has a height of 500px.&lt;/p&gt;

&lt;p&gt;What if there was way to tell CSS to apply a specific width and height to the entire box (border, padding, and content) instead of just the content box?&lt;/p&gt;

&lt;p&gt;Well, there is!&lt;/p&gt;

&lt;p&gt;There is a CSS property called &lt;code&gt;box-sizing&lt;/code&gt;. The default value of &lt;code&gt;box-sizing&lt;/code&gt; is &lt;code&gt;content-box&lt;/code&gt;, which tells CSS to apply the width and height to the content box only.&lt;/p&gt;

&lt;p&gt;We can set &lt;code&gt;box-sizing&lt;/code&gt; to &lt;code&gt;border-box&lt;/code&gt; to tell CSS to apply the width and height to the content box, padding box, and border box combined. When we change the property to &lt;code&gt;border-box&lt;/code&gt;, we are using the alternate box model. In my opinion, it's much easier to work with the alternate box model.&lt;/p&gt;

&lt;p&gt;Applying this small change to our CSS fixes the two boxes and makes them take up 50% of the parent container as we expected earlier.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.box-1, .box-2 {
  width: 50%;
  height: 50%;
  padding: 15px
  box-sizing: border-box;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fygh97oer33itp66kpqgq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fygh97oer33itp66kpqgq.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The box model in the dev tool now says the content box of Box 1 is 220 x 220. CSS automatically adjusted the length and width of the content box so that everything inside could add up to 50% of the width of the parent container:&lt;/p&gt;

&lt;p&gt;border top + padding top + content height + padding bottom + border bottom = total box height.&lt;/p&gt;

&lt;p&gt;So, 0 + 15px + 220px + 15px + 0 = 250px total height (which is 50% of the parent container's height)&lt;/p&gt;

&lt;h3&gt;
  
  
  Collapsing margin
&lt;/h3&gt;

&lt;p&gt;The second strange behavior is one that I've encountered a few times.&lt;/p&gt;

&lt;p&gt;Let's start with a new, simple HTML document:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="container"&amp;gt;
  &amp;lt;h1&amp;gt;Hello dev.to&amp;lt;/h1&amp;gt;
  &amp;lt;p&amp;gt;Lorem ipsum...&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We add some styles to the container:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.container {
  background-color: #333333;
  max-width: 500px;
  margin: 35px auto;
  color: white;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the result should look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcid91nxps7g728ea8w47.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcid91nxps7g728ea8w47.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I don't like how the title is right up against the edge of its element, so let's add some margin to push it down a bit.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;h1 {
  margin-top: 25px;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hmm. Nothing seemed to change.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fesaidilys7t01d1c9zrk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fesaidilys7t01d1c9zrk.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Time to check the trusty box model in the dev tools.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmr1mviceckade1ebw2qg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmr1mviceckade1ebw2qg.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The box model shows that the top margin of 25px we add is there. So what's going on? Well, when I hover over the &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt; element in the Elements window, I can see that the top margin is actually spilling out of the container box.&lt;/p&gt;

&lt;p&gt;If I was working on a project, my next step would be to go to Google and search something like "CSS h1 element margin is outside the container box". That search lead me to this &lt;a href="https://stackoverflow.com/questions/6929874/item-with-margin-top-has-margin-outside-of-containing-box" rel="noopener noreferrer"&gt;stackoverflow question&lt;/a&gt;, which let me to this &lt;a href="https://www.sitepoint.com/collapsing-margins/" rel="noopener noreferrer"&gt;sitepoint article&lt;/a&gt; that talks about collapsing margins.&lt;/p&gt;

&lt;p&gt;In short, the article explains that when two elements are touching margins, only the larger margin is applied. The smaller margin collapses into the larger margin. "Touching margins" means there is no border, padding, or content between the two margins.&lt;/p&gt;

&lt;p&gt;For example, imagine there are two sibling &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; elements in a page. You add margin-bottom of 25px to the top &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt;, and margin-top of 50px to the bottom &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt;. If the two margins touch, they will collapse into each other. So instead of the elements being 75px (25px + 50px) apart, they are only 50px apart.&lt;/p&gt;

&lt;p&gt;This behavior also happens with margins of parent and child elements, like in our example. Here, the margin of the &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt; header is collapsing with the margin of the container &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; because they are touching. So how do we stop them from touching?&lt;/p&gt;

&lt;p&gt;We can add a border or padding to the parent container:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.container {
  background-color: #333333;
  max-width: 500px;
  margin: 35px auto;
  color: white;
  border-top: 1px solid #333333;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since the container &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; now has a border, the margins will no longer touch and collapse. The margin-top of the &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt; now appears inside the container:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyggo033aacf9qhdgme70.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyggo033aacf9qhdgme70.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Now what?
&lt;/h2&gt;

&lt;p&gt;In this blog post, we talked about the box model. We talked about what it is and how to use it to investigate weird CSS behaviors.&lt;/p&gt;

&lt;p&gt;In fact, these 'weird' behaviors are part of how CSS works. As you search for solutions to these behaviors, you'll learn that they were intentionally setup that way. CSS will make more sense to you as you encounter these behaviors and learn why they do what they do.&lt;/p&gt;

&lt;p&gt;When you encounter a 'weird' behavior in your element layouts, try using the box model tool first to investigate the problem. It'll help you arrive at solutions faster.&lt;/p&gt;

&lt;h2&gt;
  
  
  Keep me honest
&lt;/h2&gt;

&lt;p&gt;See something that is incorrect? Let me know in the comments and I'll fix it. Thanks!&lt;/p&gt;




&lt;p&gt;If you made it all the way down here, you might as well hit that Follow button and check me out on &lt;a href="https://twitter.com/jesus_codes" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>html</category>
      <category>css</category>
      <category>webdev</category>
    </item>
    <item>
      <title>.NET Core vs .NET Framework vs .NET Standard vs .NET vs ASP.NET</title>
      <dc:creator>Jesus Perez</dc:creator>
      <pubDate>Wed, 08 Jun 2022 14:59:39 +0000</pubDate>
      <link>https://forem.com/jesushperez/net-core-vs-net-framework-vs-net-standard-vs-net-vs-aspnet-1okj</link>
      <guid>https://forem.com/jesushperez/net-core-vs-net-framework-vs-net-standard-vs-net-vs-aspnet-1okj</guid>
      <description>&lt;p&gt;One of the most frustrating aspects of learning .NET is navigating the confusing naming convention of the .NET ecosystem. In this blog post, I will explain the difference between all these .NET tools.&lt;/p&gt;




&lt;p&gt;A few weeks ago, I picked up a C# &amp;amp; .NET book because I wanted to learn .NET for backend web development. The book uses console and WPF apps to teach C#. &lt;/p&gt;

&lt;p&gt;After a few chapters, I realized this book wasn’t going to teach me how to make web applications. So I decided to do a search to see what I needed to use in order to create web apps with .NET. This is how I discovered the larger (and confusing) world of .NET.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is .NET?
&lt;/h2&gt;

&lt;p&gt;In order to understand the difference between all these .NET tools, it’s important to understand what .NET is. Microsoft calls it a development platform. &lt;/p&gt;

&lt;p&gt;Essentially, .NET consists of the common language runtime (CLR) and the collection of class libraries that make it easier to develop all sorts of applications. &lt;/p&gt;

&lt;p&gt;The CLR performs various tasks in the background. For example, it translates your compiled C# code into code that your machine can understand. The CLR performs other important tasks in the background, but that’s outside the scope of this article.&lt;/p&gt;

&lt;p&gt;The class libraries that are part of .NET are created by Microsoft devs (and now the open source community) to make development of applications easier and faster. Again, this is outside the code of the current article.&lt;/p&gt;

&lt;p&gt;The important thing to understand is that .NET consists of the CLR and the class libraries. It is not a language. Developers write code in C#, F#, or Visual Basic to use the .NET tools and libraries.&lt;/p&gt;

&lt;h2&gt;
  
  
  .NET Framework vs .NET Core vs .NET
&lt;/h2&gt;

&lt;p&gt;The original .NET implementation was introduced in the early 2000’s. Its tools expanded over the years, but it was a platform for creating and running applications on Windows only. &lt;/p&gt;

&lt;p&gt;A new .NET implementation was released In 2016. It got rid of the bloated code from the original .NET implementation AND introduced a cross-platform implementation for running applications on Windows, Linux, and MacOS. &lt;/p&gt;

&lt;p&gt;To distinguish the two .NET implementations, the newer cross-platform implementation was called &lt;strong&gt;.NET Core&lt;/strong&gt;, and the older Windows-only implementation was rebranded as &lt;strong&gt;.NET Framework&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Developers can still use .NET Framework to create Windows-only applications. However, .NET Core is the future of the .NET platform. In fact, since the release of .NET Core 5, Microsoft no longer uses “Core” to distinguish between .NET Framework and .NET Core. .NET Core is now simply &lt;strong&gt;.NET&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Additionally, since .NET 5, Microsoft is planning to release new versions of .NET every year (currently in .NET 6 as of writing this blog). This new .NET has unified the various implementations of .NET. But what does that mean, exactly? Let’s talk a bit about the .NET Standard first, and it’ll make more sense later.&lt;/p&gt;

&lt;h2&gt;
  
  
  .NET Standard
&lt;/h2&gt;

&lt;p&gt;When .NET Core was first introduced, some but not all code was compatible with .NET Framework. In order to resolve compatibility issues, Microsoft implemented the .NET Standard.&lt;/p&gt;

&lt;p&gt;Developers who create class libraries for .NET write code targeting specific versions of the .NET Standard instead of specific versions of .NET Framework or .NET Core. &lt;/p&gt;

&lt;p&gt;For example, writing a library for .NET Standard version 1.4 will guarantee that the library works in the following versions of .NET Core, .NET Framework, and the various other .NET implementations:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---7fBp0Vn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4zcureua09krpmunpw19.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---7fBp0Vn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4zcureua09krpmunpw19.jpg" alt="Image description" width="787" height="422"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;.NET Standard 2.1 is the latest and &lt;strong&gt;final release&lt;/strong&gt; of the .NET Standard. As you can see below, it does not support .NET Framework. However, devs can still release libraries targeting previous versions of the .NET Standard if they wish to support .NET Framework.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VRVpuEYV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tmob7hxqhsn4blm8om65.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VRVpuEYV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tmob7hxqhsn4blm8om65.jpg" alt="Image description" width="782" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;.NET 5+ versions are unifying various of the .NET implementations listed above. This makes it easier for developers to create .NET libraries without having to worry about which versions of which implementations are supported.&lt;/p&gt;

&lt;p&gt;For example, developers can now create a .NET 6 library and know that it can be used in various types of applications being created with .NET 6. In essence, .NET 5+ has moved beyond the .NET Standard. That’s why there will no longer be .NET Standard releases.&lt;/p&gt;

&lt;p&gt;Still, if developers want, they can still create libraries targeting any of the previous versions of the .NET Standard, just like they can keep using the .NET Framework.&lt;/p&gt;

&lt;h2&gt;
  
  
  ASP.NET
&lt;/h2&gt;

&lt;p&gt;This is the .NET tool used for creating web applications. &lt;/p&gt;

&lt;p&gt;There are two versions of the tool. The older version used in the .NET Framework is called &lt;strong&gt;ASP.NET&lt;/strong&gt;. The newer version introduced in .NET Core is called &lt;strong&gt;ASP.NET Core&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Even though .NET Core is simply .NET now, ASP.NET Core still uses the word “Core” to distinguish it from the older version of ASP.NET.&lt;/p&gt;

&lt;h2&gt;
  
  
  Keep me honest!
&lt;/h2&gt;

&lt;p&gt;If you see anything that’s wrong or sounds confusing/incorrect, please let me know in the comments.&lt;/p&gt;

</description>
      <category>dotnet</category>
    </item>
  </channel>
</rss>
