1

I need to have 3 classes as follow:

.class-4, .class-5, .class-6 {
    color: pink;
}

And it works perfectly. Let's say I need the same but for 100:

.class-4, .class-5, .... .class-100 {
    color: pink;
}

Is there anything similar to this or any other way to do this which I can use.

.class->3<101 {
    color: pink;
}

To get the same result without writing 97 times the class and the comma?

10
  • 1
    What situation would ever need that many individual classes? Anyways try [class^="class-"] Commented May 14, 2022 at 8:53
  • If you find yourself writing 100 classes for the same style then you need to rethink your logic. Something else need to be fixed instead of finding how to do this. Commented May 14, 2022 at 8:53
  • Many images numbered from a php function, depending on the number of the image need a different size, for example 1 to 4 width 800px, 5 to 12 400px, 13 to 29 200px and so on till 100, so not really 100 the same but many classes to be repeated. Please explain [class^="class-"] Commented May 14, 2022 at 9:01
  • 1
    Many images numbered from a php function, depending on the number of the image need a different size --> this is what need to be fixed, your PHP function to generate a better HTML markup. Updated the logic of your PHP function, don't try to implement this logic in CSS Commented May 14, 2022 at 9:06
  • In this project I am not allowed to modify php Commented May 14, 2022 at 9:08

4 Answers 4

1

There is nothing in pure CSS which will do this, but you could use JavaScript to create a stylesheet for you which has all that tedious repetition created automatically.

In this snippet you say what the ends of the class ranges are and what styling is to be put in each of the ranges.

If there is a range which you don't want to alter then you still need to include it but make its styles string just an empty string.

The snippet runs through each of the ranges creating the relevant style sheet entries and puts them in a style element in the head element of the document.

A few fairly random divs are shown here just to test that we are hitting the right ranges.

const rangeEnds = [4, 20, 35, 41, 48, 100];
const styles = ['color: pink;', 'color: red; background-color: black;', 'color: green;', 'color: yellow;', 'color: blue;', 'color: black; background: pink;'];

let lastRangeEnd = 0;
const styleEl = document.createElement('style');
for (let i = 0; i < rangeEnds.length; i++) {
  for (let j = lastRangeEnd + 1; j < rangeEnds[i]; j++) {
    styleEl.innerHTML += '.class-' + j + ',';
  }
  styleEl.innerHTML += '.class-' + rangeEnds[i] + '{' + styles[i] + '}';
  lastRangeEnd = rangeEnds[i];
}
document.querySelector('head').append(styleEl);
<!doctype html>
<html>

<head>
  <title>Classes</title>
</head>

<body>
  <div class="class-1">ABCD</div>
  <div class="class-19">ABCD</div>
  <div class="class-21">ABCD</div>
  <div class="class-40">ABCD</div>
  <div class="class-41">ABCD</div>
  <div class="class-48">ABCD</div>
  <div class="class-100">ABCD</div>
</body>

Sign up to request clarification or add additional context in comments.

1 Comment

Great solution, thanks to everybody anyway for the general learning
0

If all elements will have the same property which is {color:pink} You can create only one class (lets call it .pink)

.pink {
    color: pink;
}

and then you can simply give your elements the .pink class.

1 Comment

Hello, you should rephrase your answer, as is , your advise is not understood ;)
0

One of class attribute's main purpose is to define a shared style reference name. It is rather not a very good practice to want to reference multiple class references and let them share the same styling.

The best way to get around this is to have a common class attribute name YourClassName. This way, any element you want the styling applied to can have that class appended to its class attribute through element.classList.add(YourClassName) with JS. And, that would solve all the hussle of having to worry about putting multiple classe names and I cannot think of any 1 situation that would force you to declare each element class separated by commas provided that they are to receive the same styling.

Comments

0

The OP asks if it’s possible to have a ‘number range’ (array) at the end of CSS classes that shares the same name, but ending on 1, 2, 3, etc.

As @zer00ne pointed out; You can target multiple classes with one "class". When defining your class selector - leave out the numbers, but make the class name unique.

So, if the class names are i.e. my-row-class-1, my-row-class-2, etc., write the selector like this;

[class^="my-row-class-"] {
    color: pink;
}

Pro tip: Instead of using class^= selector, it's possible to do this for id^= as well - and more. Check out the MDN web docs for more info.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.