{"id":15745,"date":"2014-10-14T17:57:32","date_gmt":"2014-10-14T12:27:32","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=15745"},"modified":"2014-10-14T18:35:06","modified_gmt":"2014-10-14T13:05:06","slug":"using-media-queries-with-sass","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/using-media-queries-with-sass\/","title":{"rendered":"Using Media queries with Sass"},"content":{"rendered":"<p>While starting work on redesign of an existing site, I was suggested to give CSS preprocessors like Less\/SCSS a try.\u00a0 I was thrilled by the features of SCSS and started woking on it and I would like to share my experience about writing media queries in SCSS with you guys.<\/p>\n<p>In SCSS, we can use if\/else condition like any other programming language. We&#8217;ll be using this power of SCSS to write very verbose media queries which are much more readable than the usual CSS media queries. The end result will be a <a href=\"http:\/\/sass-lang.com\/guide#topic-6\" title=\"@mixin\" target=\"_blank\">@mixin<\/a> which will allow us to &#8220;name&#8221; media queries.<\/p>\n<p><strong>1. Name the mixin<\/strong><\/p>\n<p>The @mixin feature of SCSS allows us to define &#8220;function-like&#8221; structures which are reusable and we&#8217;ll use this functionality to create a reusable piece of CSS styling. A mixin needs a name at the very least. Let&#8217;s name the mixin as &#8220;devices&#8221; since we&#8217;ll use it to differentiate between devices:<br \/>\n[html]<br \/>\n@mixin devices($media) {<br \/>\n&#8230;<br \/>\n}[\/html]<\/p>\n<p>Currently, this mixin does not do anything but accepts an &#8220;argument&#8221; which specifies the type of media we&#8217;re targeting.<\/p>\n<p><strong>2. Output device specific media queries<\/strong><\/p>\n<p>The next piece of magic to be added to this mixin is to output specific media queries based on the type of media that we pass to the mixin.<\/p>\n<p>[html]@mixin devices($media) {<br \/>\n@if $media == mobile {<br \/>\n@media only screen and (min-width:320px) and (max-width:767px) {<br \/>\n&#8230;<br \/>\n}<br \/>\n} @else if $media == ipad {<br \/>\n@media only screen and (min-width:768px) and (max-width:991px) {<br \/>\n&#8230;<br \/>\n}<br \/>\n}<br \/>\n}[\/html]<br \/>\n<strong><br \/>\n3. Add the actual CSS<\/strong><\/p>\n<p>Now the final part of the puzzle is the actual CSS to be used on each of the devices. SCSS allows you to use a special directive named &#8220;@content&#8221; which allows us to pass a content block into a mixin.<\/p>\n<p>So, I can write my mixin like this:<\/p>\n<p>[html]@mixin devices($media) {<br \/>\n@if $media == mobile {<br \/>\n@media only screen and (min-width:320px) and (max-width:767px) {<br \/>\n@content;<br \/>\n}<br \/>\n} @else if $media == ipad {<br \/>\n@media only screen and (min-width:768px) and (max-width:991px) {<br \/>\n@content;<br \/>\n}<br \/>\n}<br \/>\n}[\/html]<\/p>\n<p>That&#8217;s it! Now, to specify styles for devices I can simply write:<\/p>\n<p>[html]\/\/Specifies style for mobile devices<br \/>\n@include devices(mobile) {<br \/>\n#header {<br \/>\n.logo {width: 135px;}<br \/>\n}<br \/>\n}<\/p>\n<p>\/\/Specifies style for ipads<br \/>\n@include devices(ipad) {<br \/>\n#header {<br \/>\n.logo {width: 185px;}<br \/>\n}<br \/>\n}[\/html]<\/p>\n<p>The final output for this CSS is as follows:<\/p>\n<p>[html]@media only screen and (min-width: 320px) and (max-width: 767px) {<br \/>\n#header .logo {<br \/>\nwidth: 135px;<br \/>\n}<br \/>\n}<br \/>\n@media only screen and (min-width: 768px) and (max-width: 991px) {<br \/>\n#header .logo {<br \/>\nwidth: 185px;<br \/>\n}<br \/>\n}<br \/>\n[\/html]<\/p>\n<p>Cool and simple! Enjoy!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>While starting work on redesign of an existing site, I was suggested to give CSS preprocessors like Less\/SCSS a try.\u00a0 I was thrilled by the features of SCSS and started woking on it and I would like to share my experience about writing media queries in SCSS with you guys. In SCSS, we can use [&hellip;]<\/p>\n","protected":false},"author":50,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1},"categories":[1],"tags":[1105,4842,1443,1535],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/15745"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/50"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=15745"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/15745\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=15745"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=15745"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=15745"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}