{"id":466,"date":"2010-06-21T23:41:02","date_gmt":"2010-06-22T07:41:02","guid":{"rendered":"http:\/\/www.stubbornella.org\/content\/?p=466"},"modified":"2010-06-21T23:41:02","modified_gmt":"2010-06-22T07:41:02","slug":"css-granularity-architecture","status":"publish","type":"post","link":"https:\/\/www.stubbornella.org\/2010\/06\/21\/css-granularity-architecture\/","title":{"rendered":"How to create CSS objects? Get the granularity right!"},"content":{"rendered":"<p>In a <a href=\"http:\/\/www.stubbornella.org\/content\/2010\/06\/12\/visual-semantics-in-html-and-css\/\">previous post<\/a>, I said:<\/p>\n<blockquote><p>Shoehorning CSS and HTML into PHP abstractions prevents the code from being DRY and ultimately leads to code bloat, because, the CSS and HTML require a far more granular object structure than the PHP.<\/p><\/blockquote>\n<p>And then I didn&#8217;t expand on it, or give proper context. Ooops, sorry!<\/p>\n<h3>Correct granularity is one of the keys to tiny CSS<\/h3>\n<p>In the PHP layer, the following stream story (from facebook) might be a single object. All of the logic of determining which bits of HTML to show or hide are contained within one object structure. <\/p>\n<figure id=\"attachment_467\" aria-describedby=\"caption-attachment-467\" style=\"width: 477px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.stubbornella.org\/wp-content\/uploads\/2010\/06\/Facebook-stream-story.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.stubbornella.org\/wp-content\/uploads\/2010\/06\/Facebook-stream-story.png\" alt=\"An example of a stream story on the facebook website\" title=\"Facebook stream story\" width=\"477\" height=\"213\" class=\"size-full wp-image-467\" srcset=\"https:\/\/www.stubbornella.org\/wp-content\/uploads\/2010\/06\/Facebook-stream-story.png 477w, https:\/\/www.stubbornella.org\/wp-content\/uploads\/2010\/06\/Facebook-stream-story-300x134.png 300w\" sizes=\"auto, (max-width: 477px) 100vw, 477px\" \/><\/a><figcaption id=\"caption-attachment-467\" class=\"wp-caption-text\">A Facebook stream story<\/figcaption><\/figure>\n<p>Frequently, developers try to make the CSS match this middle-end logic. We expect all the CSS code for the stream story to be sand-boxed within a wrapper id, for example #story. This ends up with bloated code that isn&#8217;t DRY. Why? because the proper architecture for the CSS layer is much more granular. Trying to base the CSS architecture on the PHP layer is something akin to trying to use the DB schema to configure apache. It just doesn&#8217;t work. <\/p>\n<figure id=\"attachment_477\" aria-describedby=\"caption-attachment-477\" style=\"width: 494px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.stubbornella.org\/wp-content\/uploads\/2010\/06\/smaller_objects.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.stubbornella.org\/wp-content\/uploads\/2010\/06\/smaller_objects.png\" alt=\"stream story broken down\" title=\"smaller_objects\" width=\"494\" height=\"216\" class=\"size-full wp-image-477\" srcset=\"https:\/\/www.stubbornella.org\/wp-content\/uploads\/2010\/06\/smaller_objects.png 494w, https:\/\/www.stubbornella.org\/wp-content\/uploads\/2010\/06\/smaller_objects-300x131.png 300w\" sizes=\"auto, (max-width: 494px) 100vw, 494px\" \/><\/a><figcaption id=\"caption-attachment-477\" class=\"wp-caption-text\">The stream story is composed of many smaller (more granular) objects<\/figcaption><\/figure>\n<p>So we can see that the stream story example above is composed of many more granular objects. As we make the objects more granular, we begin to see the same patterns emerge over and over, and the same objects become much more reusable. We start to see that most sites on the interwebs are composed of the same basic repeating patterns combined in different ways. As a side benefit, the CSS gets *much* smaller and simpler. <\/p>\n<p>The stream story, one PHP object, is composed of several HTML &#038; CSS objects:<\/p>\n<h3>1 heading <\/h3>\n<p><a href=\"https:\/\/www.stubbornella.org\/wp-content\/uploads\/2010\/06\/heading.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.stubbornella.org\/wp-content\/uploads\/2010\/06\/heading.png\" alt=\"Heading\" title=\"heading\" width=\"416\" height=\"33\" class=\"alignnone size-full wp-image-478\" srcset=\"https:\/\/www.stubbornella.org\/wp-content\/uploads\/2010\/06\/heading.png 416w, https:\/\/www.stubbornella.org\/wp-content\/uploads\/2010\/06\/heading-300x24.png 300w\" sizes=\"auto, (max-width: 416px) 100vw, 416px\" \/><\/a><\/p>\n<h3>5 Media Blocks<\/h3>\n<p><a href=\"https:\/\/www.stubbornella.org\/wp-content\/uploads\/2010\/06\/5mediaBlocks.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.stubbornella.org\/wp-content\/uploads\/2010\/06\/5mediaBlocks.png\" alt=\"\" title=\"5mediaBlocks\" width=\"477\" height=\"227\" class=\"alignnone size-full wp-image-479\" srcset=\"https:\/\/www.stubbornella.org\/wp-content\/uploads\/2010\/06\/5mediaBlocks.png 477w, https:\/\/www.stubbornella.org\/wp-content\/uploads\/2010\/06\/5mediaBlocks-300x143.png 300w\" sizes=\"auto, (max-width: 477px) 100vw, 477px\" \/><\/a><\/p>\n<h3>2 Link Styles<\/h3>\n<p><a href=\"https:\/\/www.stubbornella.org\/wp-content\/uploads\/2010\/06\/link.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.stubbornella.org\/wp-content\/uploads\/2010\/06\/link.png\" alt=\"Link Styles\" title=\"link\" width=\"107\" height=\"45\" class=\"alignnone size-full wp-image-480\" \/><\/a><\/p>\n<h3>1 Comments List<\/h3>\n<p><a href=\"https:\/\/www.stubbornella.org\/wp-content\/uploads\/2010\/06\/comments_list.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.stubbornella.org\/wp-content\/uploads\/2010\/06\/comments_list.png\" alt=\"Comments List\" title=\"comments_list\" width=\"380\" height=\"160\" class=\"alignnone size-full wp-image-481\" srcset=\"https:\/\/www.stubbornella.org\/wp-content\/uploads\/2010\/06\/comments_list.png 380w, https:\/\/www.stubbornella.org\/wp-content\/uploads\/2010\/06\/comments_list-300x126.png 300w\" sizes=\"auto, (max-width: 380px) 100vw, 380px\" \/><\/a><\/p>\n<h3>1 Action List<\/h3>\n<p><a href=\"https:\/\/www.stubbornella.org\/wp-content\/uploads\/2010\/06\/action_list.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.stubbornella.org\/wp-content\/uploads\/2010\/06\/action_list.png\" alt=\"Action List\" title=\"action_list\" width=\"263\" height=\"28\" class=\"alignnone size-full wp-image-482\" \/><\/a><\/p>\n<h3>1 Paragraph<\/h3>\n<p><a href=\"https:\/\/www.stubbornella.org\/wp-content\/uploads\/2010\/06\/paragraph.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.stubbornella.org\/wp-content\/uploads\/2010\/06\/paragraph.png\" alt=\"paragraph\" title=\"paragraph\" width=\"332\" height=\"42\" class=\"alignnone size-full wp-image-483\" srcset=\"https:\/\/www.stubbornella.org\/wp-content\/uploads\/2010\/06\/paragraph.png 332w, https:\/\/www.stubbornella.org\/wp-content\/uploads\/2010\/06\/paragraph-300x38.png 300w\" sizes=\"auto, (max-width: 332px) 100vw, 332px\" \/><\/a><\/p>\n<h3>Benefits of this approach?<\/h3>\n<p>Getting the CSS object granularity right means that our CSS (and by extension, our HTML) become much simpler and lighter. Our little reusable object can be employed across the site to create new and different pages without adding to the weight of the CSS.<\/p>\n<p>As a starting point, I recommend creating the following objects: headings, lists, text treatments and links, containers, media, grids, and template. Create all your smallest objects first, before you build whole pages, and your CSS will stay small by default.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my <a href=\"http:\/\/www.stubbornella.org\/content\/2010\/06\/12\/visual-semantics-in-html-and-css\/\">previous post<\/a>, I said:<\/p>\n<blockquote><p>Shoehorning CSS and HTML into PHP abstractions prevents the code from being DRY and ultimately leads to code bloat, because, the CSS and HTML require a far more granular object structure than the PHP.<\/p><\/blockquote>\n<p>And then I didn&#8217;t expand on it, or give proper context. Ooops, sorry!<\/p>\n<p>In the PHP layer, the following stream story (from facebook) might be a single object. All of the logic of determining which bits of HTML to show or hide are contained within one object structure. <\/p>\n<p><a href=\"http:\/\/www.stubbornella.org\/content\/wp-content\/uploads\/2010\/06\/Facebook-stream-story.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.stubbornella.org\/content\/wp-content\/uploads\/2010\/06\/Facebook-stream-story.png\" alt=\"An example of a stream story on the facebook website\" title=\"Facebook stream story\" width=\"477\" height=\"213\" class=\"size-full wp-image-467\" srcset=\"https:\/\/www.stubbornella.org\/wp-content\/uploads\/2010\/06\/Facebook-stream-story.png 477w, https:\/\/www.stubbornella.org\/wp-content\/uploads\/2010\/06\/Facebook-stream-story-300x134.png 300w\" sizes=\"auto, (max-width: 477px) 100vw, 477px\" \/><\/a><\/p>\n<p>Frequently, developers try to make the CSS match this middle-end logic. We expect all the CSS code for the stream story to be sand-boxed within a wrapper id, for example #story. This ends up with bloated code that isn&#8217;t DRY. Why? because the proper architecture for the CSS layer is much more granular. Trying to base the CSS architecture on the PHP layer is something akin to trying to use the DB schema to configure apache. It just doesn&#8217;t work. <\/p>\n<p><a href=\"http:\/\/www.stubbornella.org\/content\/wp-content\/uploads\/2010\/06\/smaller_objects.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.stubbornella.org\/content\/wp-content\/uploads\/2010\/06\/smaller_objects.png\" alt=\"stream story broken down\" title=\"smaller_objects\" width=\"494\" height=\"216\" class=\"size-full wp-image-477\" srcset=\"https:\/\/www.stubbornella.org\/wp-content\/uploads\/2010\/06\/smaller_objects.png 494w, https:\/\/www.stubbornella.org\/wp-content\/uploads\/2010\/06\/smaller_objects-300x131.png 300w\" sizes=\"auto, (max-width: 494px) 100vw, 494px\" \/><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31,20,5,35,24,27],"tags":[],"class_list":["post-466","post","type-post","status-publish","format-standard","hentry","category-css","category-geek","category-general","category-image","category-performance-geek-general","category-xhtml-html"],"_links":{"self":[{"href":"https:\/\/www.stubbornella.org\/wp-json\/wp\/v2\/posts\/466","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.stubbornella.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.stubbornella.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.stubbornella.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.stubbornella.org\/wp-json\/wp\/v2\/comments?post=466"}],"version-history":[{"count":0,"href":"https:\/\/www.stubbornella.org\/wp-json\/wp\/v2\/posts\/466\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.stubbornella.org\/wp-json\/wp\/v2\/media?parent=466"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.stubbornella.org\/wp-json\/wp\/v2\/categories?post=466"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.stubbornella.org\/wp-json\/wp\/v2\/tags?post=466"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}