0

From a styling point of view I can achieve what I need but it feels like I am duplicating a lot of code.

I've created a very small example of what I am trying to achieve on a much bigger scale. Each difficulty level as a colour associated with it and is used for different things like font colour, border colour, background colour etc.

CodePen

<div class="lesson easy">
  <h2>Easy Lesson</h2>
  <p>Description</p>
  <button>More Info</button>
</div>

<div class="lesson medium">
  <h2>Medium Lesson</h2>
  <p>Description</p>
  <button>More Info</button>
</div>

<div class="lesson hard">
  <h2>Hard Lesson</h2>
  <p>Description</p>
  <button>More Info</button>
</div>
//variables that relate to a specific difficulty
$easy: green;
$medium: orange;
$hard: red;

//base styles
.lesson {
  border: 2px solid black;
  width: 200px;
  float: left;
  margin-right: 10px;
  padding: 0px 20px 20px 20px;
}

button {
  border: 0;
  padding: 10px;
}

//specific styles, is there an easier, cleaner way to write this using a mixin that will allow me to use it on a much larger scale with even more nested HTML elements?
.lesson {
  &.easy {
    border-color: $easy;
    button {
      background: $easy;
    }
    p {
      color: $easy;
    }
  }
  &.medium {
    border-color: $medium;
    button {
      background: $medium;
    }
    p {
      color: $medium;
    }
  }
  &.hard {
    border-color: $hard;
    button {
      background: $hard;
    }
    p {
      color: $hard;
    }
  }
}
  • Look into [CSS variables](https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_variables). Example: https://codepen.io/Sphinxxxx/pen/EpZrQZ?editors=0100 – Sphinxxx Jul 21 '18 at 23:33

1 Answers1

0

You can write all variants on a map and iterate over it's properties to generate all class combinations.

$colors: (
  'easy': green,
  'medium': orange,
  'hard': red
);

@each $name, $color in $colors {
  .lesson.#{$name} {
    border-color: $color;
    button {
      background: $color;
    }
    p {
      color: $color;
    }
  }
}
VitorLuizC
  • 360
  • 1
  • 7