Starting with version 5, charts can now be styled using CSS rules, making it quick and easy to style charts to a particular look and feel.
This tutorial shows how to take a chart like:
and style it to become:
Let’s go through the 5 steps one by one.
1. Load ‘Styled Mode’ version of Highcharts
Styled mode is a Highcharts version which separates the functionality of the chart from the style. If you load the styled mode version of Highcharts your chart can be styled using CSS rules.
The styled mode version of Highcharts is a separate branch found under the /js directory of the root directory. To include this in your project use something like:
You’ll also need to include a default stylesheet. Without this, the chart will just appear as an unstyled mess!
2. Add a CSS file for style changes
Now we can add a CSS file to contain any style changes we wish to make.
Create a CSS file and make sure to include it in your page:
3. Add new CSS rules
Let’s add some rules to our CSS file!
Start by changing the background colour. The Highcharts documents have a useful guide showing the CSS selectors for each element in the chart. Using this guide we can see that the selector for the background rectangle is
.highcharts-background so let’s go ahead and add a new CSS rule:
We can continue to add rules for each element in the chart. For example we can change the font using:
(Don’t forget to include the Google Font code in the header!)
Other selectors we’ve used to style the chart are:
.highcharts-title (for the main title),
.highcharts-legend-item (for legend items),
.highcharts-axis (for the axes),
.highcharts-axis-labels (for the axis labels),
.highcharts-grid for the background grid and
.highcharts-graph for the lines.
See the CSS files in the codepen to see the exact changes.
4. Add colour palette
Each series in the chart is assigned a specific class name starting
.highcharts-color-0 etc. We’ll select some colours from the colour palette generator Coolors and add some more rules to change the colour of each line:
We’re almost there!
5. Re-arrange elements
We’ll now shift things around a bit to maximise the size of the actual chart and tidy things up.
Let’s start by aligning the title left, the subtitle right and the legend bottom:
and finally we’ll adjust the padding:
That’s it! Have a look at the finished chart in the codepen and see what changes you can make yourself.