Mapbox/MapLibre to CSS Font Utility

3.2.0 · active · verified Sun Apr 19

mapbox-to-css-font is a focused JavaScript/TypeScript utility designed to convert Mapbox GL Style font definitions, whether single font names or complex fontstacks, into a CSS-compatible `font` shorthand string. The package is currently at version 3.2.0 and is actively maintained, with recent minor releases addressing bug fixes and dependency updates. Its primary function involves parsing Mapbox-specific font declarations and generating a standard CSS font property string, handling details like font size and line height. A key differentiating feature is its intelligent font replacement mechanism, which substitutes common Mapbox/MapLibre fonts (e.g., "Arial Unicode MS", "DIN Pro") with widely available system fonts or open-source alternatives like Arial or Barlow, ensuring visual consistency when the original fonts are not present. It also correctly extracts style and weight from the primary font within a fontstack and applies it consistently.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates converting single Mapbox font names and fontstacks into CSS `font` properties, including how the utility handles font replacement and style extraction.

import parseFont from 'mapbox-to-css-font';

// Example 1: Single font name
const cssFont1 = parseFont('Open Sans Regular', 16, 1.2);
console.log(`Single font: ${cssFont1}`);
// Expected: 'normal 400 16px/1.2 "Open Sans"'

// Example 2: Fontstack with Mapbox-specific replacement
const cssFont2 = parseFont(['DIN Pro Medium', 'Arial Unicode MS Regular'], 18);
console.log(`Fontstack (DIN Pro): ${cssFont2}`);
// Expected: 'normal 500 18px "Barlow", "Arial"'

// Example 3: Fontstack without line height
const cssFont3 = parseFont(['Roboto Bold', 'sans-serif'], 20);
console.log(`Fontstack (Roboto Bold): ${cssFont3}`);
// Expected: 'normal 700 20px "Roboto", sans-serif'

view raw JSON →