Skip to content

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 option
className/cells
Context menu
alignment item
Set programmatically, for the whole grid, a column, or individual cellsSet interactively, by the person using the grid
Requires no additional pluginRequires the ContextMenu plugin, with the alignment item enabled
Not tracked by UndoRedoTracked 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.

JavaScript
import Handsontable from 'handsontable/base';
import { registerAllModules } from 'handsontable/registry';
// Register all Handsontable's modules.
registerAllModules();
// generate an array of arrays with dummy data
const data = new Array(100) // number of rows
.fill(null)
.map((_, row) =>
new Array(18) // number of columns
.fill(null)
.map((_, column) => `${row}, ${column}`)
);
const container = document.querySelector('#example1');
new Handsontable(container, {
data,
colWidths: 100,
height: 320,
rowHeaders: true,
colHeaders: true,
contextMenu: 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(row, col, key, val) {
console.log('cell meta changed', row, col, key, val);
},
autoWrapRow: true,
autoWrapCol: true,
});
TypeScript
import Handsontable from 'handsontable/base';
import { registerAllModules } from 'handsontable/registry';
// Register all Handsontable's modules.
registerAllModules();
// generate an array of arrays with dummy data
const data: string[][] = new Array(100) // number of rows
.fill(null)
.map((_, row) =>
new Array(18) // number of columns
.fill(null)
.map((_, column) => `${row}, ${column}`)
);
const container = document.querySelector('#example1')!;
new Handsontable(container, {
data,
colWidths: 100,
height: 320,
rowHeaders: true,
colHeaders: true,
contextMenu: 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(row, col, key, val) {
console.log('cell meta changed', row, col, key, val);
},
autoWrapRow: true,
autoWrapCol: true,
});

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.

Configuration options

Hooks

Plugins