Capsize

Flipping how we define typography in CSS.

README

CapsizeCapsize

Capsize makes the sizing and layout of text as predictable as every other element on the screen.

Using font metadata, text can now be sized according to the height of its capital letters while trimming the space above capital letters and below the baseline.


  1. ``` sh
  2. npm install @capsizecss/core
  3. ```

  - Text size

Usage


createStyleObject


Returns a CSS-in-JS style object.

1. Import createStyleObject passing the relevant options.

  1. ```ts
  2. import { createStyleObject } from '@capsizecss/core';

  3. const capsizeStyles = createStyleObject({
  4.   fontSize: 16,
  5.   leading: 24,
  6.   fontMetrics: {
  7.     capHeight: 700,
  8.     ascent: 1058,
  9.     descent: -291,
  10.     lineGap: 0,
  11.     unitsPerEm: 1000,
  12.   },
  13. });
  14. ```

Note: It is recommended that you install the @capsizecss/metrics package and import the metrics from there:

  1. ```ts
  2. import { createStyleObject } from '@capsizecss/core';
  3. import arialMetrics from '@capsizecss/metrics/arial';

  4. const capsizeStyles = createStyleObject({
  5.   fontSize: 16,
  6.   leading: 24,
  7.   fontMetrics: arialMetrics,
  8. });
  9. ```

See the fontMetrics option documented below for more ways to obtain these metrics.

2. Apply styles to the text element, for example via the css prop.

  1. ```ts
  2. <div
  3.   css={{
  4.     // fontFamily: '...' etc,
  5.     ...capsizeStyles,
  6.   }}
  7. >
  8.   My capsized text
  9. </div>
  10. ```

createStyleString


Returns a CSS string that can be inserted into a style tag or appended to a stylesheet.

1. Import createStyleString passing the relevant options.

  1. ```ts
  2. import { createStyleString } from '@capsizecss/core';
  3. import arialMetrics from '@capsizecss/metrics/arial';

  4. const capsizedStyleRule = createStyleString('capsizedText', {
  5.   fontSize: 16,
  6.   leading: 24,
  7.   fontMetrics: arialMetrics,
  8. });
  9. ```

2. Add the styles into a stylesheet or style element and apply the specified class name.

  1. ```ts
  2. document.write(`
  3.   <style type="text/css">
  4.     ${capsizedStyleRule}
  5.   </style>
  6.   <div class="capsizedText">
  7.     My capsized text
  8.   </div>
  9. `);
  10. ```

Options


Text size


Capsize supports two methods of defining the size of text, capHeight and fontSize.

NOTE: You should only ever pass one or the other, not both.

#### `capHeight: `

Sets the height of the capital letters to the defined value. Defining typography in this way makes aligning to a grid or with other elements, e.g. icons, a breeze.

Highlighting the cap height

#### `fontSize: `

Setting the font size allows you to get all the benefits of the white space trimming, while still specifying an explicit font-size for your text. This can be useful when needed to match a concrete design spec or fitting into an existing product.

Highlighting the font size

Line height


Capsize supports two mental models for specifying line height, lineGap and leading. If you pass neither the text will follow the default spacing of the specified font, e.g. line-height: normal.

NOTE: You should only ever pass one or the other, not both.

#### `lineGap: `

Sets the number of pixels between lines, as measured between the baseline and cap height of the next line.

Highlighting the line gap

#### `leading: `

Sets the line height to the provided value as measured from the baseline of the text. This aligns the web with how typography is treated in design tools.

Highlighting the leading

Font Metrics


This metadata is extracted from the metrics tables inside the font itself. There are a number of ways to find this information:

- If using a Google Font or system font, install the @capsizecss/metrics package and import the metrics by name. For example:
  1. ```ts
  2.   import arialMetrics from '@capsizecss/metrics/arial';
  3. ```
- If using a font from a file, install the @capsizecss/unpack package and extract the metrics from the font file directly. For example:

  1. ```ts
  2.   import { fromFile } from '@capsizecss/unpack';

  3.   const metrics = await fromFile(filePath);
  4. ```

- Or, use the Capsize website to find these by selecting a font and referencingMetrics tab in step 3.

Core


The core package also provides access to lower level values for a specific font and font size combination.

precomputeValues


Returns all the information required to create styles for a specific font size given the provided font metrics. This is useful for integrations with different styling solutions.

  1. ```ts
  2. import { precomputeValues } from '@capsizecss/core';

  3. const capsizeValues = precomputeValues({
  4.   fontSize: 24,
  5.   fontMetrics: {
  6.     ...
  7.   }
  8. })

  9. // => {
  10. //  fontSize: string,
  11. //  lineHeight: string,
  12. //  capHeightTrim: string,
  13. //  baselineTrim: string,
  14. //}
  15. ```

getCapHeight


Return the rendered cap height for a specific font size given the provided font metrics.

  1. ```ts
  2. import { getCapHeight } from '@capsizecss/core';

  3. const actualCapHeight = getCapHeight({
  4.   fontSize: 24,
  5.   fontMetrics: {
  6.     ...
  7.   }
  8. })

  9. // => number
  10. ```

Integrations



Thanks


- Vincent De Oliveira for writing Deep dive CSS: font metrics, line-height and vertical-align, which provided the research needed to build all this.
- Devon Govett for creating Fontkit, which does all the heavy lifting of extracting the font metrics under the covers.
- SEEK for giving us the space to do interesting work.

License


MIT.