Simple Bar Graphs with HTML and CSS Variables

2026-11-22 5:46 PM

#code #html #css #graph

I've been poking away at my student skill dashboard and the users index page really needed a facelift. I wanted to give a visual for their progress on the skills I've assessed so far in class with a little bar chart. I reached for some Python charting libraries which were super complicated, needed a bunch of dependency installs, and most required Javascript to render or to save a file on my filesystem. These progress bars change from time to time, so I wanted something more lightweight.

After fiddling for a while, I decided I didn't have the brain power and just threw a completed / total column on the table. This lists all students - current and past - which is why the totals change from row to row.

A table of progress values for students. The left number is the current number of skills they've demonstrated and the right number is the total number assessed.

After a cup of tea, I realized that I can just use CSS custom properties to do this. We're working with grid layouts in my web development class, so that's a nice tie in for a real-world application.

First, I updated my template file. I originally wanted to use data-* attributes and then access them in the CSS with the attr() function. The problem with that method is that attr() returns a string and CSS grid needs integers. So, I went with putting an inline style with a custom property in the template that is picked up and styled individually in each row.

<div class="graph" style="--data-cols: {{ user.assessed }}>
  <span style="--data-colspan: {{ user.proficient }}></span>
</div>
.graph {
  display: grid;
  grid-template-columns: repeat(var(--data-cols), 1fr);
  height: 20px;
  background-color: var(--red);
}

.graph > span {
  grid-column: 1 / var(--data-colspan);
  background-color: var(--green);
  height: 100%;
}

This turns into:

A column of horizontal stacked bar graphs. The green bar spans a portion of the space showing student proficiency. Red is shown stacked on the green for standards they have not yet met. Each bar is a different amount of green and red, but all of the bars are the same overall width.

So simple and just as effective as a full on chart rendering library.

Share this post