Favorites

attr() CSS Function

attr()
CSS FunctionThis function is supported by all modern browsers.
Updated 21st November 2026

The attr() function reads the value of an HTML attribute and uses it in CSS, as text in content or, in newer browsers, as a typed value in any property.

Looks like you're using an ad blocker, and that's fine! Ads help keep CSS Portal's generators and references free, so if the site's useful to you, visit our Support page to see other ways to help.

Description

The attr() CSS function allows you to retrieve the value of an attribute from an HTML element and use it in your styles. It is particularly useful for dynamically displaying content based on the attributes of elements, such as tooltips, labels, or counters, without requiring JavaScript. While attr() is most commonly used with the content property in pseudo-elements like ::before and ::after, its support in other CSS properties is currently limited in many browsers, though evolving in modern CSS specifications.

For example, if you have an a element with a title attribute, you can use attr() to display the title in a tooltip-like fashion using CSS only:

a::after {
  content: " (" attr(title) ")";
  color: gray;
  font-size: 0.9em;
}

This will automatically append the value of the title attribute to the link text.

Another practical usage is with img elements to display alternate text:

img::after {
  content: attr(alt);
  display: block;
  text-align: center;
  color: #555;
}

Here, attr(alt) pulls the alt attribute from the img and renders it below the image.

By default, attr() returns the attribute as a string. Newer versions of CSS add typed attr(), which can read an attribute as another type with type() or a unit, for example attr(data-size type(<length>)) or attr(data-width px), and can then be used with any property, such as width or color. A fallback value after a comma is used when the attribute is missing or invalid. Typed attr() is supported in Chrome and Edge (from version 133); support in Firefox and Safari is still arriving, so keep a fallback for other browsers.

Interactive Demo

  • Apples
  • Bananas
  • Cherries
Demo Code
HTML Code
<ul class="demo-element">
	<li data-label="New" title="Added today">Apples</li>
	<li data-label="Sale" title="20% off">Bananas</li>
	<li data-label="Low stock" title="Only 3 left">Cherries</li>
</ul>
CSS Code
.demo-element {
	width: 260px;
	margin: 0;
	padding: 10px 14px 10px 30px;
	background: #e7ecff;
	border: 2px solid #4361ee;
	font-size: 15px;
	line-height: 2;
}
.demo-element li::after {
	margin-left: 8px;
	padding: 1px 6px;
	border-radius: 4px;
	background: #f72585;
	color: #fff;
	font-size: 12px;
	content: attr(data-label);
}

Syntax

attr() = attr( <attr-name> <attr-type>? , <declaration-value>? )

<attr-type> = type( <syntax> ) | raw-string | number | <attr-unit>

Values

  • <attr-name>The name of the attribute to read from the element, for example data-label or title.
  • <attr-type>Optional. How to interpret the attribute value. Without it, the value is a string. type(<syntax>) parses it as a CSS type such as type(<color>), type(<length>) or type(<number>); raw-string keeps it as a string; and a unit such as px, em, deg or % reads a number and adds that unit, for example attr(data-size px).
  • <declaration-value>Optional. A fallback value, after a comma, used when the attribute is missing or cannot be parsed as the requested type.

Example

<p>
Check out this
<a href="https://www.wikipedia.org" data-info="The Free Encyclopedia">
Wikipedia link
</a>.
</p>
/* Styling the link anchor */
a {
color: #007bff;
text-decoration: none;
position: relative;
}

/* Using attr() to display the data-info value */
a:hover::after {
content: " (" attr(data-info) ")";
color: #555;
font-size: 0.8rem;
font-style: italic;
}

/* Another example using the href attribute */
a:active::after {
content: " Opening: " attr(href);
display: block;
color: red;
}
Open in editor

Browser Support

The following information will show you the current browser support for the CSS attr() function. Hover over a browser icon to see the version that first introduced support for this CSS function.

This function is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported

Looks like you're using an ad blocker, and that's fine! Ads help keep CSS Portal's generators and references free, so if the site's useful to you, visit our Support page to see other ways to help.