3

This is the HTML:

<div class="class1">Class</div>
<div class="class2">Class</div>
<div class="class3">Class</div>

CSS is the following:

.class1, .class2, .class3 { ... }

I'm not sure if it's possible, but I'm trying to simplify the css.

Something like:

.class(from1to3) { ... }
  • 2
    could you not add a generic `class` class and a `id` to the divs? – treyBake Oct 26 '18 at 09:33
  • @ThisGuyHasTwoThumbs I get the classes from a script that automatically makes the name with numbers – Paul Borsan Oct 26 '18 at 09:35
  • @PaulBorsan ah I see ... :/ – treyBake Oct 26 '18 at 09:35
  • 1
    In contrary to what you suggest this certainly would _not_ be a simplification. Sure, that line might get a bit shorter. But as a tradeof you would make understanding the styling rules _much_ more difficult. Also simple text searches for the class name would _not_ produce hits any more. Taking all together: that is _not_ a good idea. Keep things transparent. Only exception: if those classes are created in a dynamic manner, but that would be questionable anyway... – arkascha Oct 26 '18 at 09:36
  • @arkascha Oh, okay! I will think of it. Thanks – Paul Borsan Oct 26 '18 at 09:43
  • hate when people mark question as favorite and not upvoting – Morpheus Oct 26 '18 at 09:57

4 Answers4

5

If the class name will be the same for all elements, but with different numbers, you can use Attribute Selectors

div[class^="class"],
div[class*="class"] {
  color: red;
}
<div class="class1">Class</div>
<div class="class2">Class</div>
<div class="class3">Class</div>
rrd
  • 5,789
  • 3
  • 28
  • 36
Carl Binalla
  • 5,393
  • 5
  • 27
  • 46
0

You can use an attribute selector.

div[class*='class'] {...}

but the best solution i think is:

.class1, .class2, .class3 { ... }

0

use div[class*="class"]

div[class*="class"] {
    background: #ffff00;
}
<div class="class1">Class</div>
<div class="class2">Class</div>
<div class="class3">Class</div>
Sooriya Dasanayake
  • 1,106
  • 1
  • 7
  • 14
0

Consider using SASS or another pre-processor, it would be easier.

Dorian B
  • 108
  • 10