{"id":1192,"date":"2020-01-26T22:50:52","date_gmt":"2020-01-27T03:50:52","guid":{"rendered":"http:\/\/newmediaproduction.mynmi.net\/?page_id=1192"},"modified":"2023-08-31T16:01:38","modified_gmt":"2023-08-31T16:01:38","slug":"css-box-model","status":"publish","type":"page","link":"https:\/\/nmi.cool\/web\/css-box-model\/","title":{"rendered":"CSS Box Model"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"2560\" height=\"1707\" src=\"https:\/\/nmi.cool\/newmediaproduction\/wp-content\/uploads\/sites\/3\/2020\/01\/kelli-mcclintock-GopRYASfsOc-unsplash-scaled-1.jpg\" alt=\"A white box sitting on a table. \" class=\"wp-image-1215\" srcset=\"https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/01\/kelli-mcclintock-GopRYASfsOc-unsplash-scaled-1.jpg 2560w, https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/01\/kelli-mcclintock-GopRYASfsOc-unsplash-scaled-1-300x200.jpg 300w, https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/01\/kelli-mcclintock-GopRYASfsOc-unsplash-scaled-1-1024x683.jpg 1024w, https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/01\/kelli-mcclintock-GopRYASfsOc-unsplash-scaled-1-768x512.jpg 768w, https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/01\/kelli-mcclintock-GopRYASfsOc-unsplash-scaled-1-1536x1024.jpg 1536w, https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/01\/kelli-mcclintock-GopRYASfsOc-unsplash-scaled-1-2048x1366.jpg 2048w, https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/01\/kelli-mcclintock-GopRYASfsOc-unsplash-scaled-1-1568x1046.jpg 1568w\" sizes=\"auto, (max-width: 2560px) 100vw, 2560px\" \/><figcaption class=\"wp-element-caption\">Photo by&nbsp;<a href=\"https:\/\/unsplash.com\/@kelli_mcclintock?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText\">Kelli McClintock<\/a>.<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">So what is the CSS Box Model?<\/h2>\n\n\n\n<p>You might have noticed that CSS treats HTML elements as if they are enclosed in rectangles or boxes. In the example below, the <code>background-color<\/code> CSS declaration helps illustrate the point.  CSS is applied to the HTML file as if all the text were enclosed in a rectangle or a box. <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"541\" src=\"https:\/\/nmi.cool\/newmediaproduction\/wp-content\/uploads\/sites\/3\/2020\/01\/Screen-Shot-2020-01-26-at-9.24.01-PM-1024x541.png\" alt=\"\" class=\"wp-image-1212\" srcset=\"https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/01\/Screen-Shot-2020-01-26-at-9.24.01-PM-1024x541.png 1024w, https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/01\/Screen-Shot-2020-01-26-at-9.24.01-PM-300x159.png 300w, https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/01\/Screen-Shot-2020-01-26-at-9.24.01-PM-768x406.png 768w, https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/01\/Screen-Shot-2020-01-26-at-9.24.01-PM-1536x812.png 1536w, https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/01\/Screen-Shot-2020-01-26-at-9.24.01-PM-1568x829.png 1568w, https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/01\/Screen-Shot-2020-01-26-at-9.24.01-PM.png 1986w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">A basic webpage with &lt;h1&gt;, &lt;h2&gt;, and &lt;p&gt; elements styled with background colors.<\/figcaption><\/figure>\n\n\n\n<p>It&#8217;s important to understand that these boxes exist because they allow us to use CSS to alter the layout of our website. We can actually move these boxes around on the page instead of just having everything stacked on top of each other. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Content, Padding, Borders, and Margins<\/h2>\n\n\n\n<p>Additionally, these boxes have four specific properties that help define how they look and interact with other boxes. These include <strong>content<\/strong>, <strong>padding<\/strong>, <strong>borders<\/strong>, and <strong>margins<\/strong>. Here&#8217;s a handy diagram to illustrate the point: <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1050\" height=\"778\" src=\"https:\/\/nmi.cool\/newmediaproduction\/wp-content\/uploads\/sites\/3\/2020\/01\/Screen-Shot-2020-01-26-at-10.41.24-PM.png\" alt=\"A diagram of the CSS box model, including content, padding, border, and margin. \" class=\"wp-image-1216\" srcset=\"https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/01\/Screen-Shot-2020-01-26-at-10.41.24-PM.png 1050w, https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/01\/Screen-Shot-2020-01-26-at-10.41.24-PM-300x222.png 300w, https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/01\/Screen-Shot-2020-01-26-at-10.41.24-PM-1024x759.png 1024w, https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/01\/Screen-Shot-2020-01-26-at-10.41.24-PM-768x569.png 768w\" sizes=\"auto, (max-width: 1050px) 100vw, 1050px\" \/><figcaption class=\"wp-element-caption\">A diagram of the CSS box model from <a href=\"https:\/\/internetingishard.com\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">internetingishard.com<\/a><\/figcaption><\/figure>\n\n\n\n<p>The <strong>content<\/strong> is simply the text or media that appears within the element. The <code>&lt;h1&gt;<\/code> in our first example has &#8220;CSS Box Model&#8221; as the content. The words &#8220;Red&#8221; and &#8220;Blue&#8221; are also content. <\/p>\n\n\n\n<p><strong>Padding<\/strong> is the space between the content and the border. It &#8220;pads&#8221; the content from the border kind of like how bubble wrap protects a fragile item inside a shipping box. <\/p>\n\n\n\n<p>The <strong>border<\/strong> is nothing more than a line enclosing the padding and the content. We&#8217;ll look at an example in a minute. <\/p>\n\n\n\n<p>Lastly, the <strong>margin<\/strong> exists outside of the border, and is the space between two boxes.  <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1932\" height=\"1018\" src=\"https:\/\/nmi.cool\/newmediaproduction\/wp-content\/uploads\/sites\/3\/2020\/01\/Screen-Shot-2020-01-26-at-9.29.21-PM.png\" alt=\"In this example, the h1 element, which reads CSS Box Model has been altered. It now has 50px of padding and a light blue border. \" class=\"wp-image-1211\" srcset=\"https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/01\/Screen-Shot-2020-01-26-at-9.29.21-PM.png 1932w, https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/01\/Screen-Shot-2020-01-26-at-9.29.21-PM-300x158.png 300w, https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/01\/Screen-Shot-2020-01-26-at-9.29.21-PM-1024x540.png 1024w, https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/01\/Screen-Shot-2020-01-26-at-9.29.21-PM-768x405.png 768w, https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/01\/Screen-Shot-2020-01-26-at-9.29.21-PM-1536x809.png 1536w, https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/01\/Screen-Shot-2020-01-26-at-9.29.21-PM-1568x826.png 1568w\" sizes=\"auto, (max-width: 1932px) 100vw, 1932px\" \/><figcaption class=\"wp-element-caption\">Padding and a border have been added to our <code>&lt;h1&gt;<\/code> element. <\/figcaption><\/figure>\n\n\n\n<p>In this example, we have added 50px of <strong>padding<\/strong> to the first box. We have also added a light blue <strong>border<\/strong> around the box. <\/p>\n\n\n\n<p>What&#8217;s important to note here is that the added space created by the <strong>padding<\/strong> surrounds the <strong>content<\/strong> and is inside the <strong>border<\/strong>. It&#8217;s also helpful to note that the <strong>padding<\/strong> retained the purple background color. <\/p>\n\n\n\n<p>The <strong>margin<\/strong> here is the white <a href=\"#footnote-1-1192\" id=\"note-1-1192\" rel=\"footnote\">1<\/a> space around the block that separates it from the other blocks. It&#8217;s outside of the border. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Styling Padding and Margins<\/h2>\n\n\n\n<p>To add padding or a margin to your block elements you can either apply it to the entire element like this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> p {\n    padding: 50px;\n}<\/pre>\n\n\n\n<p>Or you can add a padding or margin amount to each side of the block individually. This is especially handy if you want different amounts of space on certain sides. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">p {\n    margin-top: 30px;\n    margin-right: 15px;\n    margin-bottom: 35px;\n    margin-left: 10px;\n  }<\/pre>\n\n\n\n<p>There&#8217;s even a shortcut to set up your padding and margin amounts more quickly. You can actually omit the top, right, bottom, left so that your CSS looks like this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> p {\n      margin: 30px 15px 35px 10px;\n  }<\/pre>\n\n\n\n<p>It&#8217;s important to note that the four values are <strong>interpreted clockwise<\/strong>, starting with top, then right, then bottom, then left. If this feels like it might be hard to remember you can always stick with the first method of just writing the full declaration out. The results will be the same. <\/p>\n\n\n\n<p>You can also use this shorthand for just two values:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">p {\n    padding: 15px 30px;\n  }<\/pre>\n\n\n\n<p>In this case, <strong>when only two values are provided<\/strong>, the first value (<code>15px<\/code>) will be applied to the top and bottom and the second value (<code>30px<\/code>) will be applied to the right and left. Again, it&#8217;s your call if you want to use this shorthand! <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Styling Borders<\/h2>\n\n\n\n<p>Styling a border is pretty easy. You can pick the color, weight, and style of the line. What do I mean by the &#8220;style&#8221; of the line? You can decide if you want your line to be solid, dotted, dashed, or a number of <a href=\"https:\/\/www.w3schools.com\/css\/css_border.asp\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">other properties<\/a>. You can set these properties individually like this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">h1 {\n    border-color: magenta;\n    border-width: 4px;\n    border-style: solid;\n  }<\/pre>\n\n\n\n<p>Or you can use some shorthand and include them all in one declaration: <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">h1 {\n      border: magenta 4px solid;\n  }<\/pre>\n\n\n\n<p>Be aware that when you combine the styles into a single line they no longer need semicolons between them; in fact, they should just have spaces. <\/p>\n\n\n\n<p>Another fun trick with borders is to give them <strong>rounded edges<\/strong>. Check out how to add a <a href=\"https:\/\/www.w3schools.com\/cssref\/css3_pr_border-radius.asp\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\"><code>border-radius<\/code> here<\/a>. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Your Mission<\/h2>\n\n\n\n<p>The CSS box model is important to understand; it&#8217;s a powerful tool that lets us modify the whole layout of our website. To fully understand the box model concept and to get some good practice implementing it, check out the CSS Box Model Tutorial on <a href=\"https:\/\/internetingishard.netlify.app\/html-and-css\/css-box-model\/\" data-type=\"link\" data-id=\"https:\/\/internetingishard.netlify.app\/html-and-css\/css-box-model\/\">Interneting is Hard<\/a>. The direct link to the <a href=\"https:\/\/internetingishard.netlify.app\/html-and-css\/css-box-model\/\" data-type=\"link\" data-id=\"https:\/\/internetingishard.netlify.app\/html-and-css\/css-box-model\/\">tutorial<\/a> isn&#8217;t cooperating consistently, so it might be easiest if you visit <a href=\"https:\/\/internetingishard.netlify.app\/\" data-type=\"link\" data-id=\"https:\/\/internetingishard.netlify.app\/\">Interneting is Hard<\/a> and then click &#8220;HTML &amp; CSS Start Here.&#8221;<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"219\" height=\"221\" src=\"http:\/\/nmi.cool\/newmediaproduction\/wp-content\/uploads\/sites\/3\/2020\/09\/Screen-Shot-2020-09-14-at-10.21.06-PM.png\" alt=\"\" class=\"wp-image-2397\" srcset=\"https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/09\/Screen-Shot-2020-09-14-at-10.21.06-PM.png 219w, https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/09\/Screen-Shot-2020-09-14-at-10.21.06-PM-150x150.png 150w\" sizes=\"auto, (max-width: 219px) 100vw, 219px\" \/><figcaption class=\"wp-element-caption\">Click this link! <\/figcaption><\/figure>\n<\/div>\n\n\n<p>Then scroll on down until you see the Box Model and click that link. <\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"725\" height=\"273\" src=\"http:\/\/nmi.cool\/newmediaproduction\/wp-content\/uploads\/sites\/3\/2020\/09\/Screen-Shot-2020-09-14-at-10.22.01-PM.png\" alt=\"\" class=\"wp-image-2398\" srcset=\"https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/09\/Screen-Shot-2020-09-14-at-10.22.01-PM.png 725w, https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/09\/Screen-Shot-2020-09-14-at-10.22.01-PM-300x113.png 300w\" sizes=\"auto, (max-width: 725px) 100vw, 725px\" \/><figcaption class=\"wp-element-caption\">Click this link to access the tutorial! <\/figcaption><\/figure>\n<\/div>\n\n\n<p>There you go! This is a great tutorial because it asks you to follow along by creating your own HTML file and CSS style sheet. You&#8217;ll get the most out of it if you actually do the steps! <\/p>\n\n\n\n<p>But <strong>be aware<\/strong>! This tutorial references a code editor called Atom. This is not a big deal\u2014 there are many great code editors out there! When you see a reference to Atom, just mentally swap it out for Visual Studio Code and keep moving forward! <\/p>\n\n\n\n<p>When you finish the tutorial you should have a good understanding of how inline and block elements work<a href=\"#footnote-2-1192\" id=\"note-2-1192\" rel=\"footnote\">2<\/a>, and you should know how to modify padding, borders, and margins to effectively alter the layout of your html document. With these skills you&#8217;ll be well on your way to creating beautiful webpages!  <\/p>\n<div class=\"footnotes\"><hr \/><ol><li id=\"footnote-1-1192\" class=\"footnote\"><p> actually, it&#8217;s transparent! This can be helpful if your content is on top of an image or pattern<a href=\"#note-1-1192\" class=\"footnote-return\">&#8617;<\/a><\/p><\/li><!--\/#footnote-1.footnote--><li id=\"footnote-2-1192\" class=\"footnote\"><p>We talked about these briefly in a previous lesson. You can get a quick refresher on <a href=\"https:\/\/nmi.cool\/web\/intro-css\/\" target=\"_blank\" rel=\"noreferrer noopener\">inline and block elements<\/a> here<a href=\"#note-2-1192\" class=\"footnote-return\">&#8617;<\/a><\/p><\/li><!--\/#footnote-2.footnote--><\/ol><\/div><!--\/#footnotes-->","protected":false},"excerpt":{"rendered":"<p>So what is the CSS Box Model? You might have noticed that CSS treats HTML elements as if they are enclosed in rectangles or boxes. In the example below, the background-color CSS declaration helps illustrate the point. CSS is applied to the HTML file as if all the text were enclosed in a rectangle or &hellip; <a href=\"https:\/\/nmi.cool\/web\/css-box-model\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">CSS Box Model<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1192","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/nmi.cool\/web\/wp-json\/wp\/v2\/pages\/1192","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nmi.cool\/web\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/nmi.cool\/web\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/nmi.cool\/web\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nmi.cool\/web\/wp-json\/wp\/v2\/comments?post=1192"}],"version-history":[{"count":7,"href":"https:\/\/nmi.cool\/web\/wp-json\/wp\/v2\/pages\/1192\/revisions"}],"predecessor-version":[{"id":4404,"href":"https:\/\/nmi.cool\/web\/wp-json\/wp\/v2\/pages\/1192\/revisions\/4404"}],"wp:attachment":[{"href":"https:\/\/nmi.cool\/web\/wp-json\/wp\/v2\/media?parent=1192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}