302

If the HTML has elements like this:

id="product42" id="product43" ... 

How do I match all of those id's starting with "product"?

I've seen answers that do this exactly using javascript, but how to do it with only CSS?

5 Answers 5

539
[id^=product] 

^= indicates "starts with". Conversely, $= indicates "ends with".

The symbols are actually borrowed from Regex syntax, where ^ and $ mean "start of string" and "end of string" respectively.

See the specs for full information.

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

6 Comments

Thanks for the method and explanation, I've edited my question so its more clear. Out of curiosity, is there a way to match a string inside of the id's string?
See the specs, they explain it better than I could!
@itamar: I appreciate your attempt at editing my answer, but quote are only required if the value contains characters that are not a valid identifier. product is clearly a valid identifier, and therefore needs no quotes.
The specificity of this type of selector is very low
@Emerald214 :not([id^=product])
|
91

I'd do it like this:

[id^="product"] { ... } 

Ideally, use a class. This is what classes are for:

<div id="product176" class="product"></div> <div id="product177" class="product"></div> <div id="product178" class="product"></div> 

And now the selector becomes:

.product { ... } 

1 Comment

@Blender, thanks, I chose the other answer because it explains a bit more to me and understand the symbols being used. I can't use classes for this scenario, otherwise, yes it would be nicer.
8

Use the attribute selector

[id^=product]{property:value} 

Comments

4

I want to share this solution too, maybe in the future it could help someone.
As the others said you can write [id^=product] for id

But we can give an example for the class as well: [class^="product-"] which indicates classes starts with product and also * like this [class*="product-"]

This is a simple example :

/* Icons */ [class^="icon-"], [class*=" icon-"] { /* use !important to prevent issues with browser extensions that change fonts */ font-family: 'mk-font' !important; font-size: 3em; } 

good luck ...

Comments

1

I noticed that there is another CSS selector that does the same thing . The syntax is as follows :

[id|="name_id"] 

This will select all elements ID which begins with the word enclosed in double quotes.

3 Comments

How did you notice? reference?
Doc for this here : w3.org/TR/selectors-3/#attribute-selectors This should select all id which start or equal to "name_id"
att|=val is NOT the same as att^=val. From the reference mentioned : The |= selector "Represents an element with the att attribute, its value either being exactly "val" or beginning with "val" immediately followed by "-". " So an id like "product42" would not be matched by |=, but "product-42" would.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.