type() CSS Function
The type() function describes a data type inside other functions: in attr() it sets how an attribute is read, and in @function it types parameters.
Description
The type() CSS function describes a data type inside other functions. It has two different roles, depending on where it is used.
In image-set(), type() takes a string with a MIME type, such as type("image/avif"), and labels an image option with its file format. Browsers skip any option whose format they cannot decode, so you can list modern formats such as AVIF and WebP first and older formats as fallbacks, just like the type attribute on the HTML <source> element.
In typed attr(), type() takes a CSS syntax description, such as type(<color>), type(<length>) or type(<number> | <percentage>), and tells the browser how to parse an HTML attribute's value. This lets an attribute such as data-color="tomato" be used as a real color in any property, not just as text in content. The syntax uses the same notation as the syntax descriptor of @property.
/* type() in image-set(): pick the first format the browser supports */
.hero {
background-image: image-set(
"hero.avif" type("image/avif"),
"hero.webp" type("image/webp"),
"hero.jpg" type("image/jpeg")
);
}
/* type() in attr(): read data-color as a color */
.swatch {
background-color: attr(data-color type(<color>), gray);
}
The support data shown is for type() in image-set(). Typed attr() with type() is supported from Chrome and Edge 133, with Firefox and Safari support still arriving.
Interactive Demo
data-size="200px" data-color="#f72585" data-angle="15deg"
Syntax
/* in image-set() */
type( <string> )
/* in attr() */
type( <syntax> )
Values
- <string>In image-set(): a MIME type such as "image/avif", "image/webp" or "image/png". Options with unsupported types are skipped.
- <syntax>In attr(): a CSS syntax such as <color>, <length>, <number>, <angle> or a combination like <length> | auto, describing how to parse the attribute value.
Example
Browser Support
The following information will show you the current browser support for the CSS type() function. Hover over a browser icon to see the version that first introduced support for this CSS function.
Desktop





Tablets & Mobile





