Favorites

type() CSS Function

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

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.

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 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

Typed attr()

data-size="200px" data-color="#f72585" data-angle="15deg"

Demo Code
HTML Code
<div class="demo-element" data-size="200px" data-color="#f72585" data-angle="15deg">Typed attr()</div>
<p class="demo-note">data-size="200px" data-color="#f72585" data-angle="15deg"</p>
CSS Code
.demo-note {
	margin: 6px 0 0;
	font-size: 12px;
	color: #666;
}
.demo-element {
	width: 100px;
	height: 60px;
	margin: 30px;
	background: #4361ee;
	color: #fff;
	padding: 6px;
	box-sizing: border-box;
	font-size: 13px;
	width: attr(data-size type(<length>));
}

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

<div class="wrap">
<h1>CSS type() examples</h1>
<div class="grid">
<div class="card">
<div class="box formats">Blue = image/png chosen</div>
<div class="label">image-set(... type("image/x-unknown"), ... type("image/png"))</div>
</div>
<div class="card">
<div class="box swatch" data-color="tomato">data-color="tomato"</div>
<div class="label">attr(data-color type(&lt;color&gt;), #64748b)</div>
</div>
<div class="card">
<div class="box swatch" data-color="rebeccapurple">data-color="rebeccapurple"</div>
<div class="label">attr(data-color type(&lt;color&gt;), #64748b)</div>
</div>
</div>
<p class="note">Browsers without typed attr() show the gray fallback in the last two boxes.</p>
</div>
/* Base demo layout */
* { box-sizing: border-box; }

body {
margin: 24px;
font-family: system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial;
background: #f5f7fb;
color: #102a43;
}

.wrap {
max-width: 860px;
margin: 0 auto;
}

h1 {
font-size: 20px;
margin: 0 0 16px;
}

.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
gap: 16px;
}

.card {
background: #fff;
border-radius: 12px;
padding: 16px;
box-shadow: 0 6px 18px rgba(16, 42, 67, 0.08);
}

.label {
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, monospace;
font-size: 12px;
color: #486581;
margin-top: 10px;
word-break: break-word;
}

.note {
font-size: 13px;
color: #486581;
margin-top: 14px;
}

/* type() examples */
.box {
height: 110px;
border-radius: 10px;
display: grid;
place-items: center;
color: #fff;
font-weight: 700;
text-shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
}

/* image-set() with type(): unsupported types are skipped */
.formats {
background-image: linear-gradient(135deg, #64748b, #334155); /* fallback */
background-image: image-set(
linear-gradient(135deg, #db2777, #f97316) type("image/x-unknown"),
linear-gradient(135deg, #2563eb, #06b6d4) type("image/png")
);
}

/* attr() with type(): the attribute is read as a color */
.swatch {
background-color: #64748b; /* fallback */
background-color: attr(data-color type(), #64748b);
}
Open in editor

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.

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.