How to specify that the styles only apply to this element's parent element and that element's child elements in HTML?


Use the scoped attribute to specify that the styles only apply to the parent element and the element’s child elements −

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>

seetha
seetha

e

Updated on: 03-Mar-2020

162 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements