Text alignment
Align values within cells: horizontally (to the right, left, center, or by justifying them), and vertically (to the top, middle, or bottom of the cell).
Apply text alignment to cells using CSS class names or the className configuration option.
Overview
You can set cell alignment in two ways:
Configuration optionclassName/cells | Context menualignment item |
|---|---|
| Set programmatically, for the whole grid, a column, or individual cells | Set interactively, by the person using the grid |
| Requires no additional plugin | Requires the ContextMenu plugin, with the alignment item enabled |
Not tracked by UndoRedo | Tracked by UndoRedo |
To align a cell
To set alignment for individual cells, configure them using the cells option or the cell array. Available class names:
- Horizontal:
htLeft,htCenter,htRight,htJustify - Vertical:
htTop,htMiddle,htBottom
You can track alignment changes by using the afterSetCellMeta hook.
To align a column
To apply alignment globally or per column, provide the alignment details in the className option, for example:
className="htCenter"Align cells using the context menu
Let the person using the grid set alignment interactively, through the context menu.
Enable the ContextMenu plugin and include the alignment item:
contextMenu: ['alignment'],Select one or more cells, right-click to open the context menu, and choose an option from the Align submenu:
- Horizontal: Left, Center, Right, Justify
- Vertical: Top, Middle, Bottom
Each option sets the matching CSS class name (htLeft, htCenter, htRight, htJustify, htTop, htMiddle, htBottom) on the selected cells, and fires the beforeCellAlignment hook before applying the change.
Undo and redo alignment changes
Alignment changes made through the context menu are tracked by the UndoRedo plugin. Press Ctrl/⌘+Z to undo an alignment change, and Ctrl/⌘+Y to redo it.
Alignment changes made through the className or cells configuration options aren’t tracked by UndoRedo.
Basic example
The following code sample configures the grid to use htCenter and configures individual cells to use different alignments.
import { HotTable } from '@handsontable/react-wrapper';import { registerAllModules } from 'handsontable/registry';
// register Handsontable's modulesregisterAllModules();
// generate an array of arrays with dummy dataconst data = new Array(100) // number of rows .fill(null) .map((_, row) => new Array(18) // number of columns .fill(null) .map((_, column) => `${row}, ${column}`) );
const ExampleComponent = () => { return ( <HotTable data={data} colWidths={100} height={320} rowHeaders={true} colHeaders={true} contextMenu={true} autoWrapRow={true} autoWrapCol={true} licenseKey="non-commercial-and-evaluation" mergeCells={[ { row: 1, col: 1, rowspan: 3, colspan: 3 }, { row: 3, col: 4, rowspan: 2, colspan: 2 }, ]} className="htCenter" cell={[ { row: 0, col: 0, className: 'htRight' }, { row: 1, col: 1, className: 'htLeft htMiddle' }, { row: 3, col: 4, className: 'htLeft htBottom' }, ]} afterSetCellMeta={function (row, col, key, val) { console.log('cell meta changed', row, col, key, val); }} /> );};
export default ExampleComponent;import { HotTable } from '@handsontable/react-wrapper';import { registerAllModules } from 'handsontable/registry';
// register Handsontable's modulesregisterAllModules();
// generate an array of arrays with dummy dataconst data = new Array(100) // number of rows .fill(null) .map((_, row) => new Array(18) // number of columns .fill(null) .map((_, column) => `${row}, ${column}`) );
const ExampleComponent = () => { return ( <HotTable data={data} colWidths={100} height={320} rowHeaders={true} colHeaders={true} contextMenu={true} autoWrapRow={true} autoWrapCol={true} licenseKey="non-commercial-and-evaluation" mergeCells={[ { row: 1, col: 1, rowspan: 3, colspan: 3 }, { row: 3, col: 4, rowspan: 2, colspan: 2 }, ]} className="htCenter" cell={[ { row: 0, col: 0, className: 'htRight' }, { row: 1, col: 1, className: 'htLeft htMiddle' }, { row: 3, col: 4, className: 'htLeft htBottom' }, ]} afterSetCellMeta={function (row, col, key, val) { console.log('cell meta changed', row, col, key, val); }} /> );};
export default ExampleComponent;Result
Cells display the configured horizontal or vertical alignment. Global settings apply to all cells, and per-cell settings take precedence over the global defaults.
Related API reference
Configuration options
Hooks
Plugins