{"id":2580,"date":"2020-10-26T04:05:23","date_gmt":"2020-10-26T09:05:23","guid":{"rendered":"https:\/\/cssnewbie.com\/?p=2580"},"modified":"2020-10-26T04:05:23","modified_gmt":"2020-10-26T09:05:23","slug":"how-to-add-css-box-model","status":"publish","type":"post","link":"https:\/\/cssdeck.com\/blog\/how-to-add-css-box-model\/","title":{"rendered":"How To Add CSS Box Model"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"size-large wp-image-2581\" src=\"https:\/\/cssnewbie.com\/wp-content\/uploads\/2020\/09\/43160-1024x819.jpg\" alt=\"CSS Box Model\" width=\"1024\" height=\"819\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>\u201cCSS Box Model\u201d is generally referred to as design &amp; layout in CSS. It is the box that can be imagined as concentric-rectangles which has the outer-most rectangle as margin. This rectangle is ordinarily transparent. The next rectangle inside represents a border which has a variable thickness and is filled with colors. Inside this, the third rectangle is of padding which is transparent too and the one in the center is the content that has been written inside the HTML tag.<\/p>\n<p>CSS Box Model is the most basic concept that should be learned by a developer to understand the elements of a page. According to <a href=\"https:\/\/www.w3.org\/TR\/CSS2\/box.html\">W3C<\/a>, the CSS box model forms the boxes that make the basic visual formatting of a web page.\u00a0The CSS Box Model <a href=\"https:\/\/www.techrepublic.com\/article\/make-better-web-pages-by-understanding-the-css-box-model\/\">underpins most of the layout and positioning<\/a> of a webpage.<\/p>\n<p>Let&#8217;s take a look at one such CSS Box Model<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"size-large wp-image-2582 aligncenter\" src=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2020\/09\/box-model-1024x570.png\" alt=\"CSS Box Model\" width=\"1024\" height=\"570\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>We will make our first sample box to have an understanding. Remember, we style the box in CSS. So have a look at the HTML code and go meticulously through the CSS code. The CSS code has several properties like <em>width, border, padding, margin-left &amp; margin-right. <\/em>Alter the values in it and understand the output using <a href=\"https:\/\/codepen.io\/pen\/\">CodePen<\/a>.<\/p>\n<p>Here&#8217;s the HTML code:<\/p>\n<pre>&lt;html&gt;\n&lt;head&gt;\n&lt;title&gt; How to add css box model &lt;\/title&gt;\n&lt;link rel= \"stylesheet\" href= \"samplebox.css\"&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;h2&gt; The Box Model &lt;\/h2&gt;\n&lt;p&gt; CSS Box Model added here is around the content inside the div tag. &lt;\/p&gt;\n&lt;div&gt; &lt;p&gt; I am wrapped inside with padding of 50px &amp; border 300px. &lt;\/p&gt;\n&lt;\/div&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n<p>CSS FileName: SampleBox.css<\/p>\n<pre>div {\nbackground-color: lightgrey;\nwidth: 300px;\nborder: 15px solid green;\npadding: 50px;\nmargin-left: 20px;\nmargin-right: auto;\n}<\/pre>\n<p>The output will look as follows:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"size-large wp-image-2583 aligncenter\" src=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2020\/09\/SampleBox-1024x268.png\" alt=\"CSS Box Model\" width=\"1024\" height=\"268\" \/><\/p>\n<h3>How To Center The Box Model<\/h3>\n<p>The sample box which we created above can be aligned to the center simply by adjusting the outer rectangle of our Box Model, i.e. the margin. So we can change the value of the margin in a manner the box shifts to the center of our webpage. Here I have set the value of <em>margin-right <\/em>to 400px which aligned the box to center, change those values &amp; follow the code for better clarity.<\/p>\n<p>Here&#8217;s the HTML code:<\/p>\n<pre>&lt;html&gt;\n  &lt;head&gt;\n    &lt;title&gt;How to add css box model&lt;\/title&gt;\n    &lt;link rel= \"stylesheet\" href= \"centerbox.css\"&gt;\n  &lt;\/head&gt;\n  &lt;body&gt;\n    &lt;h2&gt;\u00a0 The Box Model &lt;\/h2&gt;\n    &lt;p&gt; Altering the values in the properties to see how the output cahnges &lt;\/p&gt;\n    &lt;div&gt;&lt;p&gt; I am in the center of the webpage all thanks to margin-left &lt;\/p&gt;\n    &lt;\/div&gt;\n  &lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n<p>CSS File: Centerbox.css<\/p>\n<pre>div {\nbackground-color: powderblue;\nwidth: 400px;\nborder: 20px solid yellow;\npadding: 70px;\nmargin-left: 400px;\nmargin-right: auto;\n}<\/pre>\n<p>This is what the output will be:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"size-large wp-image-2584 aligncenter\" src=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2020\/09\/CenterBox-1024x282.png\" alt=\"CSS Box Model\" width=\"1024\" height=\"282\" \/><\/p>\n<p>&nbsp;<\/p>\n<h3>How To Create A Dashed Border &amp; Align The Box<\/h3>\n<p>The property <em>border<\/em> has several values, we can use\u00a0 \u201cdashed color\u201d with color being what we want and the dashes break the border. Learn using the code given below:<\/p>\n<p>HTML Code:<\/p>\n<pre>&lt;html&gt;\n  &lt;head&gt;\n    &lt;title&gt; How to add css box model &lt;\/title&gt;\n    &lt;link rel= \"stylesheet\" href= \"dashedbox.css\"&gt;\n  &lt;\/head&gt;\n\n  &lt;body&gt;\n    &lt;h2&gt; The Box Model &lt;\/h2&gt;\n    &lt;p&gt; Creating a different kind of border and aligning the box model differently.&lt;\/p&gt;\n    &lt;div&gt;&lt;p&gt; The dashed border is of the value for the property &lt;i&gt;border&lt;\/i&gt; &lt;\/p&gt;\n    &lt;\/div&gt;\n  &lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n<p>CSS File: Dashedbox.css<\/p>\n<pre>div {\nbackground-color: orange;\nwidth: 400px;\nborder: 5px dashed black;\npadding: 15px;\nmargin-left: 60%;\nmargin-right: auto;\n}\n\n<\/pre>\n<p>Here&#8217;s how the output will be:<\/p>\n<h3><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-large wp-image-2585\" src=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2020\/09\/Dashedbox-1024x267.png\" alt=\"CSS Box Model\" width=\"1024\" height=\"267\" \/><\/h3>\n<p>The following codes help us understand how altering the values can yield different outputs. Also, we can imply from the codes that<\/p>\n<p><strong>Total element width<\/strong> = width + left border + right border + left padding + right padding + left margin + right margin.<\/p>\n<p>And,<\/p>\n<p><strong>Total element height<\/strong> = height + top margin + bottom margin + padding + bottom padding + top border + bottom border.<\/p>\n<p>The calculation of total element width and height if known to us can be helpful while designing the webpage with specific alignments.<\/p>\n<h3>Conclusion<\/h3>\n<p>The beginning of the article introduced the concept of the Box Model and how to add it to our code. We also learned how to align the box model and add different types of borders to it. A novice developer should always start out by understanding the concept of CSS Box Model to create aesthetic looking web designs. To keep learning such CSS techniques, subscribe to our <a href=\"http:\/\/www.cssnewbie.com\">blog<\/a>!<\/p>\n<p>To know <a href=\"https:\/\/cssnewbie.com\/how-to-change-text-color-in-css\/#.X3H10WgzbIV\">how to change text color in CSS<\/a>, visit our previous blog!<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<div class=\"wp-socializer wpsr-share-icons \" data-lg-action=\"show\" data-sm-action=\"show\" data-sm-width=\"768\" ><h3>Share and Enjoy !<\/h3><div class=\"wpsr-si-inner\"><div class=\"wpsr-counter wpsrc-sz-32px\" style=\"color:#000\"><span class=\"scount\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook,twitter,linkedin,pinterest,print,pdf\">0<\/span><\/span><small class=\"stext\">Shares<\/small><\/div><div class=\"socializer sr-popup sr-32px sr-circle sr-opacity sr-pad sr-count-1 sr-count-1\"><span class=\"sr-facebook\"><a rel=\"nofollow\" href=\"https:\/\/www.facebook.com\/share.php?u=\" target=\"_blank\"  title=\"Share this on Facebook\"  style=\"color: #ffffff\" ><i class=\"fab fa-facebook-f\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-twitter\"><a rel=\"nofollow\" href=\"https:\/\/twitter.com\/intent\/tweet?text=%20-%20%20\" target=\"_blank\"  title=\"Tweet this !\"  style=\"color: #ffffff\" ><i class=\"fab fa-twitter\"><\/i><\/a><\/span>\n<span class=\"sr-linkedin\"><a rel=\"nofollow\" href=\"https:\/\/www.linkedin.com\/sharing\/share-offsite\/?url=\" target=\"_blank\"  title=\"Add this to LinkedIn\"  style=\"color: #ffffff\" ><i class=\"fab fa-linkedin-in\"><\/i><\/a><\/span>\n<span class=\"sr-pinterest\"><a rel=\"nofollow\" href=\"https:\/\/www.pinterest.com\/pin\/create\/button\/?url=&amp;media=&amp;description=\" target=\"_blank\"  title=\"Submit this to Pinterest\"  style=\"color: #ffffff\" data-pin-custom=\"true\"><i class=\"fab fa-pinterest\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"pinterest\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-print\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Print this article \"  style=\"color: #ffffff\" ><i class=\"fa fa-print\"><\/i><\/a><\/span>\n<span class=\"sr-pdf\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Convert to PDF\"  style=\"color: #ffffff\" ><i class=\"fa fa-file-pdf\"><\/i><\/a><\/span><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>&nbsp;<\/p>\n<\/p>\n<p>&nbsp;<\/p>\n<p>\u201cCSS Box Model\u201d is generally referred to as design &amp; layout in CSS. It is the box that can be imagined as concentric-rectangles which has the outer-most rectangle as margin. This rectangle is ordinarily transparent. The next rectangle inside represents [&#8230;]<\/p>\n<p><a class=\"more-link article\" href=\"https:\/\/cssdeck.com\/blog\/how-to-add-css-box-model\/\" title=\"Click to read 'How To Add CSS Box Model'\">Read Article<\/a><\/p>\n","protected":false},"author":18,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[169],"tags":[],"_links":{"self":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/2580"}],"collection":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/comments?post=2580"}],"version-history":[{"count":0,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/2580\/revisions"}],"wp:attachment":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media?parent=2580"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/categories?post=2580"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/tags?post=2580"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}