2

I'd like to apply the green background to paragraphs with classes and the red one to all the others. I've made it half way through but still struggling with empty classes class="":

p {background:green}

#test p:not([class]) {
  background: red;
  color: #fff;
}
<div id="test">
  <p>This should be red</p>
  <p class="abc">This should be green</p>
  <p class="newname">This should be green</p>
  <p class="">This should be red</p>
  <p class="anothername">This should be green</p>
  <p>This should be red</p>
  <p class="">This should be red</p>
</div>

Fiddle

Anyone knows a way?

VXp
  • 11,598
  • 6
  • 31
  • 46
Suresh Ponnukalai
  • 13,820
  • 5
  • 34
  • 54

2 Answers2

6

You can target a p with an empty class like this...

p[class=""] {
    ...
}

body {
  font-weight: 700;
}

p {
  background: green
}

#test p:not([class]),
#test p[class=""] {
  background: red;
  color: #fff;
}
<div id="test">
  <p>This should be red</p>
  <p class="abc">This should be green</p>
  <p class="newname">This should be green</p>
  <p class="">This should be red</p>
  <p class="anothername">This should be green</p>
  <p>This should be red</p>
  <p class="">This should be red</p>
</div>
sol
  • 22,311
  • 6
  • 42
  • 59
1

I used #test p[class=""] i hope thats what you wanted.

body{
  font-weight:700;
}
p{background:green}

#test p:not([class]) {
  background:red;
  color:#fff;
}

#test p[class=""]{
  background:red;
  color:#fff;
}
Pedro Bauer
  • 19
  • 1
  • 6