Fylgja Gauge
A gauge shows a single value against its range as a dial, the way a dashboard
shows CPU load or battery level. It is the native <meter> from the
Meter component with a different look. The dial reads
value, min, max, low, high and optimum from the element with typed
attr(), so the markup is all the data the chart needs, with no custom properties
to keep in sync.
Preview
<div class="flex-wrap gap">
<label class="gauge inline-grid align-center gap-sm">
CPU
<meter min="0" max="100" low="70" high="90" optimum="0" value="59">59%</meter>
<span class="lead" aria-hidden="true">59%</span>
</label>
<label class="gauge inline-grid align-center gap-sm">
Memory
<meter min="0" max="16" low="10" high="14" optimum="0" value="12.4">12.4 GB</meter>
<span class="lead" aria-hidden="true">12.4 GB</span>
</label>
<label class="gauge inline-grid align-center gap-sm">
Battery
<meter min="0" max="100" low="20" high="50" optimum="100" value="14">14%</meter>
<span class="lead" aria-hidden="true">14%</span>
</label>
</div>The gauge CSS
.gauge {
--gauge-size: 8rem;
--gauge-thickness: 0.22;
}
@supports (width: attr(x type(<length>))) {
/* A flat rule, as the attr() polyfill does not read nested ones */
.gauge meter {
--_min: attr(min type(<number>), 0);
--_max: attr(max type(<number>), 1);
--_value: clamp(var(--_min), attr(value type(<number>), 0), var(--_max));
--_low: attr(low type(<number>), var(--_min));
--_high: attr(high type(<number>), var(--_max));
--_optimum: attr(optimum type(<number>), calc((var(--_min) + var(--_max)) / 2));
}
.gauge {
:is(meter, span) {
grid-area: 1 / 1;
margin-block-end: calc(var(--gauge-size) / -6);
}
meter {
--_fill: calc((var(--_value) - var(--_min)) / (var(--_max) - var(--_min)));
/* Regions as the browser counts them: 0 below low, 1 between, 2 above high */
--_region: calc(
1 - max(0, sign(var(--_low) - var(--_value))) + max(0, sign(var(--_value) - var(--_high)))
);
--_best: calc(
1 - max(0, sign(var(--_low) - var(--_optimum))) + max(0, sign(var(--_optimum) - var(--_high)))
);
--_distance: abs(var(--_region) - var(--_best));
--_color: color-mix(
in oklab,
var(--error, #e00) calc(max(0, var(--_distance) - 1) * 100%),
color-mix(
in oklab,
var(--warning, #ffb900) calc(min(1, var(--_distance)) * 100%),
var(--success, #107c10)
)
);
--_track: var(--track-color, color-mix(in oklab, var(--root-bg) 85%, var(--root-fg)));
/* Round caps are dots centered on the ring, at each end of the arcs */
--_cap: calc(var(--gauge-size) * var(--gauge-thickness) / 4);
--_arc: calc(50% * (1 - var(--gauge-thickness) / 2));
--_end: calc(-135deg + var(--_fill) * 270deg);
--_start-color: color-mix(in oklab, var(--_color) calc(sign(var(--_fill)) * 100%), var(--_track));
inline-size: var(--gauge-size);
block-size: var(--gauge-size);
border-radius: 50%;
background:
radial-gradient(
circle at calc(50% + sin(var(--_end)) * var(--_arc)) calc(50% - cos(var(--_end)) * var(--_arc)),
var(--_color) calc(var(--_cap) - 0.5px),
transparent calc(var(--_cap) + 0.5px)
),
radial-gradient(
circle at calc(50% - sin(135deg) * var(--_arc)) calc(50% - cos(135deg) * var(--_arc)),
var(--_start-color) calc(var(--_cap) - 0.5px),
transparent calc(var(--_cap) + 0.5px)
),
radial-gradient(
circle at calc(50% + sin(135deg) * var(--_arc)) calc(50% - cos(135deg) * var(--_arc)),
var(--_track) calc(var(--_cap) - 0.5px),
transparent calc(var(--_cap) + 0.5px)
),
conic-gradient(
from -135deg,
var(--_color) calc(var(--_fill) * 270deg),
var(--_track) 0 270deg,
transparent 0
);
mask: radial-gradient(
closest-side,
transparent calc((1 - var(--gauge-thickness)) * 100%),
#000 calc((1 - var(--gauge-thickness)) * 100% + 1px)
);
&::-webkit-meter-bar {
visibility: hidden;
}
&::-moz-meter-bar {
visibility: hidden;
}
}
}
}
Usage
- Write a regular
<meter>withmin,maxandvalue. Addlow,highandoptimumto color it by state. - Wrap it in a
<label class="gauge inline-grid align-center gap-sm">with the name as text, so the meter is labelled without afor. - Add a
<span class="lead" aria-hidden="true">after the meter with the value as you want it shown. CSS cannot format a number, so this is the text in the middle of the dial. - Add the CSS from under the preview. Size the dial with
--gauge-size, and the ring with--gauge-thicknessas a share of the radius, such as0.22.
Anatomy
| Part | Responsibility |
|---|---|
<label class="gauge"> |
Names the meter and stacks the dial, value and name. |
<meter> |
Holds the data, and is painted as the dial. |
<span aria-hidden="true"> |
The formatted value in the middle of the dial. |
attr() reads |
Pull the numbers from the meter’s attributes into the CSS. |
conic-gradient() |
Draws the 270 degree arc, filled up to the value. |
radial-gradient() dots |
Round off both ends of the track and the end of the fill, like the meter’s radius. |
--success / --warning / --error |
Color the arc by state, matching the Meter. |
Accessibility
- The meter keeps its role and value, so a screen reader announces the name from the label and the value from the element. The dial is only a background.
- The value span is hidden from assistive technology, because the meter already reports its value.
- State is shown by color, so keep the value in text and pick thresholds that a reader can make sense of from the number alone.
Browser support
The dial needs typed attr(), plus sign() and abs() to work out the state.
Typed attr() is the newest of these and is available in Chromium based browsers
first. The styles sit in an @supports block, so other browsers fall back to the
plain Meter bar with the value under it.
With the attr() polyfill
The Fylgja CSS attr() Polyfill brings the
dial to every browser. It runs at build time, scans your markup for the attribute
values you use, and writes a static rule for each one, such as value="59".
Browsers with typed attr() keep using the original rules.
- Remove the
@supportswrapper and keep its contents. The polyfill adds its own guarded fallback, and inside the wrapper that fallback would never apply. - Keep the
attr()reads in the flat.gauge meterrule. The polyfill does not read declarations inside nested rules. - Point
contentat your templates. Values that only exist at runtime, like a live CPU reading set from JavaScript, cannot be scanned, so safelist their range, such as"value": "0..100". Every value becomes a rule, so keep the range to whole numbers where you can.
Related: Meter, Progress, Donut Chart.
FAQ
Why is my gauge a flat bar?
The dial needs typed attr(), which reads the meter's numbers straight from its attributes. Browsers without it skip the dial styles and show the regular meter bar with the value below it, so nothing is lost. To get the dial there too, compile the CSS with the CSS attr() Polyfill.
How does the gauge pick its color?
It follows the same rules as the native meter. low and high split the range into three regions, optimum says which region is good, and the dial turns amber one region away from it and red two regions away.
How do I make a higher value the good one?
Move optimum to the high end, as in the battery gauge above. Leave out low, high and optimum altogether and the dial stays green, the same as a meter without them.