CSS attr() Function

Definition and Usage

CSS's attr() The function returns the attribute value of the selected element.

Example

The following example inserts the value of the href attribute after each <a> element and encloses it in parentheses:

a:after {
  content: " (" attr(href) ")";
}

Try It Yourself

CSS Syntax

attr(attribute-name type fallback)
Value Description
attribute-name Required. The attribute name of the HTML element.
type

Optional. The type or unit of the attribute value. It can be one of the following:

  • string
  • color
  • url
  • integer
  • number
  • length
  • angle
  • em, ex, px, rem, vw, vh, vmin, vmax, mm, cm, in, pt, or pc
  • deg, grad, rad
  • time
  • s, ms
  • Hz, kHz
  • %
  • frequency
fallback Optional. A fallback value if the property is missing or contains an invalid value.

Technical Details

Version: CSS2

Browser Support

The numbers in the table indicate the browser version that first fully supports this function.

Chrome Edge Firefox Safari Opera
attr()
2 8 1 3.1 9
type Value
Not Supported Not Supported Not Supported Not Supported Not Supported
fallback Value
Not Supported Not Supported 119 Not Supported Not Supported