mrkeyoor.com_
Wed 23 Sept 00:32 UTC
npmWeb Frontendupdated 22 Sept 2026

cssfontparser review

cssfontparser turns an old-style CSS font shorthand into an object containing style, variant, weight, pixel size, pixel line height, and a family array. A parent font string lets it resolve em and percentage sizes, and a DPI argument controls physical-unit conversion. Version 1.2.1, released in 2015, fixed unitless line-height so 16px/1.2 produces 19.2 pixels. The implementation is one dependency-free CommonJS file with a narrow regular expression and a process-wide result cache. Our install had no TypeScript declarations, though both require() and ESM import loaded it.

Verdict

cssfontparser 1.2.1 installed in 0.5 seconds and bundled to 1.3 KB gzipped in our sandbox, but its parser code has been unchanged since 2015 and returns shared cached objects. Keep it for known legacy shorthand strings; choose a maintained CSS parser for user input or current syntax.

We installed it

Lab card: what happened when we installed cssfontparserScreenshot of cssfontparser documentation
Install✓ · 0.5s1 package on disk · 1 MB
ImportESM import works · require() works · CommonJS package
Browser1.3 KBgzipped (2.5 KB minified), bundled with esbuild
Typesno TypeScript types found
Known vulns00 critical · 0 high · 0 moderate · 0 low (npm audit)

Answers from our run

Does cssfontparser install cleanly?

Yes. In a fresh container with an empty cache, npm install cssfontparser finished in 0.5s, leaving 1 package and 1 MB on disk. npm audit reported no known vulnerabilities.

How much does cssfontparser add to a browser bundle?

1.3 KB gzipped (2.5 KB minified) when the whole package is bundled for the browser with esbuild. Importing only part of it is usually smaller.

Does cssfontparser work with both ESM and CommonJS?

Yes. Both import 'cssfontparser' and require('cssfontparser') worked in Node 22 in our run. The package is published as CommonJS.

Does cssfontparser include TypeScript types?

No type declarations were found in our install, so TypeScript users need their own declarations.

cssfontparser or css-font-parser: which should you use?

css-font-parser: Use it when you want a newer package focused specifically on the font shorthand. cssfontparser 1.2.1 installed in 0.5 seconds and bundled to 1.3 KB gzipped in our sandbox, but its parser code has been unchanged since 2015 and returns shared cached objects.

When should you not use cssfontparser?

Input can contain var(), calc(), rem, ch, viewport units, system fonts, escaped identifiers, or current generic families; the source regex has no grammar for them

API stability3/5The 3-position parse call and its style, variant, weight, size, lineHeight, family, and toString() result have remained fixed since version 1.2.1. That consistency helps old consumers. Several undocumented behaviors weaken the contract: repeated arguments share one cached object, invalid input can alternate between undefined and null, unresolved relative units leave missing numbers, and toString() captures parse-time values rather than serializing arbitrary later edits.
Docs2/5The README gives 1 installation command, 1 full example, the positional signature, the 96 DPI default, and the parent requirement for em and percentage sizes. The test file adds useful examples for physical units, inherit, family splitting, generic normalization, and unitless line-height. It never warns about the shared cache, mutation, unsupported ex conversion, modern CSS functions, invalid-return inconsistency, absent declarations, or source-changing serialization.
Maintenance1/5npm published version 1.2.1 on May 8, 2015. Its final parser commit fixed unitless line-height and adjusted the matching test. The only 2 later commits, both dated December 3, 2022, added an MIT license file and linked it from the README. GitHub reports 0 open issues and pull requests and does not mark the repository archived, but the implementation has received no syntax or packaging work for more than 11 years.
Ecosystem2/5npm counted 3,368,061 weekly downloads through August 24, 2026, yet GitHub shows only 9 stars and the README names no integrations. The high download count likely includes transitive use, while the public surface remains a parse function plus a 5-entry generics map. Our install found 0 dependencies and a 1.3 KB gzipped browser bundle, but it also found no types, exports map, plugin system, or shared AST used by current CSS tooling.

Use it if

  • Existing code already depends on this parser's exact object fields and pixel conversion behavior
  • Inputs are controlled font shorthands with simple family names and CSS 2-era units
  • A parent shorthand must resolve em or percentage sizes without a DOM
  • A tiny browser parser matters more than TypeScript declarations or current CSS grammar coverage
Skip it if

Setup reality

Our fresh installation of cssfontparser 1.2.1 completed in 0.5 seconds. It put 1 package and 1 MB on disk, and npm audit found 0 known vulnerabilities. The package is 24 KB unpacked with 0 direct and 0 peer dependencies. It is CommonJS without an exports map; require() and ESM import both succeeded on Node 22. No TypeScript declarations ship. Our all-exports browser build was 2.5 KB minified and 1.3 KB gzipped.

There are no credentials, native builds, or configuration files. The function takes the font string, an optional parent font string, and an optional DPI that defaults to 96. A numeric parent or CSSStyleDeclaration will not work. Relative em and percentage sizes need that second shorthand; without it, parsing may return an object whose size is undefined. Version 1.2.1 specifically computes a unitless line-height by multiplying it by the parsed size, as in 16px/1.2 becoming 19.2.

