Home  >  Article  >  Web Front-end  >  How to specify that a style only applies to that element's parent element and that element's child elements in HTML?

How to specify that a style only applies to that element's parent element and that element's child elements in HTML?

PHPz
PHPzforward
2023-08-26 08:01:151282browse

How to specify that a style only applies to that elements parent element and that elements child elements in HTML?

Use the scoped attribute to specify that the style applies only to the parent element and its children -

Example

<!DOCTYPE html>
<html>
   <head>
      <style>
         h2 {
            color:yellow;
         }
      </style>
   </head>
   <body>
      <div>
         <style scoped>
            h2 {
               color:grey;
            }
         </style>
         <h2>Grey Heading</h2>
      </div>
      <h2>Yellow Heading</h2>
   </body>
</html>

The above is the detailed content of How to specify that a style only applies to that element's parent element and that element's child elements in HTML?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete