Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent CSS code duplication inside media queries?

Tags:

css

I am trying to develop my own grid system. This is my first attempt so maybe I am missing something. Here is my CSS:

.column-1 {
  width: 6.86666666667%;
}

.column-2 {
  width: 15.3333333333%;
}

// More such columns

@media only screen and (max-width: 768px) {
  .column-s-1 {
    width: 6.86666666667%;
  }
  .column-s-2 {
    width: 15.3333333333%;
  }
}

As you can see the values are duplicated but class names are different. Is there any way I can avoid this duplication because it will become more and more complex with each additional class.

like image 599
SanJeet Singh Avatar asked Mar 01 '26 08:03

SanJeet Singh


1 Answers

You can avoid some of duplication by grouping selectors:

.column-1,
.column-s-1 {
  width: 6.86666666667%;
}

.column-2,
.column-s-2 {
  width: 15.3333333333%;
}

// More such columns
@media only screen and (max-width: 768px) {
  .column-s-1 {
    /* only properties characteristic for this width*/
  }
}

Another option is to use LESS or SASS

like image 94
lukbl Avatar answered Mar 05 '26 06:03

lukbl