Home > Web Front-end > CSS Tutorial > Why Won\'t ::after Content Display for Input Elements in HTML5?

Why Won\'t ::after Content Display for Input Elements in HTML5?

Susan Sarandon
Release: 2024-10-24 17:45:02
Original
1037 people have browsed it

Why Won't ::after Content Display for Input Elements in HTML5?

CSS Pseudo-Elements for Input Fields

In HTML5, adding visual cues to form inputs using CSS pseudo-elements can enhance user experience. One might attempt to use the ::before and ::after pseudo-elements for styling, as seen in the following example:

<code class="css">input::after         { display: inline; }
input:valid::after   { content: ' ✓ '; color: #ddf0dd; }
input:invalid::after { content: ' ✗ '; color: #f0dddd; }</code>
Copy after login

However, one may encounter difficulties in displaying ::after content for input elements. Despite using both single and double colons, removing pseudo-classes, and experimenting with different browsers, the content remains invisible.

While ::after pseudo-elements function seamlessly on elements such as div, span, p, and q, this is not the case for input elements. This behavior stems from a fundamental attribute of these elements: they lack document tree content.

The Role of Element Content

According to the CSS spec (http://www.w3.org/TR/CSS21/generate.html), ::after pseudo-elements explicitly require elements with a document tree content in order to render their content. Input elements, similar to img and br, contain no such content. Therefore, by design, browsers will not display ::after content for input fields.

The above is the detailed content of Why Won\'t ::after Content Display for Input Elements in HTML5?. 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