attr

  • Revision slug: CSS/attr
  • Revision title: attr
  • Revision id: 304303
  • Created:
  • Creator: teoli
  • Is current revision? No
  • Comment

Revision Content

{{ CSSRef() }}

Summary

The attr() function is used to retrieve the value of an HTML element's attribute and use it in the style sheet.

The attr() function can be used with any CSS property. {{ experimental_inline() }}

Syntax

Formal syntax: attr( attribute-name <type-or-unit>? [, <fallback> ]? )

Values

attribute-name
Is the name of an attribute on the HTML element referenced in the CSS. Support for attributes other than {{ cssxref("content") }} is {{ experimental_inline() }}.
<type-or-unit>
Is a keyword representing either the type of the attribute's value, or its unit as, in HTML, some attributes have implicit units. If omitted, it defaults to string. The list of valid values are:
Keyword Kind Comment Default value
string type The attribute value is parsed as a CSS {{ xref_cssstring() }} An empty string
color {{ experimental_inline() }} type The attribute value is parsed as a hash (3- or 6-value hash) or a keyword. It must be a valid CSS {{ xref_cssstring() }} value.
Leading and trailing spaces are stripped.
currentColor
url {{ experimental_inline() }} type The attribute value is parsed as a string that is used inside a CSS url()function.
Relative URL are resolved relatively to the original document, not relatively to the style sheet.
Leading and trailing spaces are stripped.
The url about:invalid that points to a non-existent document with a generic error condition.
integer {{ experimental_inline() }} type The attribute is parsed as a CSS {{ xref_cssinteger() }}. If it is not valid, that is not an integer or out of the range accepted by the CSS property, the default value is used.
Leading and trailing spaces are stripped.
0, or, if 0 is not a valid value for the property, the property's minimum value.
number {{ experimental_inline() }} type The attribute is parsed as a CSS {{ xref_cssnumber() }}. 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.
0, or, if 0 is not a valid value for the property, the property's minimum value.
length {{ experimental_inline() }} type The attribute is parsed as a CSS {{ xref_csslength() }} 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() }} unit The attribute is parsed as a CSS {{ xref_cssnumber() }}, that is without the unit (e.g. 12.5), and interpreted as a {{ xref_csslength() }} 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() }} type The attribute is parsed as a CSS {{ xref_cssangle() }} 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() }} unit The attribute is parsed as a CSS {{ xref_cssnumber() }}, that is without the unit (e.g. 12.5), and interpreted as an {{ xref_cssangle() }} 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() }} type The attribute is parsed as a CSS {{ xref_csstime() }} 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() }} unit The attribute is parsed as a CSS {{ xref_cssnumber() }}, that is without the unit (e.g. 12.5), and interpreted as an{{ xref_csstime() }} 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() }} type The attribute is parsed as a CSS {{ xref_cssfrequency() }} 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() }} unit The attribute is parsed as a CSS {{ xref_cssnumber() }}, that is without the unit (e.g. 12.5), and interpreted as a {{ xref_cssfrequency() }} 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() }} unit The attribute is parsed as a CSS {{ xref_cssnumber() }}, that is without the unit (e.g. 12.5), and interpreted as a {{ xref_csspercentage() }}. 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>
The value to be used if the associated attribute is missing or contains an invalid value. If not set, CSS will use the default value defined for each <type-or-unit>.

Examples

p:before {
    content:attr(data-foo) " ";
}
<p data-foo="hello">world</p>

Result

hello world

Specifications

Specification Status Comment
{{ SpecName('CSS3 Values', '#attr', 'attr()') }} {{ Spec2('CSS3 Values') }} Added to optional parameter; can be used on all properties; may return other values than {{xref_cssstring()}}
{{ SpecName('CSS2.1', 'generate.html#x18', 'attr()') }} {{ Spec2('CSS2.1') }} Limited to the {{ cssxref("content") }} property; always return a {{xref_cssstring()}}}

Browser Compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 2.0 {{ CompatGeckoDesktop(1.0) }} 8 9.0 3.1
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support 2.1 {{ CompatGeckoMobile(1.0) }} 8 10.0 3.1

{{ languages( { "fr": "fr/CSS/attr" } ) }}

Revision Source

