Home >Web Front-end >CSS Tutorial >Can You Set CSS Values Using HTML5 Data Attributes?

Can You Set CSS Values Using HTML5 Data Attributes?

Linda Hamilton
Linda HamiltonOriginal
2024-11-07 03:33:03765browse

Can You Set CSS Values Using HTML5 Data Attributes?

Setting CSS Values using HTML5 Data Attributes

Recently, a question arose regarding the feasibility of setting CSS values using HTML5's data- attributes. As mentioned by the question, attempts to set CSS properties like "width" using the "attr(data-width)" notation do not currently work.

However, the W3C CSS Values and Units Module Level 3 (CSS3 Values) does include a provision for this feature, as outlined in https://www.w3.org/TR/css3-values/#attr-notation.

In theory, code such as the following should work:

HTML

<div data-width="600px"></div>

CSS

div { width: attr(data-width) }

Unfortunately, despite being part of a standard, this feature is still in draft form and not fully implemented in major browsers. However, it does function when setting content on pseudo-elements:

CSS

input::before { content: attr(data-placeholder) }

The above is the detailed content of Can You Set CSS Values Using HTML5 Data Attributes?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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