1

I have three divs with following class:

<div class="page-search-site health-bundle-medical-group">Some text here</div>
<div class="health-bundle-clinic">Some text here 2</div>
<div class="page-search-site health-bundle-hospital">Some text here 3</div>

Here health-bundle- text is common for all three classes. So I want to write a CSS which will work for all these three classes, which will work if it matches the common part. Is it possible?

6 Answers6

1

div[class|=page-search-site-health-bundle] {
  color: red;
}
<div class="page-search-site-health-bundle-medical-group">Some text here</div>
<div class="page-search-site-health-bundle-clinic">Some text here</div>
<div class="page-search-site-health-bundle-hospital">Some text here</div>

Read More (https://developer.mozilla.org/en/docs/Web/Guide/CSS/Getting_started/Selectors)

rafaelcastrocouto
  • 11,781
  • 3
  • 38
  • 63
1

What you need is called attribute selector. An example, using your html structure, is the following:

div.page-search-site[class*='-bundle-medical-group']  {
    color:red 
}

In the place of div you can add any element or remove it altogether, and in the place of class you can add any attribute of the specified element.

div.page-search-site[class*='-bundle-medical-group']
div.page-search-site[class*='-bundle-hospital']
div.page-search-site[class*='-bundle-clinic']

Demo: https://jsfiddle.net/sajib_hassan/jku1fdh8/

More information on CSS attribute selectors, you can find here and here.

Sajib Hassan
  • 446
  • 4
  • 12
1

You're looking for a selctor that selects all elements with a list of class names beginning with health-bundle- or containing health-bundle- preceded by a space so you'll need to use attribute selectors, rather than class selectors, which don't support wildcards.

[class^="health-bundle-"],[class*=" health-bundle-"]{
    color:#f00;
}
<div class="page-search-site health-bundle-medical-group">Some text here</div>
<div class="health-bundle-clinic">Some text here 2</div>
<div class="page-search-site health-bundle-hospital">Some text here 3</div>
Shaggy
  • 6,696
  • 2
  • 25
  • 45
0
<div class="page-search-site-health-bundle type-medical-group">Some text here</div>
<div class="page-search-site-health-bundle type-clinic">Some text here</div>
<div class="page-search-site-health-bundle type-hospital">Some text here</div>
3rdthemagical
  • 5,271
  • 18
  • 36
0

Try this

div[class*="health-bundle"]{background:red;}
Head In Cloud
  • 2,001
  • 1
  • 8
  • 10
0

The CSS selector you're looking for looks like this.

div.page-search-site[class*=health-bundle]

It selects all div elements with class page-search-site and class attribute containing the substring health-bundle.

kamoroso94
  • 1,713
  • 1
  • 16
  • 19
  • I take it you didn't test this before posting it? It won't match *any* of the elements included in the question. – Shaggy Aug 05 '16 at 11:06
  • Ah, I see your point. I didn't think about the fact that the class attribute has all the classes so it would never work. Blame my laziness, but I'll fix this tomorrow; it's pretty late here. – kamoroso94 Aug 05 '16 at 11:11