English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

CSS Reference Manual

CSS @rules (RULES)

Comprehensive List of CSS Properties

CSS attr() function

CSS expression attr() is used to get the value of a certain HTML attribute of the selected element and use it in its style. It can also be used for pseudo-elements, where the attribute value is taken from the element that the pseudo-element is attached to. The attr() expression can be used with any CSS property.

CSS function

Online Example

The following example inserts content after each link:

!DOCTYPE html
<html>
<head>
<meta charset="utf-8"> 
<title>Basic Tutorial(oldtoolbag.com)</title> 
<style>
a:after {content: " (" attr(href) ")";}
</style>
</head>
<body>
<p><a href="//www.oldtoolbag.com">Basic Tutorial</a></p>
<p><a href="//www.oldtoolbag.com/html/">HTML Tutorial</a></p>
<p><strong>Note:</strong>IE8 The attr() function needs to declare !DOCTYPE to be supported.</p>
</body>
</html>
Test and see ‹/›

Definition and usage

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

Supported version: CSS2

Browser compatibility

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

function




attr()2.08.01.03.19.0

Note: IE8 The attr() function needs to declare !DOCTYPE to be supported.

CSS syntax

Syntax: attr(attribute-name<type-or-unit>?[,<fallback>]?())
ValueDescription
attribute-nameMust. The attribute name of the HTML element.

CSS function