Fylgja Bar Chart
A bar chart compares a few values side by side. Here it is a plain <table> with a
row per bar, and CSS paints each data cell as a bar sized from its value. Nothing is
drawn in a <canvas> or SVG, so the chart keeps every table feature for free: row
and column headers for screen readers, selectable text, and a readable fallback
when the styles do not load.
Preview
| Browser | Visitors |
|---|---|
| Chrome | 3,120 |
| Safari | 1,840 |
| Firefox | 960 |
| Edge | 610 |
| Other | 240 |
<table class="bar-chart w-full" style="--max: 3500">
<caption class="text-start font-bold" style="--py: 0 0.5rem">Visitors by browser</caption>
<thead>
<tr class="grid">
<th scope="col">Browser</th>
<th scope="col">Visitors</th>
</tr>
</thead>
<tbody>
<tr class="grid">
<th scope="row">Chrome</th>
<td class="flex gap-sm" style="--value: 3120">3,120</td>
</tr>
<tr class="grid">
<th scope="row">Safari</th>
<td class="flex gap-sm" style="--value: 1840">1,840</td>
</tr>
<tr class="grid">
<th scope="row">Firefox</th>
<td class="flex gap-sm" style="--value: 960">960</td>
</tr>
<tr class="grid">
<th scope="row">Edge</th>
<td class="flex gap-sm" style="--value: 610">610</td>
</tr>
<tr class="grid">
<th scope="row">Other</th>
<td class="flex gap-sm" style="--value: 240">240</td>
</tr>
</tbody>
</table>.bar-chart {
--table-py: 0.25rem;
--table-px: 0 0.5rem;
--table-stroke: transparent;
--bar-color: var(--brand);
--bar-size: 1.25rem;
--bar-label-size: 6rem;
--bar-value-size: 4rem;
tr {
grid-template-columns: var(--bar-label-size) 1fr;
}
:is(th, td) {
background: none;
}
tbody td {
align-items: center;
&::before {
content: "";
flex: none;
inline-size: calc(var(--value) / var(--max) * (100% - var(--bar-value-size)));
block-size: var(--bar-size);
border-radius: var(--radius-2);
background: var(--bar-color);
}
}
}
Usage
- Write the data as a normal table, with a
<caption>, a header row and one<tr>per bar. Each row starts with a<th scope="row">for its label. - Repeat each number as
--valueon its cell, for examplestyle="--value: 3120". The visible text can stay formatted. - Set
--maxon the table. Bars are drawn as a share of it, so a cell with half of--maxfills half of the plot. - Add the
bar-chartclass and the CSS above. Layout comes from Fylgja utilities in the markup:gridon each row,flex gap-smon each value cell, andw-fullon the table.
Anatomy
| Part | Responsibility |
|---|---|
<caption> |
The chart title, and the table’s accessible name. |
<th scope="row"> |
The label for each bar. |
<td> with --value |
The value as text, and the number that sizes the bar. |
--max on the table |
The value that fills the full plot width. |
td::before |
The bar itself, drawn before the value text. |
Tune the look with --bar-color, --bar-size (the bar thickness),
--bar-label-size (the label column) and --bar-value-size (the room kept for the
value text after the longest bar). Cell spacing and borders use the base table
variables, --table-py, --table-px and --table-stroke.
Accessibility
- The table keeps its semantics, so a screen reader announces each value with its row and column header. The bars are pseudo elements and are not read out.
- The
<caption>names the table. Keep it, or the chart has no title for assistive technology. - Every value is visible as text next to its bar, so nobody depends on the bar length or color to read the data.
Variants
Vertical
Add --vertical to draw columns instead of bars, which suits values over time. The
<tbody> becomes the plot with flex gap text-center, and --size-y sets its height.
Rows and value cells switch to flex-col. The header row has no visible place in
a column chart, so it gets sr-only and stays available to screen readers. Add the
CSS below on top of the bar chart CSS.
| Month | Sign-ups |
|---|---|
| Jan | 180 |
| Feb | 240 |
| Mar | 310 |
| Apr | 280 |
| May | 420 |
| Jun | 465 |
<table class="bar-chart --vertical" style="--max: 500">
<caption class="text-start font-bold" style="--py: 0 0.5rem">Sign-ups per month</caption>
<thead class="sr-only">
<tr>
<th scope="col">Month</th>
<th scope="col">Sign-ups</th>
</tr>
</thead>
<tbody class="flex gap text-center" style="--size-y: 12rem">
<tr class="flex-col">
<th scope="row">Jan</th>
<td class="flex-col gap-sm" style="--value: 180">180</td>
</tr>
<tr class="flex-col">
<th scope="row">Feb</th>
<td class="flex-col gap-sm" style="--value: 240">240</td>
</tr>
<tr class="flex-col">
<th scope="row">Mar</th>
<td class="flex-col gap-sm" style="--value: 310">310</td>
</tr>
<tr class="flex-col">
<th scope="row">Apr</th>
<td class="flex-col gap-sm" style="--value: 280">280</td>
</tr>
<tr class="flex-col">
<th scope="row">May</th>
<td class="flex-col gap-sm" style="--value: 420">420</td>
</tr>
<tr class="flex-col">
<th scope="row">Jun</th>
<td class="flex-col gap-sm" style="--value: 465">465</td>
</tr>
</tbody>
</table>.bar-chart.--vertical {
--table-px: 0;
--bar-size: 2.5rem;
tbody tr {
--flex-dir: column-reverse;
}
tbody td {
flex: 1;
justify-content: end;
&::before {
order: 1;
inline-size: var(--bar-size);
block-size: calc(var(--value) / var(--max) * (100% - 1lh));
}
}
}
Related: Table, Donut Chart, Gauge.
FAQ
Why a table and not a canvas or SVG chart library?
The data is a table, so the markup should be one. Screen readers can move through it row by row, it copies into a spreadsheet, and it still reads fine when the CSS does not load. The chart is only a way of painting it.
Why is every value written twice?
CSS cannot read the text inside a cell, so the number is repeated as the --value custom property for the bar width. The visible text stays formatted for people, such as 3,120, while --value holds the plain number.
Can CSS find the highest value for me?
No. CSS cannot compare siblings, so set --max on the table yourself. Use the largest value, or a round number above it, so the longest bar does not touch the edge.
How do I color a single bar?
Set --bar-color on that cell, next to its --value. Keep the meaning in the text as well, since color alone does not reach every reader.