2

Say in my main.less file which gets compiled into main.css, I have the following:

.section1 {
    .col-xs-1, .col-sm-1, .col-md-1, .col-lg-1,
    .col-xs-2, .col-sm-2, .col-md-2, .col-lg-2,
    .col-xs-3, .col-sm-3, .col-md-3, .col-lg-3,
    .col-xs-4, .col-sm-4, .col-md-4, .col-lg-4,
    .col-xs-5, .col-sm-5, .col-md-5, .col-lg-5,
    .col-xs-6, .col-sm-6, .col-md-6, .col-lg-6, 
    .col-xs-7, .col-sm-7, .col-md-7, .col-lg-7,
    .col-xs-8, .col-sm-8, .col-md-8, .col-lg-8,
    .col-xs-9, .col-sm-9, .col-md-9, .col-lg-9,
    .col-xs-10, .col-sm-10, .col-md-10, .col-lg-10,
    .col-xs-11, .col-sm-11, .col-md-11, .col-lg-11,
    .col-xs-12, .col-sm-12, .col-md-12, .col-lg-12
    {
        border: 1px solid green;
    }
}
.section2 {
    .col-xs-1, .col-sm-1, .col-md-1, .col-lg-1,
    .col-xs-2, .col-sm-2, .col-md-2, .col-lg-2,
    .col-xs-3, .col-sm-3, .col-md-3, .col-lg-3,
    .col-xs-4, .col-sm-4, .col-md-4, .col-lg-4,
    .col-xs-5, .col-sm-5, .col-md-5, .col-lg-5,
    .col-xs-6, .col-sm-6, .col-md-6, .col-lg-6, 
    .col-xs-7, .col-sm-7, .col-md-7, .col-lg-7,
    .col-xs-8, .col-sm-8, .col-md-8, .col-lg-8,
    .col-xs-9, .col-sm-9, .col-md-9, .col-lg-9,
    .col-xs-10, .col-sm-10, .col-md-10, .col-lg-10,
    .col-xs-11, .col-sm-11, .col-md-11, .col-lg-11,
    .col-xs-12, .col-sm-12, .col-md-12, .col-lg-12
    {
        border: 1px solid red;
    }
}

Clearly this is already becoming unwieldy, excessively cluttering my main.less file and adding duplication of sorts which could cause multiple unnecessary changes.

How can I improve this? Is there a way I could for example do something like this:

.allCellTypes 
{
    .col-xs-1, .col-sm-1, .col-md-1, .col-lg-1,
    .col-xs-2, .col-sm-2, .col-md-2, .col-lg-2,
    .col-xs-3, .col-sm-3, .col-md-3, .col-lg-3,
    .col-xs-4, .col-sm-4, .col-md-4, .col-lg-4,
    .col-xs-5, .col-sm-5, .col-md-5, .col-lg-5,
    .col-xs-6, .col-sm-6, .col-md-6, .col-lg-6, 
    .col-xs-7, .col-sm-7, .col-md-7, .col-lg-7,
    .col-xs-8, .col-sm-8, .col-md-8, .col-lg-8,
    .col-xs-9, .col-sm-9, .col-md-9, .col-lg-9,
    .col-xs-10, .col-sm-10, .col-md-10, .col-lg-10,
    .col-xs-11, .col-sm-11, .col-md-11, .col-lg-11,
    .col-xs-12, .col-sm-12, .col-md-12, .col-lg-12
}
.section1 .allCellTypes 
{
    border: 1px solid green;
}
.section2 .allCellTypes 
{
    border: 1px solid red;
}

Thanks

Edit 1: As per @Alessandro Minoccheri's suggestion, I compiled but the resulting css was as follows:

.section1 .allCellTypes 
{
    border: 1px solid green;
}
.section2 .allCellTypes 
{
    border: 1px solid red;
}

Any mention of the classes that would be covered by .allCellTypes are completely omitted. Perhaps there are two reasons for this?

  1. The definitions for the classes which would be grouped into .allCellTypes are defined elsewhere (within compiled bootstrap files).
  2. I compile the .less files using lessphp, perhaps it has a bug?

Edit 2: I placed the following code in the demo window on the lessphp page...

.cell1 {
  background-color: red;
}
.cell2 {
  background-color: green;
}
.cell3 {
  background-color: blue;
}
.allCellTypesClass {
  .cell1;
  .cell2;
  .cell3;
}
.section1 {
  .allCellTypes {
    .allCellTypesClass;
    border: 1px solid red;
  }
}

and I got the following result:

