0

I want to apply the css only for the headings h2 which have no class. There are different classes with some h2 and some h2 has no class.

I have tried like this:

h2[class*=""]{
  background: #cccccc;
}

But seems it's not working.

Navin Rauniyar
  • 10,127
  • 14
  • 45
  • 68
  • possible duplicate of [Can I write a CSS selector selecting elements NOT having a class?](http://stackoverflow.com/questions/9110300/can-i-write-a-css-selector-selecting-elements-not-having-a-class) – Calvin Scherle Dec 05 '14 at 09:30

4 Answers4

4

You can use like this:

h2:not([class]){
  background: #cccccc;
}
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
1

The following will apply css to all h2 elements that do not have a class:

h2:not([class]) {

}
at.
  • 50,922
  • 104
  • 292
  • 461
0

Its very simple actually. Try

h2
{
//css here
}

EDIT

For applying this for ONLY elements without classes, you have your answer here.

The problem in your code is that asterisk.

Community
  • 1
  • 1
Emre Acar
  • 920
  • 9
  • 24
  • That adds the CSS to all h2 elements, including those which already have a class assigned. – Calvin Scherle Dec 05 '14 at 09:30
  • @CalvinScherle You're right. I might have misunderstood the question, i edited my answer. – Emre Acar Dec 05 '14 at 09:34
  • You might want to note that the answer in your provided link only works if the `class` attribute is present. If the element has no class attribute at all, then that won't work either (not sure if that's the case for OP though) – Calvin Scherle Dec 05 '14 at 09:35
0

For example:

<ul>
  <li class="class1">Hallo</li>
  <li class="class2">Hallo</li>
  <li>Hallo</li>
  <li class="class3">Hallo</li>
  <li>Hallo</li>
</ul>

This css, styles every li that has no class

li{
  color:blue;
}

li:not([class]) {
  color:red;
  font-weight:bold;
}

For more information read here

Nerdkowski
  • 445
  • 5
  • 16
  • sorry i didnt mean to be rude. in your initial answer you just copied an answer from the duplicate question without even reading what it means. +after your edit, your answer is still incorrect... – Banana Dec 05 '14 at 09:47
  • your answer works, but is not correct. he wants to style elements which dont have any class at all, not a specific one. – Banana Dec 05 '14 at 09:54
  • much better now, +1 from me – Banana Dec 05 '14 at 10:09