CSS :scope pseudo-class

A scope element forms a context for a block of styles. That element provides a reference point for selectors to be matched against. A scope element is defined using the scoped attribute. Styles declared with scoped attributes will be applied to all elements inside its parent element.

Syntax:

:scope

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  <title>GeeksforGeeks</title>
  <body>
   <h1 style="text-align: center;
             color: green;" id="paragra">
       Welcome to GeeksforGeeks. 
       It's a Computer Science portal.
   </h1>
   <p style="color: #000; text-align: center;" 
      id="opt">
   </p>
   <script>
     let para = 
        document.getElementById("paragra");
     let opt = 
        document.getElementById("opt");
     if (para.matches(":scope")) {
        opt.innerText = 
        "Yeah!!, we are under scope GeeksforGeeks";
     }
   </script>
  </body>
</html>

chevron_right


Output:



Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  <head>
    <title>GeeeksforGeeks</title>
     <style>
      #contains {
        margin: 5% auto;
        max-width: 500px;
        background-color: #eeeeee;
      }
      section {
        padding: 60px;
      }
      :scope {
        background-color: #3cd33c;
      }
      </style>
   </head>
  <body>
    <div id="contains">
      <section>
        <p>
          Inside the scope, 
          <span style="color: green;">
                GeeksforGeeks
           </span
        </p>
      </section>
    </div>
  </body>
</html>

chevron_right


Output:

Browser Support:

  • Chrome
  • Firefox
  • Opera
  • Safari
  • Edge



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.