<p>{{ CSSRef() }}</p>
<h2 id="Summary">Summary</h2>
<p>The <code>attr()</code> function is used to retrieve the value of an HTML element's attribute and use it in the style sheet.</p>
<p>The <code>attr()</code> function can be used with any CSS property. {{ experimental_inline() }}</p>
<h2 id="Syntax">Syntax</h2>
<pre class="syntaxBox">
Formal syntax: attr( attribute-name &lt;type-or-unit&gt;? [, &lt;fallback&gt; ]? )
</pre>
<h3 id="Values">Values</h3>
<dl>
  <dt>
    <code>attribute-name</code></dt>
  <dd>
    Is the name of an attribute on the HTML element referenced in the CSS. Support for attributes other than {{ cssxref("content") }} is {{ experimental_inline() }}.</dd>
  <dt>
    <code>&lt;type-</code>or-unit&gt;</dt>
  <dd>
    Is a keyword representing either the type of the attribute's value, or its unit as, in HTML, some attributes have implicit units. If omitted, it defaults to <code>string</code>. The list of valid values are:
    <table class="standard-table">
      <thead>
        <tr>
          <th scope="col">Keyword</th>
          <th scope="col">Kind</th>
          <th scope="col">Comment</th>
          <th scope="col">Default value</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td><code>string</code></td>
          <td><em>type</em></td>
          <td>The attribute value is parsed as a CSS {{ xref_cssstring() }}</td>
          <td>An empty string</td>
        </tr>
        <tr>
          <td><code>color</code> {{ experimental_inline() }}</td>
          <td><em>type</em></td>
          <td>The attribute value is parsed as a hash (3- or 6-value hash) or a keyword. It must be a valid CSS {{ xref_cssstring() }} value.<br />
            Leading and trailing spaces are stripped.</td>
          <td><code>currentColor</code></td>
        </tr>
        <tr>
          <td><code>url</code> {{ experimental_inline() }}</td>
          <td><em>type</em></td>
          <td>The attribute value is parsed as a string that is used inside a CSS <code>url()</code>function.<br />
            Relative URL are resolved relatively to the original document, not relatively to the style sheet.<br />
            Leading and trailing spaces are stripped.</td>
          <td>The url <code>about:invalid</code> that points to a non-existent document with a generic error condition.</td>
        </tr>
        <tr>
          <td><code>integer</code> {{ experimental_inline() }}</td>
          <td><em>type</em></td>
          <td>The attribute is parsed as a CSS {{ xref_cssinteger() }}. If it is not valid, that is not an integer or out of the range accepted by the CSS property, the default value is used.<br />
            Leading and trailing spaces are stripped.</td>
          <td><code>0</code>, or, if <code>0</code> is not a valid value for the property, the property's minimum value.</td>
        </tr>
        <tr>
          <td><code>number</code> {{ experimental_inline() }}</td>
          <td><em>type</em></td>
          <td>The attribute is parsed as a CSS {{ xref_cssnumber() }}. 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.<br />
            Leading and trailing spaces are stripped.</td>
          <td><code>0</code>, or, if <code>0</code> is not a valid value for the property, the property's minimum value.</td>
        </tr>
        <tr>
          <td><code>length</code> {{ experimental_inline() }}</td>
          <td><em>type</em></td>
          <td>The attribute is parsed as a CSS {{ xref_csslength() }} dimension, that is including the unit (e.g. <code>12.5em</code>). 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.<br />
            If the given unit is a relative length, <code>attr()</code>computes it to an absolute length.<br />
            Leading and trailing spaces are stripped.</td>
          <td><code>0</code>, or, if <code>0</code> is not a valid value for the property, the property's minimum value.</td>
        </tr>
        <tr>
          <td><code>em</code>, <code>ex</code>, <code>px</code>, <code>rem</code>, <code>vw</code>, <code>vh</code>, <code>vmin</code>, <code>vmax</code>, <code>mm</code>, <code>cm</code>, <code>in</code>, <code>pt</code>, or <code>pc</code> {{ experimental_inline() }}</td>
          <td><em>unit</em></td>
          <td>The attribute is parsed as a CSS {{ xref_cssnumber() }}, that is without the unit (e.g. <code>12.5</code>), and interpreted as a {{ xref_csslength() }} 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.<br />
            If the given unit is a relative length, <code>attr()</code>computes it to an absolute length.<br />
            Leading and trailing spaces are stripped.</td>
          <td><code>0</code>, or, if <code>0</code> is not a valid value for the property, the property's minimum value.</td>
        </tr>
        <tr>
          <td><code>angle</code> {{ experimental_inline() }}</td>
          <td><em>type</em></td>
          <td>The attribute is parsed as a CSS {{ xref_cssangle() }} dimension, that is including the unit (e.g. <code>30.5deg</code>). 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.<br />
            Leading and trailing spaces are stripped.</td>
          <td><code>0deg</code>, or, if <code>0deg</code> is not a valid value for the property, the property's minimum value.</td>
        </tr>
        <tr>
          <td><code>deg</code>, <code>grad</code>, <code>rad</code> {{ experimental_inline() }}</td>
          <td><em>unit</em></td>
          <td>The attribute is parsed as a CSS {{ xref_cssnumber() }}, that is without the unit (e.g. <code>12.5</code>), and interpreted as an {{ xref_cssangle() }} 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.<br />
            Leading and trailing spaces are stripped.</td>
          <td><code>0deg</code>, or, if <code>0deg</code> is not a valid value for the property, the property's minimum value.</td>
        </tr>
        <tr>
          <td><code>time</code> {{ experimental_inline() }}</td>
          <td><em>type</em></td>
          <td>The attribute is parsed as a CSS {{ xref_csstime() }} dimension, that is including the unit (e.g. <code>30.5ms</code>). 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.<br />
            Leading and trailing spaces are stripped.</td>
          <td><code>0s</code>, or, if <code>0s</code> is not a valid value for the property, the property's minimum value.</td>
        </tr>
        <tr>
          <td><code>s</code>, <code>ms</code> {{ experimental_inline() }}</td>
          <td><em>unit</em></td>
          <td>The attribute is parsed as a CSS {{ xref_cssnumber() }}, that is without the unit (e.g. <code>12.5</code>), and interpreted as an{{ xref_csstime() }} 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.<br />
            Leading and trailing spaces are stripped.</td>
          <td><code>0s</code>, or, if <code>0s</code> is not a valid value for the property, the property's minimum value.</td>
        </tr>
        <tr>
          <td><code>frequency</code> {{ experimental_inline() }}</td>
          <td><em>type</em></td>
          <td>The attribute is parsed as a CSS {{ xref_cssfrequency() }} dimension, that is including the unit (e.g. <code>30.5kHz</code>). 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.</td>
          <td><code>0Hz</code>, or, if <code>0Hz</code> is not a valid value for the property, the property's minimum value.</td>
        </tr>
        <tr>
          <td><code>Hz</code>, <code>kHz</code> {{ experimental_inline() }}</td>
          <td><em>unit</em></td>
          <td>The attribute is parsed as a CSS {{ xref_cssnumber() }}, that is without the unit (e.g. <code>12.5</code>), and interpreted as a {{ xref_cssfrequency() }} 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.<br />
            Leading and trailing spaces are stripped.</td>
          <td><code>0Hz</code>, or, if <code>0Hz</code> is not a valid value for the property, the property's minimum value.</td>
        </tr>
        <tr>
          <td><code>%</code> {{ experimental_inline() }}</td>
          <td><em>unit</em></td>
          <td>The attribute is parsed as a CSS {{ xref_cssnumber() }}, that is without the unit (e.g. <code>12.5</code>), and interpreted as a {{ xref_csspercentage() }}. 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.<br />
            If the given value is used as a length, <code>attr()</code>computes it to an absolute length.<br />
            Leading and trailing spaces are stripped.</td>
          <td><code>0%</code>, or, if <code>0%</code> is not a valid value for the property, the property's minimum value.</td>
        </tr>
      </tbody>
    </table>
  </dd>
  <dt>
    <code>&lt;fallback&gt;</code></dt>
  <dd>
    The value to be used if the associated attribute is missing or contains an invalid value. If not set, CSS will use the default value defined for each <code>&lt;type-or-unit&gt;</code>.</dd>
