0

Not sure if this is possible or if I'm just not asking the right questions, but I'm looking to apply a global rule for a set of classes that have different suffixes.

ie.

.gallery {} would like these rules to apply also to .gallery-1, .gallery-2, gallery-3 {} etc... Without having to add those actual specific classes to my stylesheet each time a new gallery is made.

Does anyone know if this is possible?

with thanks.

Eric Brockman
  • 824
  • 2
  • 10
  • 37

4 Answers4

1

You can use wildcards with attribute selectors to do just that. Something like this should work for your case:

[class*='gallery-'] { do:something; }

See here for more info: https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors

Note the "Summary" section in the link above, it describes the different behavior of the "wildcard" symbols.

Dryden Long
  • 10,072
  • 2
  • 35
  • 47
1

You could use the attribute selectors. Possibilities include:

  • [class|='gallery'] - matches all elements whose class attribute is exactly gallery, or begins gallery-
  • [class^='gallery'] - matches all elements whose class attribute starts with gallery

Note that I'm not clear what happens if your element has more than one class, as class="some-class gallery-1"

Chowlett
  • 45,935
  • 20
  • 116
  • 150
  • Thanks Chowlett, having more than one class does not seem to matter, in my case anyways. – Eric Brockman Mar 21 '14 at 15:42
  • `[class|='gallery']` and `[class^='gallery']` don't select multiple classes if the other class is first http://jsfiddle.net/k6Nqg/ –  Mar 21 '14 at 15:45
  • On the other hand, `[class*='gallery']` may be too heavy-handed http://jsfiddle.net/k6Nqg/2/ –  Mar 21 '14 at 15:49
0

A simple alternative would be to simply apply two classes to your html elements:

class="gallery gallery-1"

Here is a very similar question and answer Is there a CSS selector by class prefix?.

Community
  • 1
  • 1
marteljn
  • 6,446
  • 3
  • 30
  • 43
  • Thanks Marteljn, the problem is I'm using a WP gallery shortcode and the the classes are defined dynamically. The classes also have style attribute written for them in the WP core stylesheet which I'm trying to override. So even if I give it a separate class, I won't be able to overwrite the classes that are assigned in the WP core stylesheet. – Eric Brockman Mar 21 '14 at 15:32
  • The attribute selector answers should be workable for your case then. The only caveat is supporting older browsers. – marteljn Mar 21 '14 at 15:36
0

Use CSS Selectors
For your example, you'll need this:

[class^='gallery']

(get all elements with a class name beginning with gallery)

darkchico
  • 647
  • 7
  • 7