Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SASS Customize Class Names with Variables

Tags:

css

sass

Is there any way to customize the variables in SASS? For example:

.m-b-{$number} {
    margin-bottom: $number;
}

If I give class="m-b-50" to an element, it should take margin-bottom 50. I just want to know if it is possible with SASS.

like image 231
raghuveer999 Avatar asked Jun 24 '17 10:06

raghuveer999


People also ask

How to use Sass variables in CSS?

Use Sass variables in situations where you want static values and CSS custom properties in situations where you want dynamic values. Sass variables can be used as CSS custom property values by wrapping the Sass variable in # {}.

Should I use CSS custom properties instead of sass Vars?

I particularly prefer to use CSS custom properties instead of SASS variables always when possible, mostly because of its reactivity. For this post I'll assume that you're already comfortable with CSS Custom Properties and SASS Vars (with SCSS Syntax). Sometimes the use of CSS Custom Properties is not a case of taste, but a necessity.

Why are there hyphens in my variable names in Sass?

This is a historical holdover from the very early days of Sass, when it only allowed underscores in identifier names. Once Sass added support for hyphens to match CSS ’s syntax, the two were made equivalent to make migration easier. Normally when you assign a value to a variable, if that variable already had a value, its old value is overwritten.

Are variables global or local in Sass?

Variables declared at the top level of a stylesheet are global. This means that they can be accessed anywhere in their module after they’ve been declared. But that’s not true for all variables. Those declared in blocks (curly braces in SCSS or indented code in Sass) are usually local, and can only be accessed within the block they were declared.


2 Answers

Yes it is possible with the help of variable interpolation or variable substitution which uses #{} for variable substitution in SASS and mixins which is a block of code just like function.

Interpolation is the process of evaluating an expression or a string containing one or more variables, yielding a result in which the variables are replaced with their corresponding values.

Simple example of interpolation and set values to the css property in SASS:

$number:60;
$n: 20px;

.m-b-#{$number}{
  margin-bottom: #{$number}px;
  margin-top: $n;
}

To create customize class names, will use mixins:

@mixin margin-class($side, $number) {
  $firstLetter: str-slice($side, 0, 1);
  .m-#{$firstLetter}-#{$number}{
     margin-#{$side}: #{$number}px;
  }
}

$margins: (10, 20);
$sides: ("top", "right", "bottom", "left");
@mixin generate-margin(){
   @each $margin in $margins{
      @each $side in $sides{
        @include margin-class($side, $margin);
      }
   }
}

@include generate-margin();

Here, generate-margin() will get executed which will call margin-class() for each $margins and $sides, and will generate the below CSS classes:

.m-t-10 {
  margin-top: 10px;
}

.m-r-10 {
  margin-right: 10px;
}

.m-b-10 {
  margin-bottom: 10px;
}

.m-l-10 {
  margin-left: 10px;
}

.m-t-20 {
  margin-top: 20px;
}

.m-r-20 {
  margin-right: 20px;
}

.m-b-20 {
  margin-bottom: 20px;
}

.m-l-20 {
  margin-left: 20px;
}

That's the one way when you want only for specific values, but if you want to create margin class for 0-20, you can loop thru 0 to 20 as shown below:

@mixin generate-margin(){
  @for $margin from 1 through 20{
    @each $side in $sides{
      @include margin-class($side, $margin);
    }
  }
}
like image 193
Pawan Kumar Avatar answered Sep 19 '22 22:09

Pawan Kumar


For anyone else facing this issue, here is how one can achieve this:-

@for $i from 1 through 10 {
  .mb-#{$i} {
     margin-bottom: #{$i}rem;
  }
}
like image 23
kks1 Avatar answered Sep 21 '22 22:09

kks1