{"id":15745,"date":"2014-10-14T17:57:32","date_gmt":"2014-10-14T12:27:32","guid":{"rendered":"https:\/\/2thenew.today\/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:\/\/2thenew.today\/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,"footnotes":""},"categories":[1],"tags":[1105,4842,1443,1535],"class_list":["post-15745","post","type-post","status-publish","format-standard","hentry","category-technology","tag-css3-media-queries","tag-html","tag-responsive","tag-sass"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.0.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"While starting work on redesign of an existing site, I was suggested to give CSS preprocessors like Less\/SCSS a try. 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\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Rajan Shergill\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/2thenew.today\/blog\/using-media-queries-with-sass\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.0.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"TO THE NEW BLOG\" \/>\n\t\t<meta property=\"og:type\" content=\"blog\" \/>\n\t\t<meta property=\"og:title\" content=\"Using Media queries with Sass | TO THE NEW Blog\" \/>\n\t\t<meta property=\"og:description\" content=\"While starting work on redesign of an existing site, I was suggested to give CSS preprocessors like Less\/SCSS a try. 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\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/2thenew.today\/blog\/using-media-queries-with-sass\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/2thenew.today\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/2thenew.today\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:site\" content=\"@tothenew\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Using Media queries with Sass | TO THE NEW Blog\" \/>\n\t\t<meta name=\"twitter:description\" content=\"While starting work on redesign of an existing site, I was suggested to give CSS preprocessors like Less\/SCSS a try. 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\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/2thenew.today\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/using-media-queries-with-sass\\\/#article\",\"name\":\"Using Media queries with Sass | TO THE NEW Blog\",\"headline\":\"Using Media queries with Sass\",\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/rajan\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"},\"datePublished\":\"2014-10-14T17:57:32+05:30\",\"dateModified\":\"2014-10-14T18:35:06+05:30\",\"inLanguage\":\"en-US\",\"commentCount\":2,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/using-media-queries-with-sass\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/using-media-queries-with-sass\\\/#webpage\"},\"articleSection\":\"Technology, css3 media queries, HTML\\\/UI\\\/CSS, responsive, Sass\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/using-media-queries-with-sass\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/technology\\\/#listItem\",\"name\":\"Technology\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/technology\\\/#listItem\",\"position\":2,\"name\":\"Technology\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/technology\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/using-media-queries-with-sass\\\/#listItem\",\"name\":\"Using Media queries with Sass\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/using-media-queries-with-sass\\\/#listItem\",\"position\":3,\"name\":\"Using Media queries with Sass\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/technology\\\/#listItem\",\"name\":\"Technology\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\",\"name\":\"TO THE NEW Blog\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/rajan\\\/#author\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/rajan\\\/\",\"name\":\"Rajan Shergill\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/using-media-queries-with-sass\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/eb5d445c6d373510aede382494d12659d23728d95f26b7cef5d075dc66529ded?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Rajan Shergill\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/using-media-queries-with-sass\\\/#webpage\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/using-media-queries-with-sass\\\/\",\"name\":\"Using Media queries with Sass | TO THE NEW Blog\",\"description\":\"While starting work on redesign of an existing site, I was suggested to give CSS preprocessors like Less\\\/SCSS a try. 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\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/using-media-queries-with-sass\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/rajan\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/rajan\\\/#author\"},\"datePublished\":\"2014-10-14T17:57:32+05:30\",\"dateModified\":\"2014-10-14T18:35:06+05:30\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\",\"name\":\"TO THE NEW Blog\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Using Media queries with Sass | TO THE NEW Blog","description":"While starting work on redesign of an existing site, I was suggested to give CSS preprocessors like Less\/SCSS a try. 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","canonical_url":"https:\/\/2thenew.today\/blog\/using-media-queries-with-sass\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/2thenew.today\/blog\/using-media-queries-with-sass\/#article","name":"Using Media queries with Sass | TO THE NEW Blog","headline":"Using Media queries with Sass","author":{"@id":"https:\/\/2thenew.today\/blog\/author\/rajan\/#author"},"publisher":{"@id":"https:\/\/2thenew.today\/blog\/#organization"},"datePublished":"2014-10-14T17:57:32+05:30","dateModified":"2014-10-14T18:35:06+05:30","inLanguage":"en-US","commentCount":2,"mainEntityOfPage":{"@id":"https:\/\/2thenew.today\/blog\/using-media-queries-with-sass\/#webpage"},"isPartOf":{"@id":"https:\/\/2thenew.today\/blog\/using-media-queries-with-sass\/#webpage"},"articleSection":"Technology, css3 media queries, HTML\/UI\/CSS, responsive, Sass"},{"@type":"BreadcrumbList","@id":"https:\/\/2thenew.today\/blog\/using-media-queries-with-sass\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog#listItem","position":1,"name":"Home","item":"https:\/\/2thenew.today\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog\/category\/technology\/#listItem","name":"Technology"}},{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog\/category\/technology\/#listItem","position":2,"name":"Technology","item":"https:\/\/2thenew.today\/blog\/category\/technology\/","nextItem":{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog\/using-media-queries-with-sass\/#listItem","name":"Using Media queries with Sass"},"previousItem":{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog\/using-media-queries-with-sass\/#listItem","position":3,"name":"Using Media queries with Sass","previousItem":{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog\/category\/technology\/#listItem","name":"Technology"}}]},{"@type":"Organization","@id":"https:\/\/2thenew.today\/blog\/#organization","name":"TO THE NEW Blog","url":"https:\/\/2thenew.today\/blog\/"},{"@type":"Person","@id":"https:\/\/2thenew.today\/blog\/author\/rajan\/#author","url":"https:\/\/2thenew.today\/blog\/author\/rajan\/","name":"Rajan Shergill","image":{"@type":"ImageObject","@id":"https:\/\/2thenew.today\/blog\/using-media-queries-with-sass\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/eb5d445c6d373510aede382494d12659d23728d95f26b7cef5d075dc66529ded?s=96&d=mm&r=g","width":96,"height":96,"caption":"Rajan Shergill"}},{"@type":"WebPage","@id":"https:\/\/2thenew.today\/blog\/using-media-queries-with-sass\/#webpage","url":"https:\/\/2thenew.today\/blog\/using-media-queries-with-sass\/","name":"Using Media queries with Sass | TO THE NEW Blog","description":"While starting work on redesign of an existing site, I was suggested to give CSS preprocessors like Less\/SCSS a try. 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","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/2thenew.today\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/2thenew.today\/blog\/using-media-queries-with-sass\/#breadcrumblist"},"author":{"@id":"https:\/\/2thenew.today\/blog\/author\/rajan\/#author"},"creator":{"@id":"https:\/\/2thenew.today\/blog\/author\/rajan\/#author"},"datePublished":"2014-10-14T17:57:32+05:30","dateModified":"2014-10-14T18:35:06+05:30"},{"@type":"WebSite","@id":"https:\/\/2thenew.today\/blog\/#website","url":"https:\/\/2thenew.today\/blog\/","name":"TO THE NEW Blog","inLanguage":"en-US","publisher":{"@id":"https:\/\/2thenew.today\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"TO THE NEW BLOG","og:type":"blog","og:title":"Using Media queries with Sass | TO THE NEW Blog","og:description":"While starting work on redesign of an existing site, I was suggested to give CSS preprocessors like Less\/SCSS a try. 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","og:url":"https:\/\/2thenew.today\/blog\/using-media-queries-with-sass\/","og:image":"https:\/\/2thenew.today\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","og:image:secure_url":"https:\/\/2thenew.today\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","twitter:card":"summary","twitter:site":"@tothenew","twitter:title":"Using Media queries with Sass | TO THE NEW Blog","twitter:description":"While starting work on redesign of an existing site, I was suggested to give CSS preprocessors like Less\/SCSS a try. 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","twitter:image":"https:\/\/2thenew.today\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png"},"aioseo_meta_data":{"post_id":"15745","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"limit_modified_date":false,"created":"2021-04-29 12:56:08","updated":"2024-02-29 08:41:23","focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/2thenew.today\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/2thenew.today\/blog\/category\/technology\/\" title=\"Technology\">Technology<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tUsing Media queries with Sass\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/2thenew.today\/blog"},{"label":"Technology","link":"https:\/\/2thenew.today\/blog\/category\/technology\/"},{"label":"Using Media queries with Sass","link":"https:\/\/2thenew.today\/blog\/using-media-queries-with-sass\/"}],"_links":{"self":[{"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/posts\/15745","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/users\/50"}],"replies":[{"embeddable":true,"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/comments?post=15745"}],"version-history":[{"count":0,"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/posts\/15745\/revisions"}],"wp:attachment":[{"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/media?parent=15745"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/categories?post=15745"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/tags?post=15745"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}