Home > Web Front-end > CSS Tutorial > Can CSS Detect Text Input in External Pages Without JavaScript?

Can CSS Detect Text Input in External Pages Without JavaScript?

Mary-Kate Olsen
Release: 2024-12-18 19:58:11
Original
281 people have browsed it

Can CSS Detect Text Input in External Pages Without JavaScript?

Detecting Text Input Using CSS in Uncontrollable External Pages

In order to determine whether or not an input field contains text solely through CSS, consider using a placeholder attribute. While the placeholder is necessary and cannot be empty, it can be set to a single space.

Applying the CSS Rule:

input:not(:placeholder-shown) {
  border-color: green;
}

input:placeholder-shown {
  border-color: red;
}
Copy after login

Demonstration:

<input placeholder="Text is required" />
<input placeholder=" " value="This one is valid" />
<input placeholder=" " />
Copy after login

In this example, the input with a placeholder and no text will have a green border, indicating that it is empty. Conversely, the input with a placeholder and text, or with a space-only placeholder and value, will have a red border.

Additional Notes:

  • This method works because :placeholder-shown is a pseudo-class that matches elements that are showing a placeholder.
  • :placeholder-shown is supported in all major browsers.

The above is the detailed content of Can CSS Detect Text Input in External Pages Without JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template