The main trap is its module-level cache. Repeating the same string, parent, and DPI returns the same object, so changing size or pushing into family changes what later callers see. Clone both the object and its family array before editing. toString() closes over the values assembled during the original parse, which means mutations are not reliably reflected in output. Failed input is cached as null, while the first failed call falls through without an explicit return; always test the result before reading fields.

The regular expression accepts a small slice of font syntax: familiar style, small-caps, old weight forms, several absolute units, em, percentages, and simple quoted or unquoted ASCII family names. It does not understand CSS functions, variables, modern relative units, digits in family names, or newer generic families. Although ex appears in the accepted unit pattern, the numeric converter has no ex case. A syntactic match can therefore still yield undefined numeric fields. Use browser parsing or a maintained CSS syntax library for untrusted stylesheets.

Patterns

Read the six classic font fields parse-shorthand

const parseFont = require('cssfontparser')

const font = parseFont('italic small-caps 700 16px/1.5 Georgia, serif')
console.log(font)
// { style: 'italic', variant: 'small-caps', weight: '700',
//   size: 16, lineHeight: 24, family: ['Georgia', 'serif'] }

Version 1.2.1 turns the unitless 1.5 line height into 24 pixels because the parsed font size is 16 pixels.

Guard every parse result reject-invalid-input

const parsed = parseFont(input)
if (!parsed || typeof parsed.size !== 'number' || !parsed.family) {
  throw new TypeError(`Unsupported font shorthand: ${input}`)
}

A failed first call can return undefined, while the cached failure is null. Relative sizes can also produce an object without size.

Calculate em from a parent shorthand resolve-em

const child = parseFont('1.5em sans-serif', '16px serif')
console.log(child.size) // 24

Argument 2 must be another font string that this parser accepts. A number or computed-style object cannot supply the parent size.

Calculate a percentage size resolve-percentage

const child = parseFont('75% Arial, sans-serif', '20px serif')
console.log(child.size) // 15

Without the 20px parent shorthand, size is undefined even though the returned object can still contain the family list.

Convert points at a chosen DPI set-dpi

const printFont = parseFont('12pt serif', null, 192)
console.log(printFont.size) // 32

Argument 3 defaults to 96 DPI. px bypasses it, while pt, pc, mm, cm, and in are converted with it.

Expand a unitless line height compute-line-height

const font = parseFont('16px/1.25 Arial, sans-serif')
console.log(font.lineHeight) // 20

Version 1.2.1 added this multiplication path. A line-height unit unsupported by numeric() can still become an unintended multiplier.

Resolve the inherit keyword inherit-parent

const inherited = parseFont('inherit', 'italic 14px Georgia, serif')
console.log(inherited.size) // 14

Calling parseFont('inherit') without argument 2 returns undefined. With a parent, inherit returns that cached parent object itself.

Emit the parser's normalized form serialize-result

const font = parseFont('italic 400 12px/2 Unknown Font, SANS-SERIF')
console.log(font.toString())
// italic 12px/24px "Unknown Font", sans-serif

The output drops weight 400, converts the line height to pixels, lowercases the recognized generic, and quotes the spaced family.

Copy before changing parsed data clone-cached-result

const parsed = parseFont('16px Arial, sans-serif')
const editable = {
  ...parsed,
  family: parsed.family.slice(),
}
editable.size = 18

The cache returns one object for identical arguments. Clone the nested family array as well as the top-level object before mutation.

Check the built-in generic table inspect-generics

const { generics } = require('cssfontparser')

if (generics[family.toLowerCase()]) {
  console.log('recognized legacy generic')
}

The exported map has 5 names: serif, sans-serif, cursive, fantasy, and monospace. Newer CSS generic families are absent.

Serialize edited fields yourself format-edited-copy

const parsed = parseFont('16px Arial, sans-serif')
const size = 18
const family = parsed.family.map((name) =>
  name.includes(' ') ? `"${name}"` : name
).join(', ')
const css = `${size}px ${family}`

parsed.toString() uses the values captured during parsing, so it is the wrong formatter after changing size or family.

Walk font fallbacks in order read-family-order

const parsed = parseFont('14px "Open Sans", Arial, sans-serif')
for (const family of parsed.family) {
  console.log(family)
}

The family split is driven by a narrow regular expression. Digits, escapes, functions, and unusual Unicode names need separate tests or another parser.

Alternatives

PackageRegistryPick it when
css-font-parsernpmUse it when you want a newer package focused specifically on the font shorthand
postcss-value-parsernpmUse it when you need a maintained value tokenizer and can implement the font grammar yourself
css-treenpmUse it when parsing, validating, walking, and regenerating broader CSS syntax all matter

More web frontend guides

postcss · react · react-dom · tailwindcss · htmlparser2 · tailwind-merge · the whole shelf →

How this guide is made: grounded in the library's documentation, release notes, changelog, and issue history, on a fixed rubric — not a hands-on install of every release. The 50 most-downloaded entries are additionally install-verified in clean containers. Corrections: contact the desk.