1

I read somewhere a class inside another class is written in this way: .class1 .class2 { style}. But I found it counld not work.

For example:

<style>
h1 p {
    background-color: yellow;
}
.a1 .a2{
     background-color: green;
}
</style>

<h1><p>Welcome to My Homepage</p></h1>

 <p class="a1">
   <p class="a2"> My name is Donald  </p> 
   <p class="a3"> I live in Duckburg.</p>
 </p>

As you can see, the following is supposed to be green. But it actually not.

<p class="a1"><p class="a2"> My name is Mike  </p> </p>

Am I missing anything here?

derek
  • 9,358
  • 11
  • 53
  • 94

2 Answers2

1

You can't nest <p/> elements. Paragraphs can only contain phrasing content.

Change the outer paragraph to a <div/> and the styles will be applied correctly:

h1 p {
  background-color: yellow;
}

.a1 .a2 {
  background-color: green;
}
<h1>
  <p>Welcome to My Homepage</p>
</h1>

<div class="a1">
  <p class="a2"> My name is Donald </p>
  <p class="a3"> I live in Duckburg.</p>
</div>
Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
1

It's not an issue with the CSS, but rather the HTML: You're nesting a <p> tag inside of another <p> tag.

See https://stackoverflow.com/a/12015809/7024837

Maybe you can try changing <p class="a1"> into <span class="a1">?

Community
  • 1
  • 1