Style grid header cells and column groups.
The grid exposes many theme parameters starting header* for customising header appearance:
const myTheme = themeQuartz.withParams({
headerHeight: '30px',
headerTextColor: 'white',
headerBackgroundColor: 'black',
headerCellHoverBackgroundColor: 'rgba(80, 40, 140, 0.66)',
headerCellMovingBackgroundColor: 'rgb(80, 40, 140)',
});For the full list, see the Headers section of the parameters reference.
When the theme parameters are not enough you can use CSS classes, in particular ag-header, ag-header-cell, and ag-header-group-cell:
.ag-header {
font-family: cursive;
}
.ag-header-group-cell {
font-weight: normal;
font-size: 22px;
}
.ag-header-cell {
font-size: 18px;
}import {
GridApi,
GridOptions,
ModuleRegistry,
createGrid,
enableDevValidations,
themeQuartz,
} from "ag-grid-community";
import { AllEnterpriseModule } from "ag-grid-enterprise";
import { IOlympicData } from "./interfaces";
// Enable extended validations only for development
if (process.env.NODE_ENV !== "production") {
enableDevValidations();
}
ModuleRegistry.registerModules([AllEnterpriseModule]);
let gridApi: GridApi<IOlympicData>;
const myTheme = themeQuartz.withParams({
headerHeight: "30px",
headerTextColor: "white",
headerBackgroundColor: "black",
headerCellHoverBackgroundColor: "rgba(80, 40, 140, 0.66)",
headerCellMovingBackgroundColor: "rgb(80, 40, 140)",
});
const gridOptions: GridOptions<IOlympicData> = {
theme: myTheme,
columnDefs: [
{
headerName: "Group 1",
children: [{ field: "athlete", minWidth: 170 }, { field: "age" }],
},
{
headerName: "Group 2",
children: [
{ field: "country" },
{ field: "year" },
{ field: "date" },
{ field: "sport" },
{ field: "gold" },
{ field: "silver" },
{ field: "bronze" },
{ field: "total" },
],
},
],
defaultColDef: {
editable: true,
filter: true,
},
};
const gridDiv = document.querySelector<HTMLElement>("#myGrid")!;
gridApi = createGrid(gridDiv, gridOptions);
fetch("https://www.ag-grid.com/example-assets/olympic-winners.json")
.then((response) => response.json())
.then((data: IOlympicData[]) => gridApi!.setGridOption("rowData", data));
.ag-header {
font-family: cursive;
}
.ag-header-group-cell {
font-weight: normal;
font-size: 22px;
}
.ag-header-cell {
font-size: 18px;
}
<div id="myGrid" style="height: 100%"></div>
export interface IOlympicData {
athlete: string,
age: number,
country: string,
year: number,
date: string,
sport: string,
gold: number,
silver: number,
bronze: number,
total: number
} Header Column Borders and Resize Handles Copy Link
Header Column Borders appear between every column, whereas Resize Handles only appear on resizeable columns (Group 1 in the example below).
const myTheme = themeQuartz.withParams({
headerColumnBorder: { color: 'purple' },
headerColumnBorderHeight: '80%',
headerColumnResizeHandleColor: 'orange',
headerColumnResizeHandleHeight: '25%',
headerColumnResizeHandleWidth: '5px',
});import {
GridApi,
GridOptions,
ModuleRegistry,
createGrid,
enableDevValidations,
themeQuartz,
} from "ag-grid-community";
import { AllEnterpriseModule } from "ag-grid-enterprise";
import { IOlympicData } from "./interfaces";
// Enable extended validations only for development
if (process.env.NODE_ENV !== "production") {
enableDevValidations();
}
ModuleRegistry.registerModules([AllEnterpriseModule]);
const myTheme = themeQuartz.withParams({
headerColumnBorder: { color: "purple" },
headerColumnBorderHeight: "80%",
headerColumnResizeHandleColor: "orange",
headerColumnResizeHandleHeight: "25%",
headerColumnResizeHandleWidth: "5px",
});
let gridApi: GridApi<IOlympicData>;
const gridOptions: GridOptions<IOlympicData> = {
theme: myTheme,
columnDefs: [
{
headerName: "Group 1",
children: [
{ field: "athlete", minWidth: 170, resizable: true },
{ field: "age", resizable: true },
],
resizable: true,
},
{
headerName: "Group 2",
children: [
{ field: "country" },
{ field: "year" },
{ field: "date" },
{ field: "sport" },
{ field: "gold" },
{ field: "silver" },
{ field: "bronze" },
{ field: "total" },
],
},
],
defaultColDef: {
editable: true,
filter: true,
resizable: false,
},
};
const gridDiv = document.querySelector<HTMLElement>("#myGrid")!;
gridApi = createGrid(gridDiv, gridOptions);
fetch("https://www.ag-grid.com/example-assets/olympic-winners.json")
.then((response) => response.json())
.then((data: IOlympicData[]) => gridApi!.setGridOption("rowData", data));
<div id="myGrid" style="height: 100%"></div>
export interface IOlympicData {
athlete: string,
age: number,
country: string,
year: number,
date: string,
sport: string,
gold: number,
silver: number,
bronze: number,
total: number
} Style Header on Filter Copy Link
Each time a Column Filter is applied to a column, the CSS class ag-header-cell-filtered is added to the header. This can be used for adding style to headers that are filtered.
The example below adds some styling to ag-header-cell-filtered, so when you filter a column you will notice the column header change.
import {
AllCommunityModule,
GridApi,
GridOptions,
ModuleRegistry,
createGrid,
enableDevValidations,
} from "ag-grid-community";
import { IOlympicData } from "./interfaces";
// Enable extended validations only for development
if (process.env.NODE_ENV !== "production") {
enableDevValidations();
}
ModuleRegistry.registerModules([AllCommunityModule]);
let gridApi: GridApi<IOlympicData>;
const gridOptions: GridOptions<IOlympicData> = {
columnDefs: [
{ field: "athlete" },
{ field: "age", maxWidth: 120 },
{ field: "country" },
{ field: "year", maxWidth: 120 },
{ field: "sport" },
{ field: "gold" },
{ field: "silver" },
{ field: "bronze" },
{ field: "total" },
],
defaultColDef: {
flex: 1,
minWidth: 150,
filter: true,
},
initialState: {
filter: {
filterModel: {
country: {
filterType: "text",
type: "contains",
filter: "us",
},
},
},
},
};
const gridDiv = document.querySelector<HTMLElement>("#myGrid")!;
gridApi = createGrid(gridDiv, gridOptions);
fetch("https://www.ag-grid.com/example-assets/olympic-winners.json")
.then((response) => response.json())
.then((data: IOlympicData[]) => gridApi!.setGridOption("rowData", data));
.ag-header-cell-filtered {
background-color: #1b6d8544;
}
<div id="myGrid" style="height: 100%"></div>
export interface IOlympicData {
athlete: string,
age: number,
country: string,
year: number,
date: string,
sport: string,
gold: number,
silver: number,
bronze: number,
total: number
} Styling the First and Last Columns Copy Link
You can style all first and last column headers (Grouped, Non-Grouped and Floating Filters) using CSS by targeting the .ag-column-first and .ag-column-last selectors as follows:
.ag-header-group-cell.ag-column-first {
background-color: #2244cc66; /* bluest */
}
.ag-header-cell.ag-column-first {
background-color: #2244cc44; /* bluer */
}
.ag-floating-filter.ag-column-first {
background-color: #2244cc22; /* blue */
}
.ag-header-group-cell.ag-column-last {
background-color: #33cc3366; /* greenest */
}
.ag-header-cell.ag-column-last {
background-color: #33cc3344; /* greener */
}
.ag-floating-filter.ag-column-last {
background-color: #33cc3322; /* green */
}import {
AllCommunityModule,
GridApi,
GridOptions,
ModuleRegistry,
createGrid,
enableDevValidations,
} from "ag-grid-community";
import { IOlympicData } from "./interfaces";
// Enable extended validations only for development
if (process.env.NODE_ENV !== "production") {
enableDevValidations();
}
ModuleRegistry.registerModules([AllCommunityModule]);
let gridApi: GridApi<IOlympicData>;
const gridOptions: GridOptions<IOlympicData> = {
columnDefs: [
{
headerName: "Athlete Details",
children: [
{ field: "athlete" },
{ field: "age", maxWidth: 120 },
{ field: "country" },
],
},
{ field: "year", maxWidth: 100 },
{
headerName: "Sport Details",
children: [
{ field: "total", columnGroupShow: "closed" },
{ field: "gold", columnGroupShow: "open" },
{ field: "silver", columnGroupShow: "open" },
{ field: "bronze", columnGroupShow: "open" },
],
},
],
defaultColDef: {
flex: 1,
minWidth: 150,
filter: true,
floatingFilter: true,
},
};
const gridDiv = document.querySelector<HTMLElement>("#myGrid")!;
gridApi = createGrid(gridDiv, gridOptions);
fetch("https://www.ag-grid.com/example-assets/olympic-winners.json")
.then((response) => response.json())
.then((data: IOlympicData[]) => gridApi!.setGridOption("rowData", data));
.ag-header-group-cell.ag-column-first {
background-color: #2244cc66; /* bluest */
}
.ag-header-cell.ag-column-first {
background-color: #2244cc44; /* bluer */
}
.ag-floating-filter.ag-column-first {
background-color: #2244cc22; /* blue */
}
.ag-header-group-cell.ag-column-last {
background-color: #33cc3366; /* greenest */
}
.ag-header-cell.ag-column-last {
background-color: #33cc3344; /* greener */
}
.ag-floating-filter.ag-column-last {
background-color: #33cc3322; /* green */
}
<div id="myGrid" style="height: 100%"></div>
export interface IOlympicData {
athlete: string,
age: number,
country: string,
year: number,
date: string,
sport: string,
gold: number,
silver: number,
bronze: number,
total: number
}