Attr() - Function CSS
Summary of characteristics of the attr()
function
Syntax diagram of attr()
.
attr()
functionThe links in the diagram provide access to more details
Definition of terms used on the diagram:
a
is the name of an attribute in the HTML or XML.t
is the type of expected data.u
is a CSS unit.d
is a fallback value.
Description of the attr()
function.
The attr()
function returns the value of one of the element's attributes.
The requested attribute must be specified in the HTML: attr()
retrieves the values as they are written
in HTML (or XML) and not the values calculated by the browser or defined in CSS.
Note: the specified attribute is not necessarily a standard HTML attribute, it can also be a custom attribute.
Example. Suppose the following HTML code:
a href="/promos/index.php" title="Promotions" /
It is then possible to retrieve the value of the title
attribute with the content
property, and display it before (or after) the link:
a::before {content: attr(title); }
It is not possible to retrieve the attribute of a given element and apply it to another element. But we can apply to a pseudo-element the value of an attribute of the main element.
The attr() function can be used with:
content
: Add content to the document, mainly with the::before
and::after
selectors.
Normally the value returned by attr()
should be able to be used with any property,
but, at the moment (2024), this possibility is only supported by .
Syntax of the attr()
function.
- content: attr(width); a
The simplest syntax: you just specify the name of the attribute whose value you want to retrieve, without apostrophes or quotation marks.
- content: attr(width type(length)); ⚠ a t
Here we specify the name of the attribute (
a
) and the type (t
) of the data to be retrieved. If the value found cannot be converted to the requested type,attr()
returns a fallback value.
Similarly, if the value found is not compatible with the property that usesattr()
, the default value is taken into account.The possible types are as follows. Without any indication, the default type is
string
.angle
The value read must be convertible to an angular value: a number followed by one of the angular units (see CSS angle units). The fallback value is0
.color
The value of the attribute must be similar to a color. The fallback value iscurrentColor
.custom-ident
The value can be anything that matches an identifier.frequency
The read value must be convertible to a frequency: it must be a number followed by a frequency unit (CSS units of frequency). The fallback value is0
.image
The value of the attribute must be similar to an image. The fallback value is an empty image.integer
The value must be convertible to an integer: the presence of a decimal separator is an error. The fallback value is0
.length
The read value must be convertible to a dimension. The unit must be specified (see CSS dimension units). The fallback value is0
.number
The value must be convertible to a number. It must not have unity. The fallback value is0
.percentage
The value must be convertible to a decimal number followed bythe %
character. The fallback value is0
.resolution
The value must be convertible to a resolution. It should be followed by one of the CSS units of resolution. The default value is0
.string
No constraint on the value of the attribute. The fallback value is an empty string.time
The read value must be convertible to a duration: a number followed by one of the CSS units of duration. The fallback value is0
.*
To accept all types, use the star (*
). It's different fromstring
in that the value is still parsed.url (adresse) ✗
This option is no longer accepted. It had to be a string with the correct syntax of a url. The default value isabout:invalid
, which is an address recognized by all browsers as invalid.Several types can be indicated, separated by a vertical bar
|
. - content: attr(width px); ⚠ a u
In this syntax, the name of the attribute and a unit that will be added to the retrieved value are specified. All units are accepted (dimensions, duration, etc.). The unit implicitly defines the type of data. For example, the
deg
unit can only match the typeangle
, just aspx
can only correspond to one dimension. - content: attr(width type(length), 200px); ⚠ a t d
The parameter
a
is the name of the attribute whose value we want to retrieve.
The secondt
parameter is the type to which this value should be converted.
Finally, the thirdd
parameter (preceded by a comma) is the fallback value, to be returned if the The requested value is not present in the HTML, or if the value found is not compatible with the specified type, or if the value obtained is not within the range of acceptable values for the property.
Simulator.
The simulator uses two div tags, the HTML code of which is given below.
We can see that each of the tags contains the standard title
attribute and a custom attribute named data-price
.
On the other hand, the custom data-stock
attribute is only present on the first element.
<div title="Souris optique" data-price="11.50€" data-stock="5">Optical mouse</div>
<div title="clavier sans fil" data-price="23.50">Wireless keyboard</div>
Browsers compatibility.
Current browsers correctly handle the attr()
function when used with the content
and with the simplest syntax (single parameter).
But its use with other properties is not yet possible (2024), although this possibility is planned in the W3C specification.
The complete syntax of attr()
, with two or three parameters, is still very poorly recognized.
attr()
.attr()
function with any property.attr()
with a parameter indicating the type of value or a unit.attr()
function.attr()
function
properties
unit
Browsers on computers :
Mobile browsers :
Outdated or marginal browsers :

Internet Explorer

Baidu Browser

QQ Browser

Opéra Mobile

Firefox

Chrome

Safari

Edge

Opéra

Chrome pour Androïd

Safari sur IOS

Samsung Internet

UC Browser pour Androïd

Androïd Brower

Firefox pour Androïd

KaiOS Browser

Opéra mini
attr()
function historic.
-
CSS Values and Units Module Level 3
The specification of theattr()
function has been deferred to the upper level of the standard.July 13, 2001Working Draft.August 28, 2012Candidate Recommendation. -
CSS Values and Units Module Level 4
The specification of theattr()
function has been deferred to level 5.August 14, 2018Working Draft. -
CSS Values and Units Module Level 5
Introduction of theattr()
function in the standard.September 03, 2024Working Draft.
See also...
The W3C specification named CSS Values and Units Module includes many functions that can be used in CSS.
Functions:









Values:

e
(the basis of natural logarithms), which is approximately 2.7182818284590452354.


pi
(about 3.1415926535897932).