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
Release: 2024-11-07 03:33:03
Original
758 people have browsed it

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>
Copy after login

CSS

div { width: attr(data-width) }
Copy after login

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) }
Copy after login

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!

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