SASS Folder Structure:
-
sass
- base
- _base.scss
- _manifest.scss
- libs
- _manifest.scss
- modules
- _example.scss
- _extends.scss
- _manifest.scss
- _modules.scss
- states
- _manifest.scss
- _print.scss
- _states.scss
- utilities
- bootstrap
- _bootstrap.scss
- _config.scss
- _helpers.scss
- _manifest.scss
- _normalize.scss
- _utilities.scss
- _shame.scss
- style.scss
- base
Using Mixins:
Using Media query Mixins:
Find the mixin for media query under utilities/_utilities.scss. Responsive web design for Sass Starter is based on Mobile first design and media query breakpoints are based on bootstrap breakpoints. Breakpoints variables can be customized according to your needs at utilities/_config.scss.
<div class="Box"></div>
.Box{ // For mobile devices of size less than 768px put code here width: 20px; height: 20px; background: red; transition: width 0.3s ease-out, height 0.3s ease-out; margin-right: 10px; float: left; @include mq(sm){// For devices of size greater than 768px(tablets) put code here width: 50px; height: 50px; background: blue; } @include mq(md){// For devices of size greater than 992px(laptops) put code here width: 70px; height: 70px; background: green; } @include mq(lg){// For devices of size greater than 1200px(desktops and high-res TVs) put code here width: 100px; height: 100px; background: yellow; } }
.Box{ width: 20px; height: 20px; background: red; transition: width 0.3s ease-out, height 0.3s ease-out; margin-right: 10px; float: left; } @media(min-width: 768px){ .Box{ width: 50px; height: 50px; background: blue; } } @media(min-width: 992px){ .Box{ width: 70px; height: 70px; background: green; } } @media(min-width: 1200px){ .Box{ width: 100px; height: 100px; background: yellow; } }
Using BEM Mixins:
Find the mixin for BEM under utilities/_utilities.scss.
Block
Block is a top-level abstraction of a new component. It is top in the hierarchy for this naming convention. Blocks can be nested with other blocks but they are independent to each other with no precedence in hierarchy.
We should use class name selector only; ids and tag names should not be used. CSS code should be independent from all other blocks and html elements.
<ul class="Nav">...</ul>
// Here ".nav" class selector is a Block .Nav { padding: 10px 0; }
Element
Element is child elements inside of Block element and gives specific meaning to its elements. Elements have their meaning always derived from its respective parent block.
CSS class is formed as block name plus two underscores plus element name. Example: .block__elem Element naming can also have a single dash. Example: .Patient__info-detail
We should use class name selector only, ids and tag names should not be used. CSS code should be independent from all other blocks and html elements.
<ul class="Nav"> <li class="nav__item">...</nav> <li class="nav__item">...</nav> <li class="nav__item">...</nav> </ul>
// Here ".Nav" class selector is a Block and ".Nav__item" is an Element .Nav{ padding: 10px 0; @include e(item){ //'e' is small for element float: left; margin-right: 10px; } }
Output is completely independent from the parent block class selectors.
.Nav { padding: 10px 0; } .Nav__item{ float: left; margin-right: 10px; }
Modifier
Modifier is used to modify blocks or elements. CSS class will be formed by added two dashes to block’s or element’s class name. Example: .block--mod or .block__elem--mod Modifier naming can also have a single dash. Example: .Box__row--gray-bg
We should use class name selector only, ids and tag names should not be used. CSS code should be independent from all other blocks and html elements.
<ul class="Nav"> <li class="nav__item nav__item--active">...</nav> <li class="nav__item">...</nav> <li class="nav__item">...</nav> </ul>
// Here ".Nav" class selector is a Block and ".Nav__item" is an Element .Nav{ padding: 10px 0; @include e(item){ float: left; margin-right: 10px; @include m(active){//'m' is small for element. Nest this under element mixin color: blue; font-weight: bold; } } }
Output is completely independent from the parent block and element class selectors.
.Nav { padding: 10px 0; } .Nav__item{ float: left; margin-right: 10px; } .Nav__item--active{ color: blue; font-weight: bold; }
Using Functions:
Find the functions defined under utilities/_utilities.scss.
rem function
Convert pixel unit into rem.
header{ padding: rem(20px) rem(30px); }
header{ padding: rem(20) rem(30); }
header{ padding: 1.25rem 1.875rem; //base font size of 16px }
pntg function
Convert pixel unit to percentage.
.box{ width: pntg(300,768); //First parameter is target width and second parameter is context width }
.box{ width: 39.0625%; }
Using Maps:
Define maps at _config.scss partial.
Mapping background colors to elements:
<div> <button class="btn btn--default">Default</button> <button class="btn btn--success">Succes</button> <button class="btn btn--error">Error</button> <button class="btn btn--warning">Warning</button> </div>
// Declare map for UI colors $ui-colors: ( default: $blue, success: $success, error: $error, warning: $edit ); //Using a mixin to iterate through the map: @mixin bg-colors($map){ @each $theme, $color in $map{ &--#{$theme}{ //Interpolating parent class name with map key background: $color; } } } //Using the mixin to create different types of button with different background colors: .btn{ padding: 10px 15px; border: 0; border-radius: 2px; color: #fff; cursor: pointer; font-weight: bold; @include bg-colors($ui-colors); }
.btn{ padding: 10px 15px; border: 0; border-radius: 2px; color: #fff; cursor: pointer; font-weight: bold; } .btn--default{ background: blue; } .btn--success{ background: green; } .btn--error{ background: red; } .btn--warning{ background: yellow; }
Using nested maps:
<div class="Palette__wrap"> <div class="gray--dark">Dark</div> <div class="gray--base">Base</div> <div class="gray--light">Light</div></div> <div class="Palette__wrap"> <div class="blue--dark">Dark</div> <div class="blue--base">Base</div> <div class="blue--light">Light</div> </div>
// Nested Map: $palettes: ( gray-shades: ( base: $gray, dark: darken($gray,20%), light: lighten($gray,20%) ), blue-shades: ( base: $blue, dark: darken($blue,20%), light: lighten($blue,20%) ) ); // Use palette function // $palette - Map required under $palettes map($palettes map is defined under _config.scss partial) // $shade - Key of provide $palette map // base - Default key of $shade under $palette map // @returns - map @function palette($palette, $shade: base){ @return map-get(map-get($palettes, $palette), $shade); } //Using function in SCSS code: .blue{ @include m(base){ @extend %palette-props; background: palette(blue-shades); } @include m(dark){ @extend %palette-props; background: palette(blue-shades, dark); } @include m(light){ @extend %palette-props; background: palette(blue-shades, light); } }
Using grids
Theming
Find the theme map and functions in the _theme.scss partial.
<h3>Theming with SASS</h3>
// Theme map $themes: ( color: ( default: ( heading-color: #000000, paragraph-color: #353535 ), custom: ( heading-color: #534D56, paragraph-color: #656176 ) ), size: ( cozy: ( heading-font-size: 18px, paragraph-font-size: 12px ), comfortable: ( heading-font-size: 22px, paragraph-font-size: 14px ) ) ); h3{ margin-bottom: 10px; display: inline-block; @include themify(theme-type(color)){ color: theme-property(heading-color); } @include themify(theme-type(size)){ font-size: theme-property(heading-font-size); } }
You can test it out by selecting the following.
Theming demo
This is a theming demo with SASS. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo.