My objective is to create charts using chart.js
using colors that match the current chosen Bootstrap 4 theme (there are multiple available for the site)
My current strategy is to have a badge of every color on the page:
<div id="color-example-badge-primary" class="badge badge-primary" ></div > <div id="color-example-badge-warning" class="badge badge-warning" ></div > <div id="color-example-badge-danger" class="badge badge-danger" ></div > <div id="color-example-badge-secondary" class="badge badge-secondary" ></div > <div id="color-example-badge-light" class="badge badge-light" ></div > <div id="color-example-badge-success" class="badge badge-success" ></div > <div id="color-example-badge-info" class="badge badge-info" ></div > <div id="color-example-badge-dark" class="badge badge-dark" ></div >
and then use jQuery to pull the background colors into an array:
var themeColors = [$("#color-example-badge-primary").css('backgroundColor'), $("#color-example-badge-warning").css('backgroundColor'), $("#color-example-badge-danger").css('backgroundColor'), $("#color-example-badge-secondary").css('backgroundColor'), $("#color-example-badge-light").css('backgroundColor'), $("#color-example-badge-success").css('backgroundColor'), $("#color-example-badge-info").css('backgroundColor'), $("#color-example-badge-dark").css('backgroundColor') ];
I can then give this array to chart.js
to create a chart with the theme colors.
Is there a better way to get the current theme colors from the CSS using Javascript? Or is there no way without creating a DOM element and testing it’s background color?
If there is an easy way to get CSS rules using Javascript without dealing with a DOM element, that’d be great.
Advertisement
Answer
Bootstrap stores its themed colors also inside CSS variables on the :root
element.
A more appropriate way would be to extract these colors using getComputedStyle()
and getPropertyValue('--variable')
const style = getComputedStyle(document.body); const theme = { primary: style.getPropertyValue('--primary'), secondary: style.getPropertyValue('--secondary'), success: style.getPropertyValue('--success'), info: style.getPropertyValue('--info'), warning: style.getPropertyValue('--warning'), danger: style.getPropertyValue('--danger'), light: style.getPropertyValue('--light'), dark: style.getPropertyValue('--dark'), }; console.log(theme);
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous"> <div class="container"></div>
Now you can get these use these colors with JavaScript
or jQuery
as follow
element.style.backgroundColor = theme.primary; // Javascript $(element).css('background-color', theme.primary); // jQuery
For bootstrap 5
Bootstrap 5 renamed it properties, to use above code with Bootstrap 5 see example below
const style = getComputedStyle(document.body); const theme = { primary: style.getPropertyValue('--bs-primary'), secondary: style.getPropertyValue('--bs-secondary'), success: style.getPropertyValue('--bs-success'), info: style.getPropertyValue('--bs-info'), warning: style.getPropertyValue('--bs-warning'), danger: style.getPropertyValue('--bs-danger'), light: style.getPropertyValue('--bs-light'), dark: style.getPropertyValue('--bs-dark'), }; console.log(theme);
<!-- JavaScript Bundle with Popper --> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-A3rJD856KowSb7dwlZdYEkO39Gagi7vIsF0jrRAoQmDKKtQBHUuLZ9AsSv4jD4Xa" crossorigin="anonymous"></script> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-gH2yIJqKdNHPEq0n4Mqa/HGKIhSkIHeL5AyhkYV8i59U5AR6csBvApHHNl/vI1Bx" crossorigin="anonymous"> <div class="container"></div>