IE8 https://publish.mediacurrent.com/ en Sass Breakpoint Mixin Meets IE8 https://publish.mediacurrent.com/blog/sass-breakpoint-mixin-meets-ie8 <span class="field field--name-title field--type-string field--label-hidden">Sass Breakpoint Mixin Meets IE8</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><p>If you're familiar with Sass, you're probably well aware of the <a href="http://breakpoint-sass.com/">Breakpoint mixin</a> and it's awesomeness. It's a tool that makes using media queries extremely easy. Instead of having to break up your CSS into large chunks by breakpoint or even into separate stylesheets, you can organize your CSS by base elements, layout, module, state, or theme. Note that these terms are not Drupal terms, for more information on how to categorize your Sass, check out the <a href="http://smacss.com/book/categorizing">SMACSS e-book by Jonathan Snook</a> and also this handy <a href="https://github.com/tinganho/smacss-sass-template">SMACSS</a> template.</p> <p style="margin-bottom: 0px; padding-bottom: 0;">Enter breakpoint, which allows you to inject media queries wherever necessary. Say you are creating menu lists in the footer of your site. In a desktop view, you may want the menu blocks to sit side-by-side, but on mobile they should stack vertically. So your Sass breakpoint mixin may be something simple where you define a mobile first strategy with two breakpoints for tablet and desktop:</p> <pre class="line-numbers"> <code class="language-css">@import "breakpoint"; $tablet: 500px; $desktop: 960px;</code></pre><p>So in your code, you can utilize your breakpoint mixin like this:</p> <p style="margin-bottom: 0px; padding-bottom: 0;"><strong><em>.scss syntax OR</em></strong></p> <pre class="line-numbers"> <code class="language-css">#footer .menu-block { width: 96%; margin: 0 2%; @include breakpoint($desktop) { width: 29.5%; }</code></pre><p style="margin-bottom: 2px; padding-bottom: 0;"><strong><em>.sass syntax (shorthand)</em></strong></p> <pre class="line-numbers"> <code class="language-css">#footer .menu-block width: 96% margin: 0 2% +breakpoint($desktop) width: 29.5% </code></pre><p style="margin-bottom: 2px; padding-bottom: 0;"><strong>which will output css like this:</strong></p> <pre class="line-numbers"> <code class="language-css">#footer .menu-block {   width: 96%;   margin: 0 2%; } @media (min-width: 960px) { #footer .menu-block { width: 29.5%; } }</code></pre><p>Brilliant! But... a couple things you may not have considered is 1) the effect on performance and 2) how you can use breakpoint to retrofit Internet Explorer 8.</p> <h3>1. Performance Considerations</h3> <p>As you can imagine, this method makes media queries easy to use and helps keep your Sass organized. But what about the resulting CSS? This means you will end up with media queries sprinkled throughout your CSS. Won't this make everything load slower? As it turns out, the effects on performance are almost nil. Check out this great case study at <a href="http://sasscast.tumblr.com/post/38673939456/sass-and-media-queries">sasscast.tumblr.com</a> to see exactly how this plays out. In the case study, a <a href="http://aaronjensen.github.com/media_query_test/">test scenario</a> was created that included 2000 queries. In one result, all the queries were separate (which is how the breakpoint output looks) and the other where they are all combined. The resulting difference was a couple hundred milliseconds, which when you factor in caching becomes almost entirely negligible.</p> <h3>2. Backwards Compatibility for Older Browsers</h3> <p>Another problem that creeps up when using the breakpoint mixin and fragmented media queries is how to easily make a custom stylesheet for IE8 (or 7) so that these older browsers will be able to understand the queries. It is true that there are various javascript solutions like <a href="https://github.com/scottjehl/Respond">respond.js</a> or <a href="https://code.google.com/p/css3-mediaqueries-js/">css3-mediaqueries-js</a>, but if you are dealing with a large site or complex caching methods... then these may not be viable options. </p> <p style="margin-bottom: 2px; padding-bottom: 0;">Why not let Sass do the work for you? After you've got your basic breakpoint mixin created, then you can create a special mixin for desktop (or whichever breakpoint is appropriate that you want to use to apply those same styles to IE8).</p> <p style="margin-bottom: 0px; padding-bottom: 0;"> <strong><em>.scss syntax OR</em></strong></p> <pre class="line-numbers"> <code class="language-css">@mixin desktop {   @include breakpoint($desktop) {   @content   }   .old-ie &amp; {   @content   } } </code></pre><p style="margin-bottom: 0px; padding-bottom: 0;"><strong><em>.sass syntax (shorthand)</em></strong></p> <pre class="line-numbers"> <code class="language-css">@mixin desktop   +breakpoint($desktop)   @content   .old-ie &amp;   @content </code></pre><p>Then in your Sass stylesheet, use @include desktop instead of @include breakpoint(desktop). Sass will then create both the media query with the appropriate styles, and the same styles with the prefix of .old-ie (or whatever you want to name it). In reality, if you have lots of breakpoints you could create matching mixins for all breakpoints, which would make the amount of text you have to type even shorter!</p> <p style="margin-bottom: 2px; padding-bottom: 0;"><a href="http://note.io/14hkbfq"><img alt="Breakpoint Internet Explorer 8" data-entity-type="" data-entity-uuid="" height="587" src="/sites/default/files/example.scss_.png" style="max-width: 100%; height: auto; display: block;" title="Breakpoint Internet Explorer 8" width="872" /></a></p> <p><a href="http://sassmeister.com/gist/starryeyez024/6168597">See this code in action at sassmeister.com.</a></p> <p style="margin-bottom: 2px; padding-bottom: 0;">Finally, to bring it all together, simply add an IE conditional statement to your html so that older versions of Internet Explorer will pick up these styles. Here's an example:</p> <pre class="line-numbers"> <code class="language-css">&lt;!--[if lt IE 9]&gt; &lt;html class="old-ie"&gt; &lt;![endif]--&gt; </code></pre><p>Now you can continue to use object-oriented Sass while keeping IE8 users happy!</p> <p><img alt="" data-entity-type="" data-entity-uuid="" src="/sites/default/files/high-five.gif" /></p> <p>UPDATE:  You can take this a step farther and build in your fallbacks into your <a href="https://github.com/Team-Sass/breakpoint/wiki/No-Query-Fallbacks">Breakpoint variable</a> (thanks <a href="https://twitter.com/codingdesigner">@codingdesigner</a>!).</p> <p><strong>Additional Resources</strong></p> <p><a href="https://www.mediacurrent.com/blog/webinar-elegant-css-drupal%25E2%2580%2594less-vs-sass">Elegant CSS In Drupal—LESS vs Sass</a> | Webinar </p> <p><a href="http://www.mediacurrent.com/blog/controlling-your-site-layout-using-mobile-first-omega-theme">Controlling Your Site Layout Using The Mobile-First Omega Theme</a> | Webinar</p> <p><a href="http://www.mediacurrent.com/blog/tapping-drupal-7-responsive-adaptivetheme">Tapping into the Drupal 7 Responsive AdaptiveTheme</a> | Blog</p> <p> </p> </div> <span class="field field--name-uid field--type-entity-reference field--label-hidden"><a title="View user profile." href="/about/our-team/mediacurrent-team" lang="" about="/about/our-team/mediacurrent-team" typeof="schema:Person" property="schema:name" datatype="" class="username">Mediacurrent Team</a></span> <span class="field field--name-created field--type-created field--label-hidden">Thu, 08/08/2013 - 10:56</span> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field__label">Tags</div> <div class="field__items"> <div class="field__item"><a href="/tags/breakpoint" hreflang="en">breakpoint</a></div> <div class="field__item"><a href="/tags/compatibility" hreflang="en">compatibility</a></div> <div class="field__item"><a href="/tags/css" hreflang="en">CSS</a></div> <div class="field__item"><a href="/tags/drupal" hreflang="en">Drupal</a></div> <div class="field__item"><a href="/tags/ie7" hreflang="en">IE7</a></div> <div class="field__item"><a href="/tags/ie8" hreflang="en">IE8</a></div> <div class="field__item"><a href="/tags/internet-explorer" hreflang="en">internet explorer</a></div> <div class="field__item"><a href="/tags/sass" hreflang="en">Sass</a></div> <div class="field__item"><a href="/tags/theming" hreflang="en">Theming</a></div> <div class="field__item"><a href="/tags/web-design" hreflang="en">web design</a></div> </div> </div> <div class="gatsby-iframe-container"><iframe class="gatsby-iframe" src="https://preview-misriptide.gtsb.io/blog/sass-breakpoint-mixin-meets-ie8"></iframe></div> Thu, 08 Aug 2013 14:56:01 +0000 Mediacurrent Team 918 at https://publish.mediacurrent.com