In CSS, the :placeholder-shown and :not(:placeholder-shown) pseudo-classes are used to target and style form input elements based on whether their placeholder text is visible or not. These pseudo-classes are particularly useful for customizing the appearance of input elements depending on whether the placeholder text is displayed or hidden.
:placeholder-shown Pseudo-Class: This pseudo-class selects form elements that have their placeholder text currently displayed or visible. It is typically used with input elements such as text fields or textareas that have a placeholder attribute.
:not(:placeholder-shown) Pseudo-Class: Conversely, the :not(:placeholder-shown) pseudo-class selects form elements that do not have their placeholder text displayed, indicating that the user has interacted with the field or entered text.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Input pseudo-classes</title> <style> textarea { outline: none; border-radius: 2px; } textarea:placeholder-shown { border-color: #e93e3e; } textarea:not(placeholder-shown) { border-color: #74e93e; } </style> </head> <body> <h3>Input pseudo-classes</h3> <p>:placeholder-shown</p> <textarea placeholder="Enter Your Comments"></textarea> </body> </html>
The provided CSS code is used to style a <textarea> element based on whether it has placeholder text displayed (:placeholder-shown) or if the user has entered text (:not(:placeholder-shown)).
textarea: This selector applies styles to all <textarea> elements.
textarea:not(placeholder-shown): This selector targets <textarea> elements that do not have their placeholder text displayed, indicating that the user has interacted with the field or entered text.
Here's how this code works in practice:
This code enhances the user experience by providing clear visual feedback regarding the state of the <textarea> and its placeholder text.
These pseudo-classes allow you to provide visual cues to users about the state of input fields, making your forms more user-friendly and informative.
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions