Hia
Hia

Reputation: 29

I cannot change the paragraph color in my html

p {
  color: #7FE330;
}
<p><center> My Paragraph </center></p>

I have been trying to change my paragraph color but it is just not working. This code above is what i been doing.

Upvotes: 2

Views: 1544

Answers (3)

Daniel Netzer
Daniel Netzer

Reputation: 2232

p {
  color: red;
}
<center>
  <p>test</p>
</center>

The center tag cannot be encapsulated inside a tag containing text nodes such as <p>, or <span>.

Changing the order will solve your problem.

It's also important to mention that the <center> tag isn't supported in HTML5 and you should use CSS to achieve the desired result according to W3C.

To align the text to the center of the container you can use the following style text-align:center instead of the deprecated center tag.

<style>
  p {
    color: red;
    text-align: center;
  }
</style>
<p>Hello World ;)</p>

Upvotes: 7

Salman Arshad
Salman Arshad

Reputation: 272106

Your HTML is broken because the <p> element cannot contain a block level element. Quote:

Paragraphs are block-level elements, and notably will automatically close if another block-level element is parsed before the closing </p> tag.

The browser tries to fix your HTML as follows:

element inspector

The correct solution is to stop using the obsolete <center> element and use CSS to center content.

Upvotes: 2

Pushprajsinh Chudasama
Pushprajsinh Chudasama

Reputation: 7949

You can change the style of tag p to center.

  <style>
  center {
     color:#7FE330
   }
  </style>
      
 <p><center> My Paragraph </center></p>

Upvotes: 0

Related Questions