files/en-us/web/api/cssfontfeaturevaluesrule/index.md
{{APIRef("CSSOM")}}
The CSSFontFeatureValuesRule interface represents an {{cssxref("@font-feature-values")}} at-rule. The values of its instance properties can be accessed with the CSSFontFeatureValuesMap interface.
@font-feature-values allows developers to associate, for a given font face, a human-readable name with a numeric index that controls a particular OpenType font feature.
For features that select alternative glyphs (stylistic, styleset, character-variant, swash, ornament or annotation), the {{cssxref("font-variant-alternates")}} property can then reference the human-readable name in order to apply the associated feature.
This is convenient, because it allows the same name to be used of represent a set of alternative glyphs across a number of fonts.
{{InheritanceDiagram}}
Inherits properties from its ancestor {{domxref("CSSRule")}}.
Inherits methods from its ancestor {{domxref("CSSRule")}}.
In this example, we declare two {{cssxref("@font-feature-values")}} one for the Font One font family, and the other for Font Two.
In both declarations we define that the name "nice-style" can be used to represent the styleset alternate glyphs for both of the fonts, specifying the index for that alternate in each font family.
The alternate glyphs are then applied for any .nice-look class, using {{cssxref("font-variant-alternates")}} and passing the name to the styleset() function.
We then use the CSSOM to read these declaration as CSSFontFeatureValuesRule instances, displaying them into the log.
/* At-rule for "nice-style" in Font One */
@font-feature-values Font One {
@styleset {
nice-style: 12; /* name used to represent the alternate set of glyphs at index 12 */
}
}
/* At-rule for "nice-style" in Font Two */
@font-feature-values Font Two {
@styleset {
nice-style: 4;
}
}
/* Apply the at-rules with a single declaration */
.nice-look {
font-variant-alternates: styleset(
nice-style
); /* name selects different index for same alternate in different fonts */
}
<pre id="log"></pre>
#log {
height: 40px;
overflow: scroll;
padding: 0.5rem;
border: 1px solid black;
}
const logElement = document.querySelector("#log");
function log(text) {
logElement.innerText = `${logElement.innerText}${text}\n`;
logElement.scrollTop = logElement.scrollHeight;
}
const rules = document.getElementById("css-output").sheet.cssRules;
const fontOne = rules[0]; // A CSSFontFeatureValuesRule
log(`The 1st '@font-feature-values' family: "${fontOne.fontFamily}".`);
const fontTwo = rules[1]; // Another CSSFontFeatureValuesRule
log(`The 2nd '@font-feature-values' family: "${fontTwo.fontFamily}"`);
{{EmbedLiveSample("read_font_family", "100%", "100px")}}
{{Specifications}}
{{Compat}}