1

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

matthewelsom
  • 944
  • 1
  • 10
  • 21

2 Answers2

2

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:

//set your base colours
$colors: (
  green: green,
  blue: blue,
  orange: orange
);

//loop through your map and apply colours to the mapped values. This overrides the default where the additional class is applied. 
@each $colors, $specific in $colors {
  .portfolio-grid-item.#{$colors} {
    background: $specific;
  }
}

Your portfolio item:

// Gird Sizings
.portfolio-grid-item {
  height: $grid-item-height;   
  position: relative;
  text-decoration: none;
  float: left;
  width: 33.33%;
  //set default
  background: $grid-color;
}

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:

//set your base colours
$colors: 
  (green, white, green),
  (orange, white, orange);

//loop through your map and apply colours to the mapped values. This overrides the default where the additional class is applied. 
@each $color, $text, $bg in $colors {
  .portfolio-grid-item.#{$color} {
    background: $bg;
    color: $text;
  }
}

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.

Gerico
  • 5,079
  • 14
  • 44
  • 85
  • Thanks for this - let me see if I can get my head around it (this is all new to me)! – matthewelsom Jun 18 '15 at 10:01
  • 1
    If you have any questions let me know.The basic principle of the map is that the first value is the map key (i.e. the name by which all the values are associated) and the next value is the text colour, followed by the background color. Anything else can be appended and added to the map to contain more or less information. It's then assigned to a var when you run the loop. If you were to paste any of my examples and tweak values you should start to see how it's working. – Gerico Jun 18 '15 at 10:22
  • got it sussed, it all makes sense! Thank you! – matthewelsom Jun 18 '15 at 15:05
-1

Well, you can do something like this:

@mixin gridbg($bg) {
   @if $bg == blue {
   background: $blue;
 } @else if $bg == green {
   background: $green;
 } @else if $bg == orange {
   background: $orange;
 }
}

.orange {
  @include gridbg(orange);
}

.green {
  @include gridbg(green);
}

.blue {
  @include gridbg(blue);
}

Update

The above was just a simple example, but you can do much more with mixins: Here is a preview:

.orange {
    @include gridbg(orange);
    h3 {
      color: $grid-color
    }
    h5 {
      color: white;
      background: $orange;
    }
    &:hover {
        @include gridbghover(orange);
        h3 {
          color: $grid-color
        }
        h5 {
          color: white;
          background: darken($orange, 20%);
        }
    }
}

And a demo

Razvan B.
  • 6,721
  • 2
  • 19
  • 32
  • Thanks, I get this this approach - but it doesn't solve the issue - it only replaces the background attribute, I am trying to replace the variable so that it affects everything that uses the color - I added your code to my pen and forked it here... If you hover over a box you will see that the hover colors (derived from the same variable) do not change... this is my issue. – matthewelsom Jun 18 '15 at 09:21
  • http://codepen.io/anon/pen/MwEwbr – matthewelsom Jun 18 '15 at 09:21
  • Your update is good - so far this is the only option I can see in achieving the effect... but still its really complicated... I have 15 colors that can be applied, so writing 15 hunks of that code is something I am sure can be improved... in simple terms I am trying to say: (if class = .orange) = (overide all $grid-color with #ffa500) – matthewelsom Jun 18 '15 at 09:47
  • 1
    The downside to this method is its all manual. You might as well just use normal CSS. The only advance you gain is the variable containing the colour makes things a little easier, but on the whole this is just massively laborious. The OP needs maintainable code! – Gerico Jun 18 '15 at 09:50