Role of CSS :only-of-type Selector


Use the CSS :only-of-type selector to style every <p> element that is the only <p> element of its parent.

Example

You can try to run the following code to implement the :only-of-type selector

Live Demo

<!DOCTYPE html>
<html>
   <head>
      <style>
         p:only-of-type {
            background: orange;
            color: white;
         }
      </style>
   </head>
   <body>
      <div>
         <p>This is demo text 1.</p>
      </div>
      <div>
         <p>This is demo text 2.</p>
         <p>This is demo text 3.</p>
         <p>This is demo text 4.</p>
      </div>
   </body>
</html>

Updated on: 01-Jul-2020

63 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements