--- title: attr slug: Web/CSS/attr() translation_of: Web/CSS/attr() ---
{{ CSSRef() }}
La función attr()
de CSS se usa para recuperar el valor de un atributo del elemento seleccionado y usarlo en la hoja de estilos. Se puede usar también en un pseudo-elemento y, en este caso, el valor del atributo en el elemento originado del pseudo-elemento es devuelto.
La función attr()
de CSS se puede usar con cualquier propiedad de CSS. {{ experimental_inline() }}
Sintaxis formal: attr( attribute-name <type-or-unit>? [, <fallback> ]? )
attribute-name
<type-
or-unit><type-or-unit>
como valor del atributo especificado no es válido, la expresión attr()
también será no válida. Si se omite, el valor por defecto es string. La lista de valores válidos son:
Keyword | Associated type | Comment | Default value |
---|---|---|---|
string |
{{cssxref("<string>")}} | El valor del atributo es tratado como una {{cssxref("<string>")}} de CSS. No se reparsea, y en este caso particular los caracteres son usados tal y como son en vez de convertirlos en diferentes caracteres de CSS. | Cadena vacía |
color {{ experimental_inline() }} |
{{cssxref("<color>")}} | El valor del atributo es tratado como hash (3- or 6-value hash) o a una palabra clave. Debe ser un valor válido de una {{cssxref("<string>")}} de CSS. Los espacios iniciales y finales son eliminados. |
currentColor |
url {{ experimental_inline() }} |
{{ cssxref("<uri>") }} | El valor del atributo es tratado como una cadena que se usa en una función url() de CSS.URLs relativas se resuelven con respecto al documento original, no respecto a la hoja de estilos. Los espacios iniciales y finales son eliminados. |
La url about:invalid que apunta a un documento inexistente con una condición de error genérico. |
integer {{ experimental_inline() }} |
{{cssxref("<integer>")}} | El valor del atributo es tratado como un {{cssxref("<integer>")}} CSS. Si no es válido, si no es un entero o está fuera del rango aceptado por la propiedad CSS, se usa el valor por defecto. Los espacios iniciales y finales son eliminados. |
0 , or, if 0 is not a valid value for the property, the property's minimum value. |
number {{ experimental_inline() }} |
{{cssxref("<number>")}} | El valor del atributo es tratado como {{cssxref("<number>")}}. Si no es válido, si no es un número o está fuera del rango aceptado por la propiedad CSS, se usa el valor por defecto. Los espacios iniciales y finales son eliminados. |
0 , or, if 0 is not a valid value for the property, the property's minimum value. |
length {{ experimental_inline() }} |
{{cssxref("<length>")}} | The attribute value is parsed as a CSS {{cssxref("<length>")}} dimension, that is including the unit (e.g. 12.5em ). If it is not valid, that is not a length or out of the range accepted by the CSS property, the default value is used.If the given unit is a relative length, attr() computes it to an absolute length.Leading and trailing spaces are stripped. |
0 , or, if 0 is not a valid value for the property, the property's minimum value. |
em , ex , px , rem , vw , vh , vmin , vmax , mm , cm , in , pt , or pc {{ experimental_inline() }} |
{{cssxref("<length>")}} | The attribute value is parsed as a CSS {{cssxref("<number>")}}, that is without the unit (e.g. 12.5 ), and interpreted as a {{cssxref("<length>")}} with the specified unit. If it is not valid, that is not a number or out of the range accepted by the CSS property, the default value is used.If the given unit is a relative length, attr() computes it to an absolute length.Leading and trailing spaces are stripped. |
0 , or, if 0 is not a valid value for the property, the property's minimum value. |
angle {{ experimental_inline() }} |
{{ cssxref("<angle>") }} | The attribute value is parsed as a CSS {{ cssxref("<angle>") }} dimension, that is including the unit (e.g. 30.5deg ). If it is not valid, that is not an angle or out of the range accepted by the CSS property, the default value is used.Leading and trailing spaces are stripped. |
0deg , or, if 0deg is not a valid value for the property, the property's minimum value. |
deg , grad , rad {{ experimental_inline() }} |
{{ cssxref("<angle>") }} | The attribute value is parsed as a CSS {{cssxref("<number>")}}, that is without the unit (e.g. 12.5 ), and interpreted as an {{ cssxref("<angle>") }} with the specified unit. If it is not valid, that is not a number or out of the range accepted by the CSS property, the default value is used.Leading and trailing spaces are stripped. |
0deg , or, if 0deg is not a valid value for the property, the property's minimum value. |
time {{ experimental_inline() }} |
{{cssxref("<time>")}} | The attribute value is parsed as a CSS {{cssxref("<time>")}} dimension, that is including the unit (e.g. 30.5ms ). If it is not valid, that is not a time or out of the range accepted by the CSS property, the default value is used.Leading and trailing spaces are stripped. |
0s , or, if 0s is not a valid value for the property, the property's minimum value. |
s , ms {{ experimental_inline() }} |
{{cssxref("<time>")}} | The attribute value is parsed as a CSS {{cssxref("<number>")}}, that is without the unit (e.g. 12.5 ), and interpreted as an{{cssxref("<time>")}} with the specified unit. If it is not valid, that is not a number or out of the range accepted by the CSS property, the default value is used.Leading and trailing spaces are stripped. |
0s , or, if 0s is not a valid value for the property, the property's minimum value. |
frequency {{ experimental_inline() }} |
{{cssxref("<frequency>")}} | The attribute value is parsed as a CSS {{cssxref("<frequency>")}} dimension, that is including the unit (e.g. 30.5kHz ). If it is not valid, that is not a frequency or out of the range accepted by the CSS property, the default value is used. |
0Hz , or, if 0Hz is not a valid value for the property, the property's minimum value. |
Hz , kHz {{ experimental_inline() }} |
{{cssxref("<frequency>")}} | The attribute value is parsed as a CSS {{cssxref("<number>")}}, that is without the unit (e.g. 12.5 ), and interpreted as a {{cssxref("<frequency>")}} with the specified unit. If it is not valid, that is not a number or out of the range accepted by the CSS property, the default value is used.Leading and trailing spaces are stripped. |
0Hz , or, if 0Hz is not a valid value for the property, the property's minimum value. |
% {{ experimental_inline() }} |
{{cssxref("<percentage>")}} | The attribute value is parsed as a CSS {{cssxref("<number>")}}, that is without the unit (e.g. 12.5 ), and interpreted as a {{cssxref("<percentage>")}}. If it is not valid, that is not a number or out of the range accepted by the CSS property, the default value is used.If the given value is used as a length, attr() computes it to an absolute length.Leading and trailing spaces are stripped. |
0% , or, if 0% is not a valid value for the property, the property's minimum value. |
<fallback>
attr()
is used, even if it is not used, and must not contain another attr()
expression. If attr()
is not the sole component value of a property, its <fallback>
value must be of the type defined by <type-or-unit>
. If not set, CSS will use the default value defined for each <type-or-unit>
.p::before { content:attr(data-foo) " "; }
<p data-foo="hello">world</p>
{{ EmbedLiveSample("Examples", '100%', '80') }}
Specification | Status | Comment |
---|---|---|
{{ SpecName('CSS3 Values', '#attr', 'attr()') }} | {{ Spec2('CSS3 Values') }} | Added two optional parameters; can be used on all properties; may return other values than {{cssxref("<string>")}}. These changes are {{ experimental_inline() }} and may be dropped during the CR phase if browser support is too small. |
{{ SpecName('CSS2.1', 'generate.html#x18', 'attr()') }} | {{ Spec2('CSS2.1') }} | Limited to the {{ cssxref("content") }} property; always return a {{cssxref("<string>")}} |
{{ CompatibilityTable() }}
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | 2.0 | {{ CompatGeckoDesktop(1.0) }} | 8 | 9.0 | 3.1 |
Usage in other properties than {{cssxref("content")}} and with non-string values {{ experimental_inline() }} | {{CompatNo()}} | {{CompatNo()}} (see {{bug("435426")}}) | {{CompatNo()}} | {{CompatUnknown()}} | {{CompatUnknown()}} |
Feature | Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|
Basic support | 2.1 | {{ CompatGeckoMobile(1.0) }} | 8 | 10.0 | 3.1 |
Usage in other properties than {{cssxref("content")}}and with non-string values {{ experimental_inline() }} | {{CompatUnknown()}} | {{CompatNo()}} (see {{bug("435426")}}) | {{CompatNo()}} | {{CompatUnknown()}} | {{CompatUnknown()}} |