Two recent CSS functions make it possible to build a bar chart without the layout workarounds the technique used to require. sibling-index() and the upgraded attr() function do most of the heavy lifting: one decides which column each bar sits in, the other how many rows tall it should grow.

Starting With a Grid

The foundation is a standard grid container. The chart payload is a percentage, meaning a number out of 100, so a grid with 100 rows is a reasonable test bed for the approach.

.chart {
  display: grid;
  grid-template-rows: repeat(100, 1fr);
  /* etc. */
}

Each bar is a grid item placed with the grid-column and grid-row properties.

.chart-bar {
  grid-column:  sibling-index();
  grid-row: span attr(data-value number);
  /* etc. */
}

Two functions deserve attention here. sibling-index() is new, and as of this writing its browser support is incomplete: the latest Chrome and Safari ship it, Firefox does not, and it appears not to be supported on iOS. The attr() function is older but has been upgraded — it now accepts data attributes, so a value like data-value="32" in the markup becomes readable.

With those two pieces in place, the chart is essentially complete in vanilla CSS. A fallback keeps the result visible in browsers that have not adopted the new functions.

Columns, Set Automatically

Declaring sibling-index() on grid-column places items in consecutive columns explicitly rather than letting the grid guess. The first <li> lands in the first column, the second in the second, and so on.

That order is generated without any manual accounting through CSS variables.

/* First bar: sibling-index() = 1 */
grid-column: sibling-index();

/* ...results in: */
grid-column: 1;
grid-column-start: 1; grid-column-end: auto;

/* Second bar: sibling-index() = 2 */
grid-column: sibling-index();

/* ...results in: */
grid-column: 2;
grid-column-start: 2; grid-column-end: auto;

/* etc. */

Height, From the Markup

Rows follow the same principle, except the span is variable: each bar occupies a number of rows proportional to the percentage it represents. The value comes straight from the data-value attribute, which tells the grid how tall the bar should be.

/* First bar: */
grid-row: span attr(data-value number);

/* ...results in: */
grid-row: span 32

/* Second bar: */
grid-row: span attr(data-value number);

/* ...results in: */
grid-row: span 46

Passing attr() a data type parameter — number, in this case — casts the retrieved value into that type. The attribute's raw string is therefore returned as a <number>, which is what the grid-row calculation expects.

Other Shapes of Chart

Once those mechanics are understood, variants are mostly a matter of rearranging what sibling-index() and attr() feed.

  • Adjusting the grid-row values changes the vertical direction the bars grow in.
  • Markers can stand in for bars entirely.
  • Swapping columns and rows produces a horizontal bar chart.

What Changed

Before these functions, charts in CSS demanded considerably more craft. Representative earlier techniques include:

  • Making Charts with CSS — Robin Rendle, 2015
  • Making A Bar Chart with CSS Grid — Robin Rendle, 2017
  • More CSS Charts, with Grid & Custom Properties — Miriam Suzanne, 2017
  • Overlapping Bar Charts — Saleh Mubasher, 2022