{"id":1635,"date":"2016-03-13T20:06:08","date_gmt":"2016-03-13T20:06:08","guid":{"rendered":"http:\/\/www.nathankowald.com\/blog\/?p=1635"},"modified":"2017-03-11T11:33:19","modified_gmt":"2017-03-11T11:33:19","slug":"responsive-utility-classes","status":"publish","type":"post","link":"https:\/\/www.nathankowald.com\/blog\/2016\/03\/responsive-utility-classes\/","title":{"rendered":"Responsive utility classes"},"content":{"rendered":"<p>Utility classes serve a single purpose.<br \/>\nExamples of utility classes: <tt class=\"code\">.align-left, .float-left, .clearfix<\/tt><br \/>\n<em>Responsive<\/em> utility classes allow you to create very flexible responsive websites.<\/p>\n<p><strong>This post is about what makes a great responsive utility class.<\/strong><\/p>\n<p>I&#8217;ll talk about: <a href=\"#naming\">naming<\/a> and <a href=\"#inclusive-and-exclusive-pattern\">a useful pattern<\/a>.<\/p>\n<p><em>For a good introduction to utility classes I suggest David Clark&#8217;s excellent article <a href=\"http:\/\/davidtheclark.com\/on-utility-classes\/\" target=\"_blank\">here<\/a>.<\/em><\/p>\n<h2 id=\"naming\">Naming<\/h2>\n<p>I don&#8217;t like namespaced or aliased utility classes.<br \/>\nI don&#8217;t like them because they are <a href=\"http:\/\/www.boston.com\/bostonglobe\/ideas\/articles\/2010\/01\/31\/easy__true\/\" target=\"_blank\">harder to think about<\/a>.<\/p>\n<p>An example of a bad utility class is: <tt class=\"code\">.u-mt-10@md<\/tt><\/p>\n<p><strong>Why is this bad?<\/strong><br \/>\nYou must know that <tt class=\"code\">.u<\/tt> is a convention for utility classes.<br \/>\nYou must know that <tt class=\"code\">mt<\/tt> refers to margin-top.<br \/>\nYou must know that <tt class=\"code\">@md<\/tt> means this utility class targets &#8220;Medium&#8221; sized devices.<\/p>\n<p><strong>What&#8217;s an alternative?<\/strong><\/p>\n<blockquote><p>Utility classes should use CSS property names and values: <tt class=\"code\">.<strong>margin-top<\/strong>-10-xs<\/tt><\/p><\/blockquote>\n<p>It is clear that this utility class adds 10 pixels of top margin.<br \/>\nUsers familiar with Bootstrap&#8217;s grid system may guess that this targets &#8220;Extra small&#8221; viewports and greater.<\/p>\n<h2 id=\"inclusive-and-exclusive-pattern\">Pattern: Inclusives and Exclusives<\/h2>\n<p>Two types of responsive utility classes are needed when building layouts: inclusives and exclusives.<\/p>\n<ol>\n<li><strong>Inclusive<\/strong>: You need a utility class to target a specific device size and everything greater than it (most common)<\/li>\n<li><strong>Exclusive<\/strong>: You need a utility class to only target a specific device size<\/li>\n<\/ol>\n<blockquote><p>Separate responsive utility classes into two categories: inclusives and exclusives.<\/p><\/blockquote>\n<p>A simple naming convention in your utility class tells you how it will behave.<\/p>\n<h3>Inclusives<\/h3>\n<p><strong>Inclusive classes include the named device size and device sizes greater than itself.<\/strong><\/p>\n<p>When you use Bootstrap&#8217;s responsive grid you only have to define an element as <tt class=\"code\">.col-xs-12<\/tt> and you get 12 columns on mobiles, tablets and desktops.<\/p>\n<p>Noticing that this simple pattern results in less classes showed me that by default all responsive utility classes should behave this way.<\/p>\n<blockquote><p>Responsive utility classes are inclusives by default.<\/p><\/blockquote>\n<p>Every utility class that uses the convention of <tt class=\"code\">.property-value-deviceSize<\/tt> is an inclusive class.<\/p>\n<p>You may have noticed our utility classes follow a different convention to Bootstrap&#8217;s columns.<\/p>\n<p>Bootstrap uses: <tt class=\"code\">.property-<strong>deviceSize<\/strong>-value<\/tt><br \/>\nWe use: <tt class=\"code\">.property-value-<strong>deviceSize<\/strong><\/tt><\/p>\n<p>This was intentional because it&#8217;s easier to think about.<\/p>\n<p>When creating an element I know I want to apply 10 pixels of top margin to an element. The targeted device is a detail.<\/p>\n<blockquote><p>In my head I&#8217;ll say &#8220;margin top 10 on mobiles and greater&#8221;, not &#8220;margin top on mobiles and greater, of 10 pixels&#8221;.<\/p><\/blockquote>\n<p>How inclusives look in code<\/p>\n<pre class=\"prettyprint lang-scss\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">\r\n\/* Inclusives *\/\r\n\r\n\/\/ Mobile and >\r\n.margin-top-10-xs {\r\n    margin-top: 10px;\r\n}\r\n\/\/ Tablet and >\r\n@media screen and (min-width: $screen-sm-min) {\r\n    .margin-top-10-sm {\r\n        margin-top: 10px;\r\n    }\r\n}\r\n\/\/ Desktop and >\r\n@media screen and (min-width: $screen-md-min) {\r\n    .margin-top-10-md {\r\n        margin-top: 10px;\r\n    }\r\n}<\/pre>\n<h3>Exclusives<\/h3>\n<p><strong>Exclusives target the named device size only.<\/strong><\/p>\n<p>They follow this convention: <tt class=\"code\">.property-value-<strong>only<\/strong>-deviceSize<\/tt><br \/>\nA margin top of 10 pixels applied to only mobiles looks like: <tt class=\"code\">.margin-top-10-<strong>only<\/strong>-xs<\/tt><\/p>\n<p>How exclusives look in code.<\/p>\n<pre class=\"prettyprint lang-scss\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">\r\n\/* Exclusives *\/\r\n\r\n\/\/ Mobile - max-width used\r\n@media screen and (max-width: $screen-xs-max) {\r\n    .margin-top-10-only-xs {\r\n         margin-top: 10px;\r\n     }\r\n}\r\n\/\/ Tablet\r\n@media screen and (min-width: $screen-sm-min) and (max-width: $screen-sm-max) {\r\n    .margin-top-10-only-sm {\r\n        margin-top: 10px;\r\n    }\r\n}\r\n\/\/ Desktop\r\n@media screen and (min-width: $screen-md-min) {\r\n    .margin-top-10-only-md {\r\n        margin-top: 10px;\r\n    }\r\n}<\/pre>\n<p>Distinguishing between inclusive and exclusive classes leads to incredibly flexible responsive utility classes.<\/p>\n<h2>Structural responsive utility classes<\/h2>\n<p>Copy this pattern for other structural properties.<\/p>\n<blockquote><p>Responsive utility classes are almost always used on structural CSS.<\/p><\/blockquote>\n<p>&#8211; Margin<br \/>\n&#8211; Padding<br \/>\n&#8211; <a href=\"https:\/\/github.com\/n8kowald\/sass-setup\/blob\/master\/utilities\/_line-breaks.scss\" target=\"_blank\">Line breaks<\/a><\/p>\n<h2>Utility classes should be imported last<\/h2>\n<p>Adding utility classes last helps you avoid specificity issues.<br \/>\nUsing Sass or Less simply import your utility classes last.<br \/>\nBy including our utilities last, I&#8217;ve not had to use !important yet.<\/p>\n<pre class=\"prettyprint lang-scss\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">\r\n@import base\/reset.scss;\r\n@import layout\/layout.scss\r\n@import module\/headers.scss;\r\n@import page\/homepage.scss;\r\n@import utilities\/utilities.scss;<\/pre>\n<h2>tl;dr<\/h2>\n<p>By naming our responsive utility classes with valid CSS property names and values we have created clear, easy to use utility classes that don&#8217;t require education &#8211; if you are familiar with CSS properties already.<\/p>\n<p>By breaking responsive utility classes into inclusives and exclusives by <tt class=\"code\">-xs<\/tt> being inclusives and <tt class=\"code\">-only-xs<\/tt> being exclusives, our responsive utility classes are very flexible.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Utility classes serve a single purpose. Examples of utility classes: .align-left, .float-left, .clearfix Responsive utility classes allow you to create very flexible responsive websites. This post is about what makes a great responsive utility class. I&#8217;ll talk about: naming and a useful pattern. For a good introduction to utility classes I suggest David Clark&#8217;s excellent [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[27,28,7],"tags":[],"class_list":["post-1635","post","type-post","status-publish","format-standard","hentry","category-css","category-responsive-design","category-web-development"],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.nathankowald.com\/blog\/wp-json\/wp\/v2\/posts\/1635","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nathankowald.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nathankowald.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nathankowald.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nathankowald.com\/blog\/wp-json\/wp\/v2\/comments?post=1635"}],"version-history":[{"count":96,"href":"https:\/\/www.nathankowald.com\/blog\/wp-json\/wp\/v2\/posts\/1635\/revisions"}],"predecessor-version":[{"id":1823,"href":"https:\/\/www.nathankowald.com\/blog\/wp-json\/wp\/v2\/posts\/1635\/revisions\/1823"}],"wp:attachment":[{"href":"https:\/\/www.nathankowald.com\/blog\/wp-json\/wp\/v2\/media?parent=1635"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nathankowald.com\/blog\/wp-json\/wp\/v2\/categories?post=1635"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nathankowald.com\/blog\/wp-json\/wp\/v2\/tags?post=1635"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}