8

I'm trying to apply styles to all my divs, except one specific. I'm doing this but it doesn't work:

#toolbar div[class~="olControlNavigationHistory"]{
   float: left;
   background-repeat: no-repeat;
   margin: 2px 12px 2px 12px;
}

So I need to apply this style to all the divs in #toolbar EXCEPT the div with a class called "olControlNavigationHistory".

How can I do this? Is this possible?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Fran Verona
  • 5,438
  • 6
  • 46
  • 85
  • Possible duplicate of [Apply CSS Style on all elements except with a SPECIFIC ID](https://stackoverflow.com/questions/19464660/apply-css-style-on-all-elements-except-with-a-specific-id) – bummi Nov 24 '18 at 14:22

2 Answers2

12

Just apply the rule to all divs first:

#toolbar div {
   float: left;
   background-repeat: no-repeat;
   margin: 2px 12px 2px 12px;
}

Then you need to zero the values out for the specific case:

#toolbar div.olControlNavigationHistor {
   float: none;
   background-repeat: repeat;
   margin: 0;
}

Of course this assumes that the property values that specific div would have had without the first rule applied are each properties defaults (such as margin: 0 and float: none.)

EDIT: However in the future when CSS3 is supported everywere, you could also just rewrite your original rule as #toolbar div:not(.olControlNavigationHistory) and it would work correctly and elegantly.

Prasad Rajapaksha
  • 6,118
  • 10
  • 36
  • 52
Marcus Whybrow
  • 19,578
  • 9
  • 70
  • 90
  • 8
    @user552669 For the record `~=` means contains, not "not equal" - [w3schools](http://www.w3schools.com/css/css_attribute_selectors.asp). However in the future when CSS3 is supported everywere, you could also just rewrite your original rule as `#toolbar div:not(.olControlNavigationHistory)` and it would work correctly and elegantly. – Marcus Whybrow Jan 04 '11 at 10:51
5

I was just doing the same and found this answer Use the :not selector:

div:not(#div1){
    color:red;
}

it was posted in Apply CSS Style on all elements except with a SPECIFIC ID

Community
  • 1
  • 1
mkkabi
  • 611
  • 8
  • 11