.cell1 {
  background-color: red;
}
.cell2 {
  background-color: green;
}
.cell3 {
  background-color: blue;
}
.allCellTypesClass {
  background-color: red;
  background-color: green;
  background-color: blue;
}
.section1 .allCellTypes {
  background-color: red;
  background-color: green;
  background-color: blue;
  border: 1px solid red;
}

So I'm afraid your answer, @Alessandro Minoccheri, is not doing what I wanted!

Jodes
  • 14,118
  • 26
  • 97
  • 156

3 Answers3

3

The gist of what I do is as follows:

My Mixin:

.red-border () {
   border:@value @value @value;
}

Applying:

.section-2 [class*="col-"] {
  .red-border;
}

If you don't want to affect nested columns, then:

.section-2 > [class*="col-"] {
  .red-border;
}

If you only want a certain column breakpoint:

.section-2 > [class*="col-sm-"] {
  .red-border;
}

If you don't want to affect forms (haven't tested this but should work fine).

.section-2 > [class*="col-"]:not(form) {
  .red-border;
}
Christina
  • 34,296
  • 17
  • 83
  • 119
1

Just in case, you can get it in LESS like this:

.section1 {
    .allCells; .allCellsProperties() {
        border: 1px solid green;
    }
}

.section2 {
    .allCells; .allCellsProperties() {
        border: 1px solid red;
    }
}

.allCells() {.col- {&xs-, &sm-, &md-, &lg- {
    &1, &2, &3, &4, &5, &6, &7, &8, &9, &10, &11, &12 {
        .allCellsProperties();
    }
}}}

But indeed, the attribute-selector-based solution suggested by @cab is more clean and less bloating.

seven-phases-max
  • 11,765
  • 1
  • 45
  • 57
0

You can create a class .allCellTypes and add all class that you want that inherited.After you can call this class and add other property you can write this:

.allCellTypesClass 
    {
    .col-xs-1; 
    .col-sm-1; 
    .col-md-1;
    .col-lg-1;
    .col-xs-2; 
    .col-sm-2; 
    .col-md-2;
    .col-lg-2;
    .col-xs-3; 
    .col-sm-3; 
    .col-md-3;
    .col-lg-3;
    .col-xs-4; 
    .col-sm-4; 
    .col-md-4; 
    .col-lg-4;
    .col-xs-5; 
    .col-sm-5; 
    .col-md-5; 
    .col-lg-5;
    .col-xs-6; 
    .col-sm-6; 
    .col-md-6; 
    .col-lg-6; 
    .col-xs-7; 
    .col-sm-7; 
    .col-md-7; 
    .col-lg-7;
    .col-xs-8; 
    .col-sm-8; 
    .col-md-8; 
    .col-lg-8;
    .col-xs-9; 
    .col-sm-9; 
    .col-md-9; 
    .col-lg-9;
    .col-xs-10; 
    .col-sm-10; 
    .col-md-10;
    .col-lg-10;
    .col-xs-11; 
    .col-sm-11; 
    .col-md-11; 
    .col-lg-11;
    .col-xs-12; 
    .col-sm-12; 
    .col-md-12; 
    .col-lg-12;
}

.section1
{
  .allCellTypes 
    {
       .allCellTypesClass;
       border: 1px solid green;
    }    
}
.section2 
{
    .allCellTypes 
    {
       .allCellTypesClass ;
       border: 1px solid red;
    } 
}
Alessandro Minoccheri
  • 35,521
  • 22
  • 122
  • 171
  • Cool, +1. Have some link with documentation, like mozilla or w3c? I've searched but not found. Thanks! – Protomen Dec 27 '13 at 18:16
  • 1
    is the property of less where you can concatenate more than a class see this lesscss.org @GuilhermeNascimento – Alessandro Minoccheri Dec 27 '13 at 18:18
  • Thanks, I thought this was pure css, thanks for clarifying me. +1 – Protomen Dec 27 '13 at 18:20
  • Thanks for your answer - unfortunately it has not worked, I have clarified in my answer what I am doing to get the incorrect result, perhaps you could explain how I could reproduce your success? – Jodes Dec 27 '13 at 18:33
  • edit answer try now please... I think isn't a bug of lessphp because I have work with that library and this functionality works fine for me. be sure that the other file is called before this file to include that class @Jodes – Alessandro Minoccheri Dec 27 '13 at 18:38
  • Thank you, I have tried again but the css output was no different – Jodes Dec 27 '13 at 18:43
  • ok be sure that the other file with the class to include is called before your declaration of css and check into your net console if that file is called. @Jodes – Alessandro Minoccheri Dec 27 '13 at 18:43