John Cargo
John Cargo

Reputation: 2121

Regular Expression in CSS - Stylesheets

I just found that we can use regular expression in CSS, and found this helping URL :- http://www.w3.org/TR/selectors/#attribute-substrings

But I couldnot find my answer over there.

My Question is can i use something like

s* {
 margin : $1px;
}

* for any data there and using it with $1.

That says, Whatever is available after s put in before px.

Thanks

Upvotes: 0

Views: 924

Answers (3)

CupawnTae
CupawnTae

Reputation: 14580

Firstly, you're misinterpreting the spec. You can't use patterns like that. You can do things like

*[attr*="string"]

and it will match any element which has an attribute attr with string somewhere in the value. However, the first * just means "any element" here, and the second * means "substring anywhere in the value". There's no actual regular expression pattern matching going on, so the matched value would always be string in this case.

And secondly, no, you can't do any kind of substitution like that in CSS. If you want to do that kind of thing you might want to look into something like LESS, Sass or Stylus.

Upvotes: 0

FelipeAls
FelipeAls

Reputation: 22161

No you can't: a selector will match or not according to a regular expression and that's it.
You can't reuse it in a declaration as part of a value as in your example.

You could use a preprocessor (LESS, Sass or Stylus) with a for loop if you want to write something like:

.mt1 { margin-top: 10px }
.mt2 { margin-top: 20px }
.mt3 { margin-top: 30px }

I'm not even sure there's a for loop in each of them (because it's barely needed)

Upvotes: 0

pr0metheus
pr0metheus

Reputation: 488

You can do something like this

#container div[id^='a'] {
    border: 1px solid black;  
}

Check: W3

Upvotes: 1

Related Questions