liveBook · Manning

Font-size property

As explained in D3.js in Action, Third Edition

The CSS font-size property specifies the size of rendered text.

The CSS font-size property sets the size of rendered text. In responsive SVG charts, text scales with the graphic, so labels that look clear on desktop can become tiny on phones. section 9.2.1

Use the property with a responsive sizing strategy:

  • Create a linear scale that maps screen or SVG width to font size, and clamp it so values stay within the chosen range. section 9.2.1 section 10.2.1
  • Apply the calculated value to chart text through D3’s style() method, then recalculate it when the window emits a resize event. section 9.2.1
  • Keep text at least 12 px, using larger sizes for primary content and smaller sizes only for secondary information. section 10.2.1 section 10.3

Responsive chart labels

A line chart can map widths from 600 to 1,000 pixels to font sizes from 26 to 16 pixels, apply the result to #line-chart text, and update windowWidth before redrawing on resize. section 9.2.1

const fontSizeScale = d3.scaleLinear()
  .domain([600, 1000])
  .range([26, 16])
  .clamp(true);

Inspect the clamped D3 linear scale used to calculate a responsive font-size value. section 9.2.1

How D3.js in Action, Third Edition covers it

How deep it goes
Explains fixed sizes, proportional scaling, clamping, D3 application, and resize updates, without covering CSS internals.
Examples
Uses line-chart text, axis labels, technology labels, count labels, linear scales, and resize event listeners.
What it assumes
Assumes basic CSS, SVG, JavaScript window properties, D3 selections, and event listeners.
What you can do after
Apply it via D3.js to resize chart text responsively and maintain readable labels on small screens.
How it connects
Connects font-size with CSS, SVG, D3.js, responsive design, chart labels, and accessibility.

Also explained in

Compare how 9 books explain font-size property →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of D3.js in Action, Third Edition. The section links point to the parts of the book it draws on.