Home > Web Front-end > CSS Tutorial > Can the CSS `content` Property Inject HTML?

Can the CSS `content` Property Inject HTML?

Barbara Streisand
Release: 2024-12-22 18:01:10
Original
989 people have browsed it

Can the CSS `content` Property Inject HTML?

CSS content Property: Can it Inject HTML?

The CSS content property, used with :before and :after pseudo-elements, enables the insertion of content before or after an element. However, many developers wonder if it's possible to insert HTML code into the content instead of simple text.

HTML Injection Question

A common question arises:

Can you make the CSS content property insert HTML code instead of text, like this:

.header:before {
  content: '<a href="#top">Back</a>';
}
Copy after login

Answer: No HTML Injection

Unfortunately, as per the CSS specification, this is not feasible. The specification states:

"Generated content does not alter the document tree. In particular, it is not fed back to the document language processor (e.g., for reparsing)."

This means that string values used with the content property are treated literally. They are never interpreted as markup, regardless of the document language in use.

Example

Consider the following:

The above is the detailed content of Can the CSS `content` Property Inject HTML?. 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