Revision 304287 of attr

  • Revision slug: CSS/attr
  • Revision title: attr
  • Revision id: 304287
  • 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> ], [ <value> ] )

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. 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. 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.
The url about:invalid that points to a non-existent document with a generic error condition.
integer{{ experimental_inline() }} type The attribue is parsed as a CSS {{ xref_cssinteger() }}. If it is not valid, that is not integer or out of the range accepted by the CSS property, the default value is used. 0
       
       
       
       
       

 

  • The third argument, <value> must be a CSS value which is valid for the property where attr() is used.
  • In CSS 3 the returned value can be any valid CSS value.
  •  

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>
Formal syntax: attr( attribute-name [ &lt;type-or-unit&gt; ], [ &lt;value&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. 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.</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.</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 attribue is parsed as a CSS {{ xref_cssinteger() }}. If it is not valid, that is not integer or out of the range accepted by the CSS property, the default value is used.</td>
          <td><code>0</code></td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
      </tbody>
    </table>
    <p>&nbsp;</p>
  </dd>
</dl>
<ul>
  <li>The third argument, <code>&lt;value&gt;</code> must be a CSS value which is valid for the property where attr() is used.</li>
  <li>In CSS 3 the returned value can be any valid CSS value.</li>
  <li>&nbsp;</li>
</ul>
<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