I have a series of a
tags that form a grid (each a
is a block with the class .portfolio-grid-item
).
Each .portfolio-grid-item
contains several div
and heading
elements and uses a default color via a variable ($grid-color: $blue;
).
I am trying to create a SCSS rule that will do the following:
- Check if an additional class is assigned to the
a
tag. - If there is an additional class, replace the default color variable with a new one.
- If not, fallback to the default.
e.g.
An element classed as .portfolio-grid-item .orange
should, replace $grid-color: $blue
with $grid-color: $orange;
I have tried using an mixin
with an if
statement, but I have never done this before and I am not sure if the method is correct, or supported.
My Pen is here: http://codepen.io/anon/pen/EjwarE
Any advice would be really appreciated!
UPDATE:
To put this in some simple terms (I know this isn't true code, it's just the logic I am aiming for, hopefully this helps!):
// Default
$grid-color: $blue
// Statement
if HTML element has class = `.orange`
// Change the following variable
$grid-color: $orange
The idea being that this will override all instances of $grid-color in one shot
This my be more along the lines of what you need. I'm sure it can be improved but I've used a map of colour values which I loop through to produce your colour variants.
I didn't use an if statement purely because by default I modified your main element CSS to contain the blue background as standard. In this way our loop only needs to churn out the additionally colours that are mapped and the styles are overridden via the extra class you add to that element.
SCSS:
Your portfolio item:
Working example - Codepen
Using this method you could apply colours to the font etc by adapting the map to contain more values. For instance you could map both background AND font colour in the map. Such as:
You can then add hover to the loop and darken the background mapped to
$bg
by whatever you need. From what I understand this should achieved the desired result.