0

How can I create a selector that matches any set of two specific strings with a dynamic string in between them?:

e.g.

[class*="animation-"**"-infinite"] {
    animation-iteration-count: infinite;
}

So it would be able to target these classes:

class="animation-hover-infinite"
class="animation-scale-infinite"
class="animation-opacity-infinite-somethingElse"
class="animation-etc-infinite withSomeOtherClass"

But not target this:

class="animation-something"
class="something-infinite"

Is this possible? Or are there other CSS selectors that can make this happen?

2
  • Unfortunately that is not something that CSS attribute selectors support. You can see the currently supported ones on the documentation: developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors Commented Aug 16, 2017 at 13:06
  • You will need JavaScript for this. Commented Aug 16, 2017 at 13:23

1 Answer 1

2

You can check for start and end, if that helps.

div[class^="animation-"][class$="-infinite"] {
  animation-iteration-count: infinite;
  background: red;
}

div {
  width: 100px;
  height: 100px;
  background: blue;
  margin-bottom: 1em;
}
<div class="animation-blabla-infinite"></div>
<div class="animation-blabla-infinites"></div>
<div class="zanimation-blabla-infinite"></div>

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

4 Comments

Thanks for this. Although this only allows it if the class is specifically has animation-blabla-infinite. Is it possible to make it work with class="animation-blabla-infinite another-class"
This do not work for class="animation-opacity-infinite-somethingElse"
@ehsan and that wasn't the question. any set of two specific strings with a dynamic string in between them. I didn't even look at the examples. Now that I do, they don't match the initial request.
I would probably use *="animation-" and *="-infinite" because op said with something in between. I think that would be the closest thing you could achieve with only css. In your code it won't work if there's another classname at the end or start.

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.