Skip to content
Content Blocks docsv0.8.0Website

Content Blocks documentation

Content Blocks are web components that give you ready-made layouts and behaviour as native HTML elements. Every page here is built from the library's own docs.json, so it always matches the version you install.

Install

From a CDN: each component loads the first time its tag appears
<script type="module"
  src="https://cdn.jsdelivr.net/npm/@content-blocks/js/dist/content-blocks/content-blocks.esm.js">
</script>
From npm: import the reset, then each component you use
$ npm install @content-blocks/js
import '@content-blocks/js/reset.css';
import '@content-blocks/js/components/content-hero-block';

How the pieces fit

Formatters place and space things. Blocks are the sections of a page. Elements are the content inside them. Components add behaviour, and can hold blocks of their own.

flowchart LR
  formatters["<b>Formatters</b><br/>content-spacer<br/>content-grid-formatter"]:::kind
  blocks["<b>Blocks</b><br/>content-hero-block<br/>content-split-block<br/>content-text-block<br/>content-tout-block"]:::kind
  elements["<b>Elements</b><br/>content-image<br/>content-line<br/>content-button<br/>content-video"]:::kind
  components["<b>Components</b><br/>content-accordion<br/>content-carousel<br/>content-navigation-block<br/>content-overlay<br/>content-tabs"]:::kind
  formatters -- "place and space" --> blocks
  blocks -- "hold" --> elements
  components -- "can hold" --> blocks
  elements -. "a button's href opens an overlay" .-> components
  classDef kind fill:#fff,stroke:#141413,color:#141413,text-align:left
How formatters, blocks, elements and components nest on a page

Responsive values

Most properties take one value per breakpoint, separated by |. Each value applies from its breakpoint up.

<!-- 1 column on phones, 2 from tablet, 4 from desktop -->
<content-grid-formatter columns="1|tablet=2|desktop=4">

Default breakpoints

The defaults line up with Apple's screen widths in CSS pixels. Every iPhone in portrait is mobile, iPads span tablet to desktop as they turn and grow, and wide andultrawide match laptops and desktop monitors.

NameFromLines up withcolumns="1|tablet=2|desktop=4"
mobile0pxiPhone in portrait (320–440px)1
tablet768pxiPad in portrait (768–834px)2
laptop1024pxiPad in landscape, 12.9-inch iPad Pro in portrait2
desktop1366px12.9-inch iPad Pro in landscape4
wide1440px13-inch MacBook Air and most laptops4
ultrawide1920px1080p desktop monitors and larger4

The first value needs no name. You can also use a pixel width (1|640=2), or leave the names out altogether to give one value per breakpoint in order (1|2|4).

Custom breakpoints

Set your own names and widths before the components load. They replace the defaults, so list every breakpoint you want to use.

<script>
  window.$contentBlocks = {
    breakpoints: { phone: 0, tablet: 700, desktop: 1200 },
  };
</script>

Or, from npm, at any time. Components already on the page update.

import { setBreakpoints } from '@content-blocks/js';

setBreakpoints({ phone: 0, tablet: 700, desktop: 1200 });

The current breakpoint is also set on <html data-breakpoint="tablet">, for your own CSS.

Blocks

The sections a page is made of.

<content-hero-block>

A full width section with an image or video behind, or above, its text. Usually the first block on a page.

<content-split-block>

Two columns side by side, such as an image next to text, that stack on smaller screens.

<content-text-block>

A block of text with its alignment, vertical position and minimum height set per breakpoint.

<content-tout-block>

A card that promotes something: an image or video with text below it, and an optional layer over the media that can show on hover.

Elements

The content inside the blocks.

<content-button>

A link or button. Wrapped around block content, such as a card, it makes the whole area clickable, and opens a content-overlay when its href points at one.

<content-image>

A responsive image or muted looping video per breakpoint with native lazy loading.

<content-line>

One line of text, such as a heading or a paragraph, with its tag, alignment and maximum width set per breakpoint.

<content-video>

A video player for YouTube or self hosted files, built on Video.js.

Formatters

Layout and spacing around anything.

<content-grid-formatter>

Lays its children out in a grid or a row, with the number of columns, gaps and alignment set per breakpoint.

<content-spacer>

Adds space around its content and limits its width, with its padding and maximum width set per breakpoint.

Components

Interactive patterns, accessible by default.

<content-accordion>

A button that shows and hides the content below it, such as an answer in a list of questions.

<content-carousel>

Slides that move sideways, built on Swiper, with labelled controls and a pause button when they move on their own.

<content-navigation-block>

Site navigation that collapses into a menu button on the breakpoints you choose, with a skip link for keyboard users.

<content-overlay>

A dialog that opens over the page, or inside another element, and keeps keyboard focus inside it while open.

<content-tabs>

Accessible tabs: a tablist of buttons and the panels they show, with arrow key support.