Column widths
Configure column widths, using an array or a function. Let your users manually change column widths using Handsontable DataGrid’s interface.
Overview
By default, the column width adjusts to the width of the content. However, if the width of the content is less than 50px, including 1px for borders on the sides, the column width remains constant at 50px. You can pass the column size as a constant, an array, or a function.
The content inside a cell will be wrapped if it doesn’t fit the cell’s width.
An auto-sized column grows to fit its widest value, even when that value is wider than the grid. The text stays on one line, and the grid scrolls horizontally. The size of the container doesn’t change the measured width. To limit how wide a column can grow, see Set a dynamic maximum column width.
Auto-sizing also measures the content of merged cells. To see how a merged cell that spans several columns affects their widths, see Merge cells: Column widths.
Set the column width as a constant
In this example we set the same width of 100px for all columns across the entire grid.
import Handsontable from 'handsontable/base';import { registerAllModules } from 'handsontable/registry';
// Register all Handsontable's modules.registerAllModules();
const container = document.querySelector('#example1');
new Handsontable(container, { data: [ ['H', 'Hydrogen', 1, 1.008, -434.4, 0.00009, 'Gas'], ['He', 'Helium', 2, 4.003, -458.0, 0.00018, 'Gas'], ['Li', 'Lithium', 3, 6.94, 356.9, 0.534, 'Solid'], ['Be', 'Beryllium', 4, 9.012, 2348.6, 1.85, 'Solid'], ['B', 'Boron', 5, 10.81, 3768.8, 2.34, 'Solid'], ], width: '100%', height: 'auto', colHeaders: ['Symbol', 'Name', 'Atomic Number', 'Atomic Mass', 'Melting Point (°F)', 'Density (g/cm³)', 'State'], rowHeaders: true, colWidths: 100, manualColumnResize: true, autoWrapRow: true, autoWrapCol: true, // licenseKey: 'your-license-key', // get a key at https://handsontable.com/pricing/});import Handsontable from 'handsontable/base';import { registerAllModules } from 'handsontable/registry';
// Register all Handsontable's modules.registerAllModules();
const container = document.querySelector('#example1')!;
new Handsontable(container, { data: [ ['H', 'Hydrogen', 1, 1.008, -434.4, 0.00009, 'Gas'], ['He', 'Helium', 2, 4.003, -458.0, 0.00018, 'Gas'], ['Li', 'Lithium', 3, 6.94, 356.9, 0.534, 'Solid'], ['Be', 'Beryllium', 4, 9.012, 2348.6, 1.85, 'Solid'], ['B', 'Boron', 5, 10.81, 3768.8, 2.34, 'Solid'], ], width: '100%', height: 'auto', colHeaders: ['Symbol', 'Name', 'Atomic Number', 'Atomic Mass', 'Melting Point (°F)', 'Density (g/cm³)', 'State'], rowHeaders: true, colWidths: 100, manualColumnResize: true, autoWrapRow: true, autoWrapCol: true, // licenseKey: 'your-license-key', // get a key at https://handsontable.com/pricing/});Set the column width in an array
In this example, each of the ten columns gets its own width, from 70px for the narrowest column to 160px for the widest. Every width is wide enough for the column’s header and data.
import Handsontable from 'handsontable/base';import { registerAllModules } from 'handsontable/registry';
// Register all Handsontable's modules.registerAllModules();
const container = document.querySelector('#example2');
new Handsontable(container, { data: [ ['H', 1, 'Hydrogen', 'Nonmetal', 1.008, -434.4, -423.2, 0.00009, 2.2, 25], ['He', 2, 'Helium', 'Noble gas', 4.003, -458.0, -452.1, 0.00018, undefined, undefined], ['Li', 3, 'Lithium', 'Alkali metal', 6.94, 356.9, 2447.6, 0.534, 0.98, 145], ['Be', 4, 'Beryllium', 'Alkaline earth metal', 9.012, 2348.6, 4478.8, 1.85, 1.57, 105], ['B', 5, 'Boron', 'Metalloid', 10.81, 3768.8, 7100.6, 2.34, 2.04, 85], ], width: '100%', height: 'auto', colHeaders: [ 'Symbol', 'Atomic Number', 'Name', 'Category', 'Atomic Mass', 'Melting Point (°F)', 'Boiling Point (°F)', 'Density (g/cm³)', 'Electronegativity', 'Atomic Radius (pm)', ], columns: [ {}, { type: 'numeric' }, {}, {}, { type: 'numeric' }, { type: 'numeric', numericFormat: { minimumFractionDigits: 1, maximumFractionDigits: 1, useGrouping: false } }, { type: 'numeric', numericFormat: { minimumFractionDigits: 1, maximumFractionDigits: 1, useGrouping: false } }, { type: 'numeric' }, { type: 'numeric' }, { type: 'numeric' }, ], rowHeaders: true, colWidths: [70, 120, 90, 160, 110, 140, 140, 130, 140, 150], manualColumnResize: true, autoWrapRow: true, autoWrapCol: true, // licenseKey: 'your-license-key', // get a key at https://handsontable.com/pricing/});import Handsontable from 'handsontable/base';import { registerAllModules } from 'handsontable/registry';
// Register all Handsontable's modules.registerAllModules();
const container = document.querySelector('#example2')!;
new Handsontable(container, { data: [ ['H', 1, 'Hydrogen', 'Nonmetal', 1.008, -434.4, -423.2, 0.00009, 2.2, 25], ['He', 2, 'Helium', 'Noble gas', 4.003, -458.0, -452.1, 0.00018, undefined, undefined], ['Li', 3, 'Lithium', 'Alkali metal', 6.94, 356.9, 2447.6, 0.534, 0.98, 145], ['Be', 4, 'Beryllium', 'Alkaline earth metal', 9.012, 2348.6, 4478.8, 1.85, 1.57, 105], ['B', 5, 'Boron', 'Metalloid', 10.81, 3768.8, 7100.6, 2.34, 2.04, 85], ], width: '100%', height: 'auto', colHeaders: [ 'Symbol', 'Atomic Number', 'Name', 'Category', 'Atomic Mass', 'Melting Point (°F)', 'Boiling Point (°F)', 'Density (g/cm³)', 'Electronegativity', 'Atomic Radius (pm)', ], columns: [ {}, { type: 'numeric' }, {}, {}, { type: 'numeric' }, { type: 'numeric', numericFormat: { minimumFractionDigits: 1, maximumFractionDigits: 1, useGrouping: false } }, { type: 'numeric', numericFormat: { minimumFractionDigits: 1, maximumFractionDigits: 1, useGrouping: false } }, { type: 'numeric' }, { type: 'numeric' }, { type: 'numeric' }, ], rowHeaders: true, colWidths: [70, 120, 90, 160, 110, 140, 140, 130, 140, 150], manualColumnResize: true, autoWrapRow: true, autoWrapCol: true, // licenseKey: 'your-license-key', // get a key at https://handsontable.com/pricing/});Set the column width using a function
In this example, the size of all columns is set using a function by taking a column index (1, 2 …) and multiplying it by 60px for each consecutive column.
import Handsontable from 'handsontable/base';import { registerAllModules } from 'handsontable/registry';
// Register all Handsontable's modules.registerAllModules();
const container = document.querySelector('#example3');
new Handsontable(container, { data: [ ['H', 1, -434.4, 0.00009, 'Nonmetal'], ['He', 2, -458.0, 0.00018, 'Noble gas'], ['Li', 3, 356.9, 0.534, 'Alkali metal'], ['Be', 4, 2348.6, 1.85, 'Alkaline earth metal'], ['B', 5, 3768.8, 2.34, 'Metalloid'], ], width: '100%', height: 'auto', colHeaders: ['Symbol', 'Atomic Number', 'Melting Point (°F)', 'Density (g/cm³)', 'Category'], columns: [ {}, { type: 'numeric' }, { type: 'numeric', numericFormat: { minimumFractionDigits: 1, maximumFractionDigits: 1, useGrouping: false } }, { type: 'numeric' }, {}, ], rowHeaders: true, colWidths(index) { return (index + 1) * 60; }, manualColumnResize: true, autoWrapRow: true, autoWrapCol: true, // licenseKey: 'your-license-key', // get a key at https://handsontable.com/pricing/});import Handsontable from 'handsontable/base';import { registerAllModules } from 'handsontable/registry';
// Register all Handsontable's modules.registerAllModules();
const container = document.querySelector('#example3')!;
new Handsontable(container, { data: [ ['H', 1, -434.4, 0.00009, 'Nonmetal'], ['He', 2, -458.0, 0.00018, 'Noble gas'], ['Li', 3, 356.9, 0.534, 'Alkali metal'], ['Be', 4, 2348.6, 1.85, 'Alkaline earth metal'], ['B', 5, 3768.8, 2.34, 'Metalloid'], ], width: '100%', height: 'auto', colHeaders: ['Symbol', 'Atomic Number', 'Melting Point (°F)', 'Density (g/cm³)', 'Category'], columns: [ {}, { type: 'numeric' }, { type: 'numeric', numericFormat: { minimumFractionDigits: 1, maximumFractionDigits: 1, useGrouping: false } }, { type: 'numeric' }, {}, ], rowHeaders: true, colWidths(index) { return (index + 1) * 60; }, manualColumnResize: true, autoWrapRow: true, autoWrapCol: true, // licenseKey: 'your-license-key', // get a key at https://handsontable.com/pricing/});Set a dynamic maximum column width
Use the modifyColWidth hook to cap how wide a column can grow based on its content. The hook receives the width that Handsontable already calculated for the column. Return a smaller number to limit it. This pattern is middleware: you modify a value on its way through the width pipeline instead of setting a fixed size up front.
Unlike colWidths, a cap in modifyColWidth does not force every cell onto one width. Short values keep a narrow column on a single line. When content would exceed your threshold, the column shrinks to your cap and the text wraps.
Don’t set the top-level colWidths option at all if you want columns to auto-size: even an array with undefined entries disables the AutoColumnSize plugin for the entire grid, so none of its columns get measured. To fix a width on some columns while letting others auto-size, set width per column in the columns array instead — only the columns you give a width skip auto-sizing.
You can also enforce a minimum width by returning Math.max(width, minWidth) from the same hook.
import Handsontable from 'handsontable/base';import { registerAllModules } from 'handsontable/registry';
// Register all Handsontable's modules.registerAllModules();
const container = document.querySelector('#example7');
new Handsontable(container, { data: [ ['H', 'Nonmetal', 1], ['He', 'Noble gas', 2], ['Li', 'Alkali metal', 3], ['Be', 'Alkaline earth metal', 4], ['B', 'Metalloid', 5], ], width: '100%', height: 'auto', colHeaders: ['Symbol', 'Category', 'No.'], rowHeaders: true, columns: [{ width: 90 }, {}, { width: 60 }], modifyColWidth(width, column) { if (column === 1 && width > 120) { return 100; } }, autoWrapRow: true, autoWrapCol: true, // licenseKey: 'your-license-key', // get a key at https://handsontable.com/pricing/});import Handsontable from 'handsontable/base';import { registerAllModules } from 'handsontable/registry';
// Register all Handsontable's modules.registerAllModules();
const container = document.querySelector('#example7')!;
new Handsontable(container, { data: [ ['H', 'Nonmetal', 1], ['He', 'Noble gas', 2], ['Li', 'Alkali metal', 3], ['Be', 'Alkaline earth metal', 4], ['B', 'Metalloid', 5], ], width: '100%', height: 'auto', colHeaders: ['Symbol', 'Category', 'No.'], rowHeaders: true, columns: [ { width: 90 }, {}, { width: 60 }, ], modifyColWidth(width, column) { if (column === 1 && width > 120) { return 100; } }, autoWrapRow: true, autoWrapCol: true, // licenseKey: 'your-license-key', // get a key at https://handsontable.com/pricing/});Adjust the column width manually
Set the option manualColumnResize to true to allow users to manually resize the column width by dragging the handle between the adjacent column headers. If you double-click on that handle, the width will be instantly adjusted to the size of the longest value in the column. Don’t forget to enable column headers by setting colHeaders to true.
You can adjust the size of one or multiple columns simultaneously, even if the selected columns are not placed next to each other.
Minimum column width
When the column headers render the menu button (the grid shows column headers, with colHeaders or nestedHeaders, and dropdownMenu is enabled), a column can’t get narrower than the button needs. The minimum width is the icon size plus the horizontal cell padding on both sides (--ht-icon-size + 2 × --ht-cell-horizontal-padding), so it depends on your theme:
| Theme | Minimum column width |
|---|---|
| Main | 32px |
| Horizon | 40px |
| Classic | 24px |
A custom theme can declare these values in any unit, such as rem or calc(). Any other grid has no button to protect, so its minimum is 20px, which is also the lowest the minimum can be.
When a column is narrow, the header hides things in this order: the label text, then the sort indicator, and then the menu button. The menu button stays visible at the minimum width. At that width you can’t sort by clicking the header text. You can still sort with the sort() method, or with Enter on the header when navigableHeaders is enabled.
A column resized this way keeps its width, and that width wins over the colWidths option. To hand control back to the option, pass colWidths to updateSettings(). That re-declares the widths and discards the stored ones:
// every column is 100px again, including the columns the user draggedhot.updateSettings({ colWidths: 100 });To resize columns without discarding what the user dragged, leave colWidths out of the call. To replace the stored widths with your own, pass manualColumnResize as an array. You can also clear them yourself with ManualColumnResize#clearManualSize() for one column, or clearManualSizes() for all of them, followed by hot.render().
hot.getPlugin('manualColumnResize').clearManualSizes();hot.render();Try it in the demo below: drag a column header to resize it, then select Clear manual sizes, which runs the snippet above.
import Handsontable from 'handsontable/base';import { registerAllModules } from 'handsontable/registry';
// Register all Handsontable's modules.registerAllModules();
const container = document.querySelector('#example4');
const hot = new Handsontable(container, { data: [ ['Hydrogen', 'H', 1, -434.4, 'Gas'], ['Helium', 'He', 2, -458.0, 'Gas'], ['Lithium', 'Li', 3, 356.9, 'Solid'], ['Beryllium', 'Be', 4, 2348.6, 'Solid'], ['Boron', 'B', 5, 3768.8, 'Solid'], ], width: '100%', height: 'auto', colHeaders: ['Name', 'Symbol', 'Atomic Number', 'Melting Point (°F)', 'State'], rowHeaders: true, colWidths: [200, 100, 100, 150, 100], // initial width of each column manualColumnResize: true, autoWrapRow: true, autoWrapCol: true, // licenseKey: 'your-license-key', // get a key at https://handsontable.com/pricing/});
// Demonstrates ManualColumnResize#clearManualSizes(): drag a column header// to resize it, then click this button to fall back to `colWidths` again.const clearSizesButton = document.querySelector('#example4-clearSizes');clearSizesButton.addEventListener('click', () => { hot.getPlugin('manualColumnResize').clearManualSizes(); hot.render();});import Handsontable from 'handsontable/base';import { registerAllModules } from 'handsontable/registry';
// Register all Handsontable's modules.registerAllModules();
const container = document.querySelector('#example4')!;
const hot = new Handsontable(container, { data: [ ['Hydrogen', 'H', 1, -434.4, 'Gas'], ['Helium', 'He', 2, -458.0, 'Gas'], ['Lithium', 'Li', 3, 356.9, 'Solid'], ['Beryllium', 'Be', 4, 2348.6, 'Solid'], ['Boron', 'B', 5, 3768.8, 'Solid'], ], width: '100%', height: 'auto', colHeaders: ['Name', 'Symbol', 'Atomic Number', 'Melting Point (°F)', 'State'], rowHeaders: true, colWidths: [200, 100, 100, 150, 100], // initial width of each column manualColumnResize: true, autoWrapRow: true, autoWrapCol: true, // licenseKey: 'your-license-key', // get a key at https://handsontable.com/pricing/});
// Demonstrates ManualColumnResize#clearManualSizes(): drag a column header// to resize it, then click this button to fall back to `colWidths` again.const clearSizesButton = document.querySelector('#example4-clearSizes')!;clearSizesButton.addEventListener('click', () => { hot.getPlugin('manualColumnResize').clearManualSizes(); hot.render();});<div id="example4"></div>
<div class="example-controls-container example-controls-below-grid"> <div class="controls"> <button type="button" id="example4-clearSizes">Clear manual sizes</button> </div></div>Column stretching
You can adjust the width of the columns to make them fit the table’s width automatically. The width of a particular column will be calculated based on the size and number of other columns in the grid. This option only makes sense when you have at least one column in your data set and fewer columns than needed to enable the horizontal scrollbar.
Fit all columns equally
This example fits all columns to the container’s width equally by setting the option stretchH: 'all'.
import Handsontable from 'handsontable/base';import { registerAllModules } from 'handsontable/registry';
// Register all Handsontable's modules.registerAllModules();
const container = document.querySelector('#example5');
new Handsontable(container, { data: [ ['H', 'Hydrogen', 'Nonmetal'], ['He', 'Helium', 'Noble gas'], ['Li', 'Lithium', 'Alkali metal'], ['Be', 'Beryllium', 'Alkaline earth metal'], ['B', 'Boron', 'Metalloid'], ], width: '100%', height: 'auto', colHeaders: ['Symbol', 'Name', 'Category'], rowHeaders: true, stretchH: 'all', // 'none' is default contextMenu: true, autoWrapRow: true, autoWrapCol: true, // licenseKey: 'your-license-key', // get a key at https://handsontable.com/pricing/});import Handsontable from 'handsontable/base';import { registerAllModules } from 'handsontable/registry';
// Register all Handsontable's modules.registerAllModules();
const container = document.querySelector('#example5')!;
new Handsontable(container, { data: [ ['H', 'Hydrogen', 'Nonmetal'], ['He', 'Helium', 'Noble gas'], ['Li', 'Lithium', 'Alkali metal'], ['Be', 'Beryllium', 'Alkaline earth metal'], ['B', 'Boron', 'Metalloid'], ], width: '100%', height: 'auto', colHeaders: ['Symbol', 'Name', 'Category'], rowHeaders: true, stretchH: 'all', // 'none' is default contextMenu: true, autoWrapRow: true, autoWrapCol: true, // licenseKey: 'your-license-key', // get a key at https://handsontable.com/pricing/});Stretch only the last column
In this example, the first three columns are set to be 80px wide, and the last column automatically fills the remaining space. This is achieved by setting the option stretchH: 'last'.
import Handsontable from 'handsontable/base';import { registerAllModules } from 'handsontable/registry';
// Register all Handsontable's modules.registerAllModules();
const container = document.querySelector('#example6');
new Handsontable(container, { data: [ ['H', 'Hydrogen', 1, 'Gas'], ['He', 'Helium', 2, 'Gas'], ['Li', 'Lithium', 3, 'Solid'], ['Be', 'Beryllium', 4, 'Solid'], ['B', 'Boron', 5, 'Solid'], ], width: '100%', height: 'auto', colWidths: 80, colHeaders: ['Symbol', 'Name', 'Atomic Number', 'State'], rowHeaders: true, stretchH: 'last', contextMenu: true, autoWrapRow: true, autoWrapCol: true, // licenseKey: 'your-license-key', // get a key at https://handsontable.com/pricing/});import Handsontable from 'handsontable/base';import { registerAllModules } from 'handsontable/registry';
// Register all Handsontable's modules.registerAllModules();
const container = document.querySelector('#example6')!;
new Handsontable(container, { data: [ ['H', 'Hydrogen', 1, 'Gas'], ['He', 'Helium', 2, 'Gas'], ['Li', 'Lithium', 3, 'Solid'], ['Be', 'Beryllium', 4, 'Solid'], ['B', 'Boron', 5, 'Solid'], ], width: '100%', height: 'auto', colWidths: 80, colHeaders: ['Symbol', 'Name', 'Atomic Number', 'State'], rowHeaders: true, stretchH: 'last', contextMenu: true, autoWrapRow: true, autoWrapCol: true, // licenseKey: 'your-license-key', // get a key at https://handsontable.com/pricing/});Column stretching and manual resizing
Column stretching interacts with manualColumnResize in two ways:
- When
stretchHis'all'or'last'together withmanualColumnResize: true, you can still drag to resize a column. Stretching then recalculates, and the other columns adjust so the grid keeps filling the container. If your manual widths add up to more than the container width, stretching stops and the columns keep the sizes you set. - When you pass
manualColumnResizean array of pre-defined widths (for example,manualColumnResize: [80, 100, 120]), the columns with explicit widths are excluded from stretching. Only the remaining columns are stretched to fill the container.
A note about the performance
As mentioned above, the default width of the column is based on the widest value in any cell within the column. You may be wondering how it’s possible for data sets containing hundreds of thousands of records.
This feature is made possible thanks to the AutoColumnSize plugin, which is enabled by default. Internally it divides the data set into smaller sets and renders only some of them to measure their size. The size is then applied to the entire column based on the width of the widest found value.
To increase the performance, you can turn off this feature by defining the fixed size for the specified column or all columns.
Size of the container
Setting the dimensions of the container that holds Handsontable is described in detail on the Grid size page.
Related API reference
Configuration options
Core methods
Hooks
Plugins