Custom ID, class, and style
Apply a custom id, class, and inline style to the Handsontable container, to target the grid with your own CSS or JavaScript selectors.
Overview
The Handsontable container is a regular DOM element, so you can give it a custom id, class, and inline style. How you set these attributes depends on the framework you use, but two rules apply everywhere:
idis not a configuration option. Handsontable does not read anidfrom the settings object passed to the constructor. The container’sidcomes from the DOM element itself, or from a prop or attribute that the framework wrapper forwards to the container.- Handsontable assigns a random
idwhen the container has none. If the container element has noid, or anidthat starts withht_, Handsontable overwrites it with a generated value in the formht_<random>. To keep a customid, use one that does not start withht_.
Set the id, class, and style
In the vanilla JavaScript build, the container is the DOM element you pass to the Handsontable constructor. Set its id, class, and style on that element before or after initialization.
In the following example, the container receives a custom class and an inline border. Because the element already has the id example1, Handsontable keeps it. An element with no id would instead receive a generated ht_<random> value.
import Handsontable from 'handsontable/base';import { registerAllModules } from 'handsontable/registry';// register Handsontable's modulesregisterAllModules();const container = document.querySelector('#example1');// apply a custom class and inline style to the container elementcontainer.classList.add('inventory-grid');container.style.border = '1px solid #4caf50';const data = [ ['SKU-4821', 'Wireless Mouse', 142, 'Electronics'], ['SKU-0093', 'USB-C Cable', 67, 'Electronics'], ['SKU-1175', 'Desk Lamp', 0, 'Home Office'], ['SKU-3340', 'Notebook', 230, 'Stationery'], ['SKU-7782', 'Standing Desk', 18, 'Furniture'],];new Handsontable(container, { data, colHeaders: ['SKU', 'Product', 'Stock', 'Category'], height: 'auto', autoWrapRow: true, autoWrapCol: true, licenseKey: 'non-commercial-and-evaluation',});import Handsontable from 'handsontable/base';import { registerAllModules } from 'handsontable/registry';
// register Handsontable's modulesregisterAllModules();
const container = document.querySelector('#example1')!;
// apply a custom class and inline style to the container elementcontainer.classList.add('inventory-grid');container.style.border = '1px solid #4caf50';
const data = [ ['SKU-4821', 'Wireless Mouse', 142, 'Electronics'], ['SKU-0093', 'USB-C Cable', 67, 'Electronics'], ['SKU-1175', 'Desk Lamp', 0, 'Home Office'], ['SKU-3340', 'Notebook', 230, 'Stationery'], ['SKU-7782', 'Standing Desk', 18, 'Furniture'],];
new Handsontable(container, { data, colHeaders: ['SKU', 'Product', 'Stock', 'Category'], height: 'auto', autoWrapRow: true, autoWrapCol: true, licenseKey: 'non-commercial-and-evaluation',});Result
The grid container has your custom class and inline style applied, so you can target it with your own CSS or JavaScript selectors. The container’s id is the one you set, or a generated ht_<random> value when none is provided.