CSS :placeholder-shown Selector

The CSS :placeholder-shown pseudo-class selects the input element or textarea element that is having placeholder text. The main difference between :placeholder-shown and ::placeholder is that :placeholder-shown is for selecting the input itself when the placeholder text is being shown whereas ::placeholder is about giving style to the placeholder text.

Syntax:

:placeholder-shown
{
   /* CSS Property */ 
}

Example 1: Below is the example that illustrates the use of :placeholder-shown property.

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <style>
        input {
            border: 1px solid black;
            padding: 10px;
        }
  
        input:placeholder-shown {
            border-color: rgb(255, 114, 58);
            color: rgb(24, 187, 97);
        }
    </style>
</head>
  
<body>
  
    <h1 style="color: green;">GeeksforGeeks</h1>
    <input placeholder="Computer Science portal !">
</body>
  
</html>

chevron_right


Output:



Example 2: Below is the example that illustrates the use of :placeholder-shown property.

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html lang="en">
  
<head>
    <style>
        input.studentid:placeholder-shown {
            background-color: yellow;
            color: red;
        }
    </style>
</head>
  
<body>
    <h1 style="color: green;">GeeksforGeeks</h1>
  
    <form>
        <p>
            <label for="branch">Enter Geek Name:</label>
            <input id="branch" placeholder="Geek Branch" />
        </p>
  
        <p>
            <label>Enter Geek ID:</label>
            <input type="number" pattern="[0-9]{7}" 
            class="studentid" placeholder="7 digit id" />
        </p>
    </form>
</body>
  
</html>

chevron_right


Output:

Supported Browsers:

  • Chrome
  • Firefox
  • Safari
  • Opera
  • Edge
  • Internet Explorer (Not Supported).



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.