Style every element that has no children with CSS


To style every <p> element that has no children with CSS, use the: empty selector t. You can try to run the following code to implement the: empty selector

Example

Live Demo

<!DOCTYPE html>
<html>
   <head>
      <style>
         p.demo {
            width: 300px;
            height: 20px;
            background: gray;
         }
         p:empty {
            width: 150px;
            height: 20px;
            background: orange;
         }
      </style>
   </head>
   <body>
      <p class = "demo">This is demo text. Below is empty text.</p>
      <p></p>
   </body>
</html>

Updated on: 30-Jun-2020

647 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements