7

A CSS "contains" selector is

td[class*="foo"]

I can select multiple classes with

td[class*="foo bar"]

This however will fail for <td class="foo baz bar" />

How can I do a CSS "contains" wildcard select?

BTW: I cannot use td.foo.bar

5
  • 2
    What do you mean by "fail"? It seems working to me jsbin.com/wufot/1/edit Though that's not the correct way of selecting the classes. Commented Mar 13, 2014 at 17:17
  • 1
    @HashemQolami is right.Can you give an example where it fails? Commented Mar 13, 2014 at 17:20
  • 1
    td[class~="foo"][class~="bar"] will match <td class="foo bar"> exactly and will not match <td class="foobar bar"> Commented Mar 13, 2014 at 17:20
  • My bad - I meant it would fail for <td class="foo baz bar" /> - I updated the question Commented Mar 13, 2014 at 21:55
  • Is there a specific reason you cannot use class selectors over attribute selectors? Are you working with a markup language that does not have class semantics in the form of a class attribute? Commented Mar 14, 2014 at 4:11

3 Answers 3

9

The selector you're looking for is as follows, see this question for more details.

td[class*="foo"][class*="bar"]

However, if you need to use selectors like that then it's often a sign that your class name logic is bad.

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

2 Comments

I'm using it on media queries in email css. I would prefer to use different selectors but from what I know this is the only way to get it to work.
I would recommend against using *= for class names and other delimited values for the reason stated in Hashem Qolami's answer.
5

Honestly I don't know what you mean by "failing" td[class*="foo bar"] selector as it seems working to me in your particular case.

However, since the class names are separated by white spaces, you could use multiple [attr~=value] attribute selectors to select the elements having the classes as follows:

td[class~="foo"][class~="baz"] {
  background-color: gold;
}

WORKING DEMO.

From the MDN:

[attr~=value] Represents an element with an attribute name of attr whose value is a whitespace-separated list of words, one of which is exactly "value".

5 Comments

My bad - I meant it would fail for <td class="foo baz bar" /> - I updated the question
yes, td[class~="foo"][class~="baz"] works perfectly. What the difference to td[class*="foo"][class*="bar"]?
@Horen You are welcome. Using [class*="foo"] my cause unexpected issues as it selects <td class="foobar"> element as well. As andyb mentioned in his comment.
@Horen Take a look at this example. Toggle the selectors to see the difference.
@Horen: The only way for ~= attribute selectors to work where class selectors wouldn't in your case is if you were not actually working with HTML...
4

Visit : CSS-Tricks (CSS Attribute Selectors)

From the above for finding a match of a given string to the string in the class specified according to your question , the only option I find working and correct is * and ~.

1. Demo for *

* CSS-selector

2. Demo for ~

~ CSS-selector


Multiple attribute matches

Multiple attribute

1 Comment

You might as well post this in a comment instead.

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.