Documentation
A jQWidgets theme is a stylesheet and a name. The stylesheet jqx.fluent.css styles every widget whose theme is fluent: the widget adds the name to its CSS classes, such as jqx-grid-fluent, and the stylesheet's rules match those classes. On a page you add the stylesheet with a <link> tag and set the theme option of each widget.
This guide does that with jqwidgets-scripts 27.0.1, then switches between a light and a dark theme while the page runs, and follows the dark mode of the operating system. The pages use plain JavaScript and no jQuery: jqxcore.js provides $, so $('#grid').jqxGrid({ … }) works without it. On a page that already loads jQuery 3 before jqxcore.js, jQWidgets uses it, and the code in this guide works unchanged. The setup is described in the introduction. The theme list below is for 27.1.0, the current release; the notes that name 27.0.1 describe that version.
The stylesheets are in jqwidgets/styles/ of the jqwidgets-scripts package: node_modules/jqwidgets-scripts/jqwidgets/styles/ after npm install jqwidgets-scripts, or https://cdn.jsdelivr.net/npm/jqwidgets-scripts@27/jqwidgets/styles/ on the jsDelivr CDN. jqx.base.css holds the layout rules every widget needs and is always loaded first. Each other file is one theme, and the theme's name is the file name without jqx. and .css.
| Kind | Theme names in jqwidgets-scripts 27.1.0 |
|---|---|
| Design systems | fluent, fluent-dark, material, material-dark, material-green, material-green-dark, material-purple, material-purple-dark, tailwind, tailwind-dark, tailwind-blue, tailwind-blue-dark, bootstrap |
| Light and dark pairs | fluent and fluent-dark, material and material-dark, material-green and material-green-dark, material-purple and material-purple-dark, tailwind and tailwind-dark, tailwind-blue and tailwind-blue-dark, light and dark, metro and metrodark, ui-lightness and ui-darkness |
| Other dark themes | black, android, blue, windowsphone, ui-le-frog |
| Other light themes | arctic, blackberry, classic, darkblue, energyblue, flat, fresh, glacier, highcontrast, mobile, office, orange, shinyblack, summer, web, ui-overcast, ui-redmond, ui-smoothness, ui-start, ui-sunny |
| Legacy files | jqx.light-ie.css and jqx.dark-ie.css (theme names light and dark), jqx.fluent_old.css (theme name fluent) |
Version 27.1.0 added fluent-dark, material-dark, material-green-dark and material-purple-dark, and the tailwind-blue pair: Tailwind with the blue of Microsoft 365. Each dark theme has the same sizes and spacing as its light theme, so nothing moves when the page switches between them. Version 27.0.1 has none of them; there, use tailwind-dark for a dark look.
The legacy files are for older pages. jqx.light-ie.css and jqx.dark-ie.css are light and dark written without CSS variables. jqx.fluent_old.css is an earlier version of fluent with the same class names, so load one or the other.
Load the base stylesheet, then the theme, then the scripts. The grid on this page needs these files:
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jqwidgets-scripts@27/jqwidgets/styles/jqx.base.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jqwidgets-scripts@27/jqwidgets/styles/jqx.fluent.css">
<script src="https://cdn.jsdelivr.net/npm/jqwidgets-scripts@27/jqwidgets/jqxcore.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jqwidgets-scripts@27/jqwidgets/jqxdata.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jqwidgets-scripts@27/jqwidgets/jqxbuttons.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jqwidgets-scripts@27/jqwidgets/jqxscrollbar.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jqwidgets-scripts@27/jqwidgets/jqxgrid.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jqwidgets-scripts@27/jqwidgets/jqxgrid.selection.js"></script>
The theme has to come after jqx.base.css: both stylesheets style the same elements, and the later one wins. With the base stylesheet last, the grid keeps some of the theme but its cells change to Verdana 13px with lighter grey borders.
To use the npm package instead of the CDN, replace https://cdn.jsdelivr.net/npm/jqwidgets-scripts@27/ with node_modules/jqwidgets-scripts/ in the same tags.
Every widget has a theme option. Give it the theme's name when you create the widget:
const source = new $.jqx.dataAdapter({
localdata: [
{ name: 'Espresso', category: 'Coffee', price: 2.2, stock: 140 },
{ name: 'Cappuccino', category: 'Coffee', price: 3.1, stock: 85 },
{ name: 'Flat White', category: 'Coffee', price: 3.4, stock: 62 },
{ name: 'Green Tea', category: 'Tea', price: 2.5, stock: 120 },
{ name: 'Chai Latte', category: 'Tea', price: 3.6, stock: 40 },
{ name: 'Hot Chocolate', category: 'Chocolate', price: 3.2, stock: 75 },
{ name: 'Croissant', category: 'Bakery', price: 2.4, stock: 30 },
{ name: 'Blueberry Muffin', category: 'Bakery', price: 2.9, stock: 18 }
],
datatype: 'array',
datafields: [
{ name: 'name', type: 'string' },
{ name: 'category', type: 'string' },
{ name: 'price', type: 'number' },
{ name: 'stock', type: 'number' }
]
});
$('#grid').jqxGrid({
theme: 'fluent',
width: 560,
autoheight: true,
source: source,
columns: [
{ text: 'Product', datafield: 'name' },
{ text: 'Category', datafield: 'category', width: 130 },
{ text: 'Price', datafield: 'price', width: 90, cellsformat: 'c2', cellsalign: 'right', align: 'right' },
{ text: 'In stock', datafield: 'stock', width: 90, cellsalign: 'right', align: 'right' }
]
});
Each widget needs its own theme. A widget without one, or with a name whose stylesheet is not on the page, uses only jqx.base.css: a grey header and Verdana text. $('#grid').jqxGrid('theme') returns the name a widget uses.
To give all widgets the same theme, set $.jqx.theme before you create them:
// Every widget created from here on uses fluent, unless its own options name a theme.
$.jqx.theme = 'fluent';
$('#size').jqxDropDownList({ source: ['Small', 'Medium', 'Large'], selectedIndex: 1, width: 140, height: 32 });
$('#order').jqxButton({ width: 140, height: 32 });
A widget reads $.jqx.theme when it is created, and a theme in its own options wins. Changing $.jqx.theme later affects only the widgets created after the change; to restyle existing widgets, set their theme option as shown next.
To let users change the theme, the page needs two things: the new theme's stylesheet, and the new name in every widget's theme option.
Several themes, such as fluent, tailwind and tailwind-dark, set CSS variables with the same names on :root. If two of them are on the page, the one loaded last decides the colours of both: with jqx.tailwind-dark.css loaded after jqx.fluent.css, a fluent grid turns dark. So a small module adds the new theme's <link>, waits until it has loaded, and then removes the previous one.
theme.js:
// Loads one jQWidgets theme stylesheet at a time.
const STYLES = 'https://cdn.jsdelivr.net/npm/jqwidgets-scripts@27/jqwidgets/styles/';
const DARK_THEMES = ['tailwind-dark', 'dark', 'metrodark', 'ui-darkness', 'black', 'shinyblack', 'darkblue', 'highcontrast'];
// The theme's <link> on the page (the page may start with one, marked id="jqx-theme").
let current = document.getElementById('jqx-theme');
let lastRequest = 0;
// Resolves to true when the theme is on the page, or false when a later call replaced it.
export async function loadTheme(name) {
const request = ++lastRequest;
const link = document.createElement('link');
link.rel = 'stylesheet';
link.href = `${STYLES}jqx.${name}.css`;
await new Promise((resolve, reject) => {
link.onload = resolve;
link.onerror = () => {
link.remove();
reject(new Error(`${link.href} could not be loaded`));
};
// Keep the theme's place: after jqx.base.css, before the page's own stylesheets.
(current ?? document.querySelector('link[href$="jqx.base.css"]')).after(link);
});
if (request !== lastRequest) {
link.remove();
return false;
}
// Themes set CSS variables on :root, so keep only one on the page.
current?.remove();
current = link;
document.body.classList.toggle('dark', DARK_THEMES.includes(name));
return true;
}
The new <link> goes right after the current one, so the theme stays after jqx.base.css and before the page's own stylesheets. If the user picks another theme before the first one has loaded, the later choice wins. If the file cannot be loaded, the promise is rejected and the current theme stays. The dark class on <body> lets the page itself follow the theme, as shown below.
The page starts with fluent. Its <link> is marked id="jqx-theme", so theme.js knows which one to replace, and the page's own stylesheet comes after it:
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jqwidgets-scripts@27/jqwidgets/styles/jqx.base.css">
<link rel="stylesheet" id="jqx-theme" href="https://cdn.jsdelivr.net/npm/jqwidgets-scripts@27/jqwidgets/styles/jqx.fluent.css">
<link rel="stylesheet" href="page.css">
The scripts are those of the grid above, plus jqxlistbox.js and jqxdropdownlist.js for the drop-down list. A native <select> picks the theme:
<label class="toolbar">
Theme
<select id="themePicker">
<option value="fluent" selected>fluent</option>
<option value="tailwind">tailwind</option>
<option value="tailwind-dark">tailwind-dark</option>
</select>
</label>
<div id="grid"></div>
<div class="toolbar">
<div id="size"></div>
<input id="order" type="button" value="Add to order">
<span id="status" role="status"></span>
</div>
The script is a module (<script type="module">), so it can import theme.js. When the stylesheet has loaded, applyTheme sets the theme option of each widget:
import { loadTheme } from './theme.js';
const theme = 'fluent';
const source = new $.jqx.dataAdapter({
localdata: [
{ name: 'Espresso', category: 'Coffee', price: 2.2, stock: 140 },
{ name: 'Cappuccino', category: 'Coffee', price: 3.1, stock: 85 },
{ name: 'Flat White', category: 'Coffee', price: 3.4, stock: 62 },
{ name: 'Green Tea', category: 'Tea', price: 2.5, stock: 120 },
{ name: 'Chai Latte', category: 'Tea', price: 3.6, stock: 40 },
{ name: 'Hot Chocolate', category: 'Chocolate', price: 3.2, stock: 75 },
{ name: 'Croissant', category: 'Bakery', price: 2.4, stock: 30 },
{ name: 'Blueberry Muffin', category: 'Bakery', price: 2.9, stock: 18 }
],
datatype: 'array',
datafields: [
{ name: 'name', type: 'string' },
{ name: 'category', type: 'string' },
{ name: 'price', type: 'number' },
{ name: 'stock', type: 'number' }
]
});
$('#grid').jqxGrid({
theme: theme,
width: 560,
autoheight: true,
source: source,
columns: [
{ text: 'Product', datafield: 'name' },
{ text: 'Category', datafield: 'category', width: 130 },
{ text: 'Price', datafield: 'price', width: 90, cellsformat: 'c2', cellsalign: 'right', align: 'right' },
{ text: 'In stock', datafield: 'stock', width: 90, cellsalign: 'right', align: 'right' }
]
});
$('#size').jqxDropDownList({ theme: theme, source: ['Small', 'Medium', 'Large'], selectedIndex: 1, width: 140, height: 32 });
$('#order').jqxButton({ theme: theme, width: 140, height: 32 });
$('#order').on('click', () => {
const row = $('#grid').jqxGrid('getrowdata', $('#grid').jqxGrid('getselectedrowindex'));
const size = $('#size').jqxDropDownList('getSelectedItem').label;
document.getElementById('status').textContent = row ? `Added: ${row.name}, ${size}` : 'Select a product first.';
});
// Load the stylesheet first, then give every widget the new name.
async function applyTheme(name) {
if (!(await loadTheme(name))) {
return;
}
$('#grid').jqxGrid({ theme: name });
$('#size').jqxDropDownList({ theme: name });
$('#order').jqxButton({ theme: name });
// jqWidgets 27.0.1: the drop-down's list keeps the old theme's offset until it is drawn again.
$('#size').jqxDropDownList('render');
}
document.getElementById('themePicker').addEventListener('change', (event) => {
applyTheme(event.target.value).catch((error) => {
document.getElementById('status').textContent = error.message;
});
});
Setting theme replaces the theme classes of the widget, and the selected row and the drop-down's value stay as they were. The drop-down list also needs render, for the reason explained in When a widget keeps part of its old theme.
A theme styles the widgets, not the page around them. page.css gives the page dark colours when theme.js sets the dark class; color-scheme makes the browser draw native controls, such as the <select>, dark as well:
body {
margin: 24px;
font-family: system-ui, sans-serif;
color-scheme: light;
background: #ffffff;
color: #1f2937;
}
body.dark {
color-scheme: dark;
background: #14171c;
color: #e5e7eb;
}
The prefers-color-scheme media query tells whether the operating system or the browser is set to dark mode. window.matchMedia reads it in JavaScript, and its change event fires when the setting changes while the page is open. This page offers a choice, with the system's setting as the default:
<label class="toolbar">
Appearance
<select id="appearance">
<option value="system" selected>Same as the system</option>
<option value="light">Light</option>
<option value="dark">Dark</option>
</select>
</label>
import { loadTheme } from './theme.js';
const THEMES = { light: 'fluent', dark: 'tailwind-dark' };
const systemDark = window.matchMedia('(prefers-color-scheme: dark)');
const picker = document.getElementById('appearance');
// The picker's choice, where "system" follows the operating system or browser setting.
function chosenTheme() {
if (picker.value === 'system') {
return systemDark.matches ? THEMES.dark : THEMES.light;
}
return THEMES[picker.value];
}
async function applyTheme() {
const name = chosenTheme();
if (!(await loadTheme(name))) {
return;
}
$('#grid').jqxGrid({ theme: name });
}
// Load the first theme before the grid exists, so the grid never shows another one.
const theme = chosenTheme();
await loadTheme(theme);
$('#grid').jqxGrid({
theme: theme,
width: 560,
autoheight: true,
source: new $.jqx.dataAdapter({
localdata: [
{ name: 'Espresso', category: 'Coffee', price: 2.2, stock: 140 },
{ name: 'Green Tea', category: 'Tea', price: 2.5, stock: 120 },
{ name: 'Hot Chocolate', category: 'Chocolate', price: 3.2, stock: 75 },
{ name: 'Croissant', category: 'Bakery', price: 2.4, stock: 30 }
],
datatype: 'array',
datafields: [
{ name: 'name', type: 'string' },
{ name: 'category', type: 'string' },
{ name: 'price', type: 'number' },
{ name: 'stock', type: 'number' }
]
}),
columns: [
{ text: 'Product', datafield: 'name' },
{ text: 'Category', datafield: 'category', width: 130 },
{ text: 'Price', datafield: 'price', width: 90, cellsformat: 'c2', cellsalign: 'right', align: 'right' },
{ text: 'In stock', datafield: 'stock', width: 90, cellsalign: 'right', align: 'right' }
]
});
picker.addEventListener('change', applyTheme);
systemDark.addEventListener('change', applyTheme);
The page has no theme <link> of its own. It loads the first theme before it creates the grid, so the grid never shows the other theme and only one stylesheet is downloaded. After that, both the picker and the system setting call applyTheme. With Light or Dark chosen, the page keeps that theme when the system setting changes.
To try the system setting without changing it, use the Rendering panel of Chrome or Edge DevTools, which can emulate prefers-color-scheme: dark.
Setting theme replaces the theme classes inside the widget's element. Some widgets also keep elements elsewhere on the page, and in jqwidgets-scripts 27.0.1 some of those keep the old theme:
<body>, and the container keeps the old theme's class. The themes position that container, so after a switch the list opens 10 px to the right of the widget.filterable: the condition lists of the column menu's filter stay in the old theme completely. Once the old stylesheet is gone, they open as white, unstyled lists in a dark grid.pageable: the pager's page-size list opens 10 px to the right.Drawing the widget again fixes this. After setting the theme, call the widget's render method; for the pager's list, set the grid's pagesizeoptions again:
async function applyTheme(name) {
if (!(await loadTheme(name))) {
return;
}
$('#grid').jqxGrid({ theme: name });
$('#category').jqxComboBox({ theme: name });
$('#delivery').jqxDateTimeInput({ theme: name });
$('#switch').jqxButton({ theme: name });
// jqWidgets 27.0.1 leaves the pop-up lists of these widgets in the old theme
// until they are drawn again.
$('#category').jqxComboBox('render');
$('#delivery').jqxDateTimeInput('render');
// The grid: render() redraws the lists of the filter menu; setting the page-size
// options again redraws the pager's list, and returns to the first page.
const { pagenum } = $('#grid').jqxGrid('getpaginginformation');
$('#grid').jqxGrid('render');
$('#grid').jqxGrid({ pagesizeoptions: [...$('#grid').jqxGrid('pagesizeoptions')] });
$('#grid').jqxGrid('gotopage', pagenum);
}
render keeps the grid's sorting, filter, selection and page, and the values of the combo box and the date input. Setting pagesizeoptions returns the grid to its first page, so the code saves the page number and goes back to it. jqxDateTimeInput's render draws a new calendar and leaves the previous one hidden in the page, so each switch adds a few elements.
jqxButton, jqxCheckBox, jqxNumberInput, jqxInput, jqxCalendar and jqxTabs take the new theme completely from the option, and so does a jqxGrid without a pager or a filter menu.
theme, or the theme's stylesheet is not on the page. Check that the name matches the file name: jqx.tailwind-dark.css is tailwind-dark.jqx.base.css is loaded after the theme. Put it first.:root are on the page at once; remove the previous theme's <link> when you load the next one.render after setting the theme, as shown in When a widget keeps part of its old theme.$.jqx.theme does not change the page. It applies only to widgets created after it is set. Set the theme option of the existing widgets.file:// address. The browser does not load modules such as theme.js from files. Open the page through a web server; any static server works.More: the introduction, the jQuery Grid tutorials, the grid styling and appearance guide, and the live demos.