An Eleventy environment for creating CSS Houdini worklets.
npm install houdini-decorative-barcharts!Preview of generated bar charts, in shades of purple, blue, and seafoam with various numbers of bars
> From Stephanie Eckles @5t3ph - author of ModernCSS.dev.
Generate dynamic _decorative/placeholder_ bar charts using CSS Houdini. Intended to be an alternate to SVG icons or less flexible CSS gradient solutions.
The worklet will create randomized bar heights and compute bar widths and gap sizes relative to the number of bars requested vs. the width of the element. Since worklets update when the element is repainted, these values will resize alongside the element. The seed value ensures consistent results across repaints (prevents "flashing" effect on resize).
- Preview on CodePen
- View in a demo application
Important: Do not use these as a substitute for real bar charts because they will not provide any information to non-sighted or keyboard users unless you separately create text alternatives to describe the information. Plus, they are random which means you will not be able to match real data.
While Houdini paint worklets have the best support out of available Houdini features, they still currently require a polyfill to ensure they are applied cross browser.
So, first include the following once in your project.
``html`
> Note: The polyfill will not work if the worklet is applied to pseudo elements.
Then, include the paint worklet script _after_ the polyfill is loaded:
`html`
Finally, use it in your styles! For best results, assign as the background-image to a dedicated element to control the size used for the bar chart.
`css
.barchart-element {
/ Number of bars /
--barchart-number: 5;
/ Chart seed number - ensures chart variance /
--barchart-seed: 22382;
/ Option 1: Provide a CSS color /
--barchart-color: purple;
/ Option 2: Provide gradient start and end colors for a "to bottom" gradient /
--barchart-start-color: hsl(260, 85%, 65%);
--barchart-end-color: hsl(260, 85%, 35%);
/ Use the worklet! /
background: paint(houdiniBarCharts);
}
``
Check out other Houdini paint worklets and more info at Houdini.How.