attr

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

Revision Content

{{ CSSRef() }}

Summary

The attr() CSS expression is used to retrieve the value of an attribute of the selected element and use it in the style sheet. It can be used on pseudo-elements too and, in this case, the value of the attribute on the pseudo-element's originated element is returned.

The attr() expression 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 the use of <type-or-unit> as a value for the given attribute is not valid, the attr() expression will be invalid too. If omitted, it defaults to string. The list of valid values are:
Keyword Associated type Comment Default value
string {{ xref_cssstring() }} The attribute value is parsed as a CSS {{ xref_cssstring() }} An empty string
color {{ experimental_inline() }} {{ xref_csscolorvalue() }} 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() }} {{ xref_cssurl() }} 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() }} {{ xref_cssinteger() }} 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() }} {{ xref_cssnumber() }} 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() }} {{ xref_csslength() }} 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() }} {{ xref_csslength() }} 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() }} {{ xref_cssangle() }} 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() }} {{ xref_cssangle() }} 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() }} {{ xref_csstime() }} 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() }} {{ xref_csstime() }} 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() }} {{ xref_cssfrequency() }} 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() }} {{ xref_cssfrequency() }} 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() }} {{ xref_csspercentage() }} 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. The fallback value must be valid where attr() is used, even if it is not used, and mustnot contains 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>.

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 two optional parameters; can be used on all properties; may return other values than {{xref_cssstring()}}. 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 {{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
Usage in other properties than {{cssxref("content")}} and with non-string values  {{ experimental_inline() }} {{CompatUnknown()}} {{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()}}

 

Revision Source

<p>{{ CSSRef() }}</p>
<h2 id="Summary">Summary</h2>
<p>The <code>attr()</code> CSS expression is used to retrieve the value of an attribute of the selected element and use it in the style sheet. It can be used on pseudo-elements too and, in this case, the value of the attribute on the pseudo-element's originated element is returned.</p>
<p>The <code>attr()</code> expression 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 the use of <code>&lt;type-or-unit&gt;</code> as a value for the given attribute is not valid, the <code>attr()</code> expression will be invalid too. 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">Associated type</th>
          <th scope="col">Comment</th>
          <th scope="col">Default value</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td><code>string</code></td>
          <td>{{ xref_cssstring() }}</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>{{ xref_csscolorvalue() }}</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>{{ xref_cssurl() }}</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>{{ xref_cssinteger() }}</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>{{ xref_cssnumber() }}</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>{{ xref_csslength() }}</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>{{ xref_csslength() }}</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>{{ xref_cssangle() }}</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>{{ xref_cssangle() }}</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>{{ xref_csstime() }}</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>{{ xref_csstime() }}</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>{{ xref_cssfrequency() }}</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>{{ xref_cssfrequency() }}</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>{{ xref_csspercentage() }}</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. The fallback value must be valid where <code>attr()</code> is used, even if it is not used, and mustnot contains another <code>attr()</code> expression. If <code>attr()</code> is not the sole component value of a property, its <code>&lt;fallback&gt;</code> value must be of the type defined by <code>&lt;type-or-unit&gt;</code>. 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 two optional parameters; can be used on all properties; may return other values than {{xref_cssstring()}}. These changes are {{ experimental_inline() }} and may be dropped during the CR phase if browser support is too small.</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>
      <tr>
        <td>Usage in other properties than {{cssxref("content")}} and with non-string values&nbsp; {{ experimental_inline() }}</td>
        <td>{{CompatUnknown()}}</td>
        <td>{{CompatNo()}} (see {{bug("435426")}})</td>
        <td>{{CompatNo()}}</td>
        <td>{{CompatUnknown()}}</td>
        <td>{{CompatUnknown()}}</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>
      <tr>
        <td>Usage in other properties than {{cssxref("content")}}and with non-string values&nbsp; {{ experimental_inline() }}</td>
        <td>{{CompatUnknown()}}</td>
        <td>{{CompatNo()}} (see {{bug("435426")}})</td>
        <td>{{CompatNo()}}</td>
        <td>{{CompatUnknown()}}</td>
        <td>{{CompatUnknown()}}</td>
      </tr>
    </tbody>
  </table>
</div>
<p>&nbsp;</p>
Revert to this revision