</dl>
<h2 id="Examples">Examples</h2>
<pre class="brush: css">
p:before {
&nbsp;&nbsp;&nbsp;&nbsp;content:attr(data-foo) " ";
}
</pre>
<pre class="brush: html">
&lt;p data-foo="hello"&gt;world&lt;/p&gt;
</pre>
<h3 id="Result">Result</h3>
<p>hello world</p>
<h2 id="Specifications" name="Specifications">Specifications</h2>
<table class="standard-table">
  <thead>
    <tr>
      <th scope="col">Specification</th>
      <th scope="col">Status</th>
      <th scope="col">Comment</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>{{ SpecName('CSS3 Values', '#attr', 'attr()') }}</td>
      <td>{{ Spec2('CSS3 Values') }}</td>
      <td>Added to optional parameter; can be used on all properties; may return other values than {{xref_cssstring()}}</td>
    </tr>
    <tr style="vertical-align: top;">
      <td style="vertical-align: top;">{{ SpecName('CSS2.1', 'generate.html#x18', 'attr()') }}</td>
      <td style="vertical-align: top;">{{ Spec2('CSS2.1') }}</td>
      <td style="vertical-align: top;">Limited to the {{ cssxref("content") }} property; always return a {{xref_cssstring()}}}</td>
    </tr>
  </tbody>
</table>
<h2 id="Browser_Compatibility">Browser Compatibility</h2>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Chrome</th>
        <th>Firefox (Gecko)</th>
        <th>Internet Explorer</th>
        <th>Opera</th>
        <th>Safari</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>2.0</td>
        <td>{{ CompatGeckoDesktop(1.0) }}</td>
        <td>8</td>
        <td>9.0</td>
        <td>3.1</td>
      </tr>
    </tbody>
  </table>
</div>
<div id="compat-mobile">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Android</th>
        <th>Firefox Mobile (Gecko)</th>
        <th>IE Mobile</th>
        <th>Opera Mobile</th>
        <th>Safari Mobile</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>2.1</td>
        <td>{{ CompatGeckoMobile(1.0) }}</td>
        <td>8</td>
        <td>10.0</td>
        <td>3.1</td>
      </tr>
    </tbody>
  </table>
</div>
<p>{{ languages( { "fr": "fr/CSS/attr" } ) }}</p>
Revert to this revision