{"id":3716,"date":"2024-09-02T13:28:41","date_gmt":"2024-09-02T18:28:41","guid":{"rendered":"https:\/\/frontendmasters.com\/blog\/?p=3716"},"modified":"2024-09-02T13:28:42","modified_gmt":"2024-09-02T18:28:42","slug":"backgrounds-for-the-box-model-and-why-it-can-be-useful","status":"publish","type":"post","link":"https:\/\/frontendmasters.com\/blog\/backgrounds-for-the-box-model-and-why-it-can-be-useful\/","title":{"rendered":"Backgrounds for the Box Model (and why it can be useful)"},"content":{"rendered":"\n<p>I had someone write to me the other day that they were trying to visualize the different parts of the box model. They said they would just code up an example themselves, but there is no way to, say, set a background color for <em>just<\/em> the &#8220;padding&#8221; area of an element. <\/p>\n\n\n\n<p>What they were talking about is something like this, the box model diagram available in DevTools:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"594\" height=\"366\" src=\"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/09\/box-model.png?resize=594%2C366&#038;ssl=1\" alt=\"\" class=\"wp-image-3717\" style=\"width:427px;height:auto\" srcset=\"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/09\/box-model.png?w=594&amp;ssl=1 594w, https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/09\/box-model.png?resize=300%2C185&amp;ssl=1 300w\" sizes=\"auto, (max-width: 594px) 100vw, 594px\" \/><figcaption class=\"wp-element-caption\">Box Model in Chrome DevTools<\/figcaption><\/figure>\n<\/div>\n\n\n<p>I said, I think you&#8230; can! actually! <\/p>\n\n\n\n<p>The trick lays in using the <code>background-clip<\/code> property in CSS, which allows you to stop the <code>background-image<\/code> property at different points in the box model. Here&#8217;s a really basic example of doing that, using the same colors as the model above:<\/p>\n\n\n\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_OJeBjGZ\" src=\"\/\/codepen.io\/anon\/embed\/OJeBjGZ?height=450&amp;theme-id=47434&amp;slug-hash=OJeBjGZ&amp;default-tab=css,result\" height=\"450\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed OJeBjGZ\" title=\"CodePen Embed OJeBjGZ\" class=\"cp_embed_iframe\" style=\"width:100%;overflow:hidden\">CodePen Embed Fallback<\/iframe><\/div>\n\n\n\n<p>The only one I&#8217;m &#8220;faking&#8221; there is the very outer shape (margin), which is drawn with an <code>outline<\/code>. It does match the value of <code>margin<\/code> though so it&#8217;s basically real!<\/p>\n\n\n\n<p>I was a bit inspired to keep going, making all the values dynamic, so I made this, which labels the section as well.<\/p>\n\n\n\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_gONzpYb\" src=\"\/\/codepen.io\/anon\/embed\/gONzpYb?height=850&amp;theme-id=47434&amp;slug-hash=gONzpYb&amp;default-tab=result\" height=\"850\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed gONzpYb\" title=\"CodePen Embed gONzpYb\" class=\"cp_embed_iframe\" style=\"width:100%;overflow:hidden\">CodePen Embed Fallback<\/iframe><\/div>\n\n\n\n<p>I put a grid of content around it so that you could see the element push others away like the real box model does. <\/p>\n\n\n\n<p>Is this actually useful ever? Sometimes!<\/p>\n\n\n\n<p>Just the other day Wes Bos posted a really nice button effect and then re-created in HTML and CSS:<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-rich is-provider-twitter wp-block-embed-twitter\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"twitter-tweet\" data-width=\"500\" data-dnt=\"true\"><p lang=\"en\" dir=\"ltr\">Man I love this cool effect with CSS Conic gradients! <a href=\"https:\/\/t.co\/t25ELxOrc1\">pic.twitter.com\/t25ELxOrc1<\/a><\/p>&mdash; Wes Bos (@wesbos) <a href=\"https:\/\/twitter.com\/wesbos\/status\/1828528779544719707?ref_src=twsrc%5Etfw\">August 27, 2024<\/a><\/blockquote><script async src=\"https:\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script>\n<\/div><\/figure>\n\n\n\n<p>There is no real big problem with how Wes did it there, but he did use an extra element as well as pseudo elements to get it done. <a href=\"https:\/\/codepen.io\/wesbos\/pen\/PoraMVV\">Here&#8217;s his demo.<\/a> <\/p>\n\n\n\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_PoraMVV\" src=\"\/\/codepen.io\/anon\/embed\/PoraMVV?height=450&amp;theme-id=47434&amp;slug-hash=PoraMVV&amp;default-tab=html,result\" height=\"450\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed PoraMVV\" title=\"CodePen Embed PoraMVV\" class=\"cp_embed_iframe\" style=\"width:100%;overflow:hidden\">CodePen Embed Fallback<\/iframe><\/div>\n\n\n\n<p>Ana Tudor <a href=\"https:\/\/twitter.com\/anatudor\/status\/1828680421078421627\">chimed in<\/a> that it could be done without using any extra elements, and guess how? Background clipping! <\/p>\n\n\n\n<p>I took a crack at that, and aside from a bit of an awkward need to set the <code>background-size<\/code>, I think I got it working:<\/p>\n\n\n\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_JjQmrYE\" src=\"\/\/codepen.io\/anon\/embed\/JjQmrYE?height=450&amp;theme-id=47434&amp;slug-hash=JjQmrYE&amp;default-tab=html,result\" height=\"450\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed JjQmrYE\" title=\"CodePen Embed JjQmrYE\" class=\"cp_embed_iframe\" style=\"width:100%;overflow:hidden\">CodePen Embed Fallback<\/iframe><\/div>\n\n\n\n<p>This may actually be truly useful sometimes. For example, a CMS that is happy to spit out a <code>&lt;button><\/code> element but doesn&#8217;t offer inner HTML control of it. If you don&#8217;t need that anyway and can pull of the effect in CSS entirely, you&#8217;re in business. <\/p>\n\n\n\n<p>Because I&#8217;m a glutton for punishment sometimes, I also decided to have a crack at it using <code>border-image<\/code> as well, which despite being the lest scrutable CSS property of all time, seems at least conceptually like the correct thing to use. I was able to get it working! <\/p>\n\n\n\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_MWMPwVe\" src=\"\/\/codepen.io\/anon\/embed\/MWMPwVe?height=450&amp;theme-id=47434&amp;slug-hash=MWMPwVe&amp;default-tab=html,result\" height=\"450\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed MWMPwVe\" title=\"CodePen Embed MWMPwVe\" class=\"cp_embed_iframe\" style=\"width:100%;overflow:hidden\">CodePen Embed Fallback<\/iframe><\/div>\n\n\n\n<p>But for whatever reason <code>border-image<\/code> is incompatible with <code>border-radius<\/code>, so, whatever, that sucks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can limit how far the background-image of an element applies by using background-clip. That means you can apply *different* backgrounds to, say, the padding and border.<\/p>\n","protected":false},"author":1,"featured_media":3723,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"sig_custom_text":"","sig_image_type":"featured-image","sig_custom_image":0,"sig_is_disabled":false,"inline_featured_image":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[234,233,7],"class_list":["post-3716","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog-post","tag-background-clip","tag-box-model","tag-css"],"acf":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/09\/shiny-button.png?fit=736%2C526&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/3716","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/comments?post=3716"}],"version-history":[{"count":6,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/3716\/revisions"}],"predecessor-version":[{"id":3724,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/3716\/revisions\/3724"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/media\/3723"}],"wp:attachment":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/media?parent=3716"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/categories?post=3716"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/tags?post=3716"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}