skip to main content
Select font family
Select colour theme
Profile picture - Ahmad Ainul Rizki Ahmad Ainul Rizki
Profile logo - Ahmad Ainul RizkiCD
  • Home
  • About me
  • Now
  • Portfolio
  • Blog
  • Components

Sass dynamic colour change

Dynamicaly generate background colours using Sass.

  • Follow me on Twitter twitterlink to twitter profile
  • View my Github profile githublink to github profile
  • View my Codepen profile codepenlink to codepen profile

Post link

bridgy link

Published on May 15th 2014 Ahmad Ainul Rizki

When building the website for Maze I wanted to display the content on one page with a downward scroll. Each section or slide would therefore need to have a different background colour to emphasise the content change.

To achieve this I took advantage of some of the new features in Sass. Firstly using variables I was able to store my colour values…

$orange: #ffa600;
$blue: #82d2e5;
$green:#c1d72e;
$light-grey: #b2b2b2 ;
$dark-grey: #5e5e5e ;

I then used a list to assign a colour variable to each slide in the site…

$color :
(1, $orange),
(2, $blue),
(3, $green),
(4, $light-grey),
(5, $dark-grey);

An @each loop is then used to run through the list and generate the css for each slide.

@each $number, $bg in $color {

  .slide-#{$number} {
    background:$bg;
  }

}

The loop generates the css code for each slide takeing the relavant value for $bg from the $color list.

.slide-1 {background: #ffa600;}
.slide-2 {background: #82d2e5;}
.slide-3 {background: #c1d72e;}
.slide-4 {background: #b2b2b2;}
.slide-5 {background: #5e5e5e;}

A working example can be found on Codepen

Share on Twitter

Webmentions

These are webmentions via the IndieWeb and webmention.io.

No mentions yet.

  • Back to all posts
  • RSS feed
  • Home
  • About me
  • Now
  • Portfolio
  • Blog
  • Components
  • Follow me on Twitter twitterlink to twitter profile
  • View my Github profile githublink to github profile
  • View my Codepen profile codepenlink to codepen profile

Proudly powered by Eleventy. Hosted on Netlify, last updated September 15th 2020, at 10:55:45 - privacy.