Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

type(): three contexts with different meanings

Distinguish a CSS syntax requirement from an image MIME hint; type() is not a standalone value converter.

Version: CSS Modules

Syntax

attr(data-size type(<length>), 12px); image-set(url("photo.avif") type("image/avif")); @function --f(--x type(<length> | <percentage>)) returns type(<length> | <percentage>) { result: var(--x); }

Inside attr(), type() asks the browser to parse the attribute as a CSS length. The string 40px satisfies that requirement; a nonzero unitless 40 does not. A supplied fallback is used when the attribute is missing or cannot be parsed as required. This is different from attr(data-size px), which interprets a numeric attribute using a specified unit. type() by itself is not a width value.

Inside image-set(), type("image/avif") supplies an image-format MIME hint, with a quoted string rather than a CSS grammar such as . A candidate with an unsupported or invalid MIME hint is excluded from selection; the function can still have other usable candidates. The hint does not convert the file or prove that the resource is valid. CSS.supports() accepting the declaration only proves syntax support, not decoding or selection of that candidate.

The custom-functions draft also uses type() for parameter and return syntax in @function. A combined syntax such as | needs the wrapper. That form describes admissible CSS values rather than image formats. Support for one context does not establish support for another; test the context actually used.

In the recorded Chromium 157 run, typed attr() yields 40px for 40px and the 12px fallback for both 40 and a missing attribute. A parsed custom-function rule returns an actual width of 25px. The image-set() probes accept both a known and an invented MIME string syntactically, while width: type() is rejected. These probes do not load an image or demonstrate format fallback rendering.

Examples

const box=document.createElement('div');
box.style.cssText='width:attr(data-size type(<length>), 12px);height:10px';document.body.append(box);
const width=value=>{if(value===null)box.removeAttribute('data-size');else box.setAttribute('data-size',value);return getComputedStyle(box).width};
const length=width('40px'),bad=width('40'),missing=width(null);
const sheet=new CSSStyleSheet();
sheet.replaceSync('@function --typed(--x type(<length> | <percentage>)) returns type(<length> | <percentage>) { result: var(--x); }');
document.adoptedStyleSheets=[...document.adoptedStyleSheets,sheet];
box.style.width='--typed(25px)';const custom=getComputedStyle(box).width;
console.log('attr='+length+','+bad+','+missing+'; custom-rule='+sheet.cssRules.length+'; custom-width='+custom+'; image-type-syntax='+CSS.supports('background-image','image-set(url("data:,") type("image/avif"))')+'; unknown-mime-syntax='+CSS.supports('background-image','image-set(url("data:,") type("image/x-not-real"))')+'; standalone='+CSS.supports('width','type(<length>)'));

Result

attr=40px,12px,12px; custom-rule=1; custom-width=25px; image-type-syntax=true; unknown-mime-syntax=true; standalone=false

The example measures attribute substitution and a custom-function result. Its two image checks are deliberately limited to syntax; true does not certify the invented MIME format.

Sources