Nik
Nik

Reputation: 7182

Combine multiple bootstrap classes into one SASS class

In a form I have rows of form-groups with each having a label and form-control element.

<div class="form-group row">
  <label class="col-xs-2 col-md-3">Text</label>
  <div class="col-xs-10 col-md-9">
    <input class="form-control" type="text">
  </div>
</div>

Labels use classes: 'col-xs-2 col-md-3' and form-controls: 'col-xs-10 col-md-9'

Is it possible to combine the two 'col' classes of the label element into one class in sass? Something like this:

.label-width{
   .col-xs-2
   .col-md-3
}

Giving me:

<label class="label-width">Text</label>

Which then I could control with a sass variable such as: '$labelColumnWidth: 2' to give me a quick way to set label widths for all rows. Something like this:

$labelColumnWidth: 2
.label-width{
   .col-xs-#{$labelColumnWidth}
   .col-md-#{$labelColumnWidth}
}

I would like to use the bootstrap classes for column spacing but able to control them with sass variables to quickly change the layout of the form. Possible?

Upvotes: 16

Views: 9350

Answers (2)

d_z90
d_z90

Reputation: 1263

Even though it is not a good practice, you should be able to do this via @extend. In your case, your code should look like this:

.label-width{
   @extend .col-xs-2
   @extend .col-md-3
}

Have a look here if you need more explanation.

Upvotes: 10

Md. Abu Sayed
Md. Abu Sayed

Reputation: 2486

you can extend multiple class using multiple way like:

Way 1

.my-class {
   @extend .class1;
   @extend .class2;
   .
   .
   .
   @extend .classn;
}

way 2

one another way is you can extend multiple class in an single line here example

.my-class {
   @extend #{'.class1', '.class2', ... , '.classn'}
}

Know more details: Sass official document

Thanks

Upvotes: 6

Related Questions