Angular Data GridTheming: Customising the grid with CSS

Customising the grid with CSS

Custom CSS Rules

A running grid instance contains thousands of DOM elements, and each of them has class names like ag-header and ag-row. You can target these class names with your own CSS rules, allowing limitless customisation.

If your designer has specified, for example, animated rainbow text on the header, that's achievable:

.ag-header-cell-text {
    background: linear-gradient(90deg, red, orange, yellow, green, blue, indigo, violet, red);
    font-weight: 600;
    font-size: 20px;
    animation: animatedTextGradient 2.5s linear infinite;
    background-clip: text;
    -webkit-text-fill-color: transparent;
    background-size: 200% auto;
}

@keyframes animatedTextGradient {
    to {
        background-position: -200% center;
    }
}

The best way to find the right class name to use in a CSS rule is using the browser's developer tools. You will notice that components often have multiple class names, some more general than others. For example, the Column Drop component is an area into which you can drag columns. There are two kinds - a horizontal one in the row group panel and a vertical one in the columns tool panel. You can use the class name ag-column-drop to target either kind, or ag-column-drop-vertical / ag-column-drop-horizontal to target one only.

Overriding Theme Parameters with Custom Properties

Themes created by the theming API support over a hundred Theme Parameters. The values provided through the theming API are default values, and can be overridden using CSS, meaning that many grids can share the same theme and CSS can be used to create differences between the grids.

The names of the CSS custom properties are prefixed with --ag- to avoid conflicts with other custom properties in your application, and use kebab-case:

body {
    /* override value of backgroundColor, foregroundColor and spacing parameters */
    --ag-background-color: darkred;
    --ag-foreground-color: lightpink;
    --ag-spacing: 4px;
    /* use dark scrollbars */
    --ag-browser-color-scheme: dark;
}

Values for CSS custom properties are inherited by child elements. The above example sets the custom property on the body element, so will affect every grid on the page. You can use different selectors to target individual grids or groups of grids.

Using Your App's Existing Custom Properties

If your app already defines a colour scheme using CSS custom properties and you want to use those values within the grid, you can use var() expressions as parameter values:

body {
    /* use --appMainTextColor as the foreground color for all grids on the page */
    --ag-foreground-color: var(--appMainTextColor);
}

Understanding CSS rule maintenance and breaking changes

With each release of the grid we add features and improve existing ones, and as a result the DOM structure changes with every release - even minor releases. Of course we test and update the CSS rules in our themes to make sure they still work, and this includes ensuring that customisations made via CSS custom property does not break between releases. But if you have written your own CSS rules, you will need to test and update them.

The simpler your CSS rules are, the less likely they are to break between releases. Prefer selectors that target a single class name where possible.

Avoiding Breaking the Grid with CSS Rules

Browsers use the same mechanism - CSS - for controlling how elements work (e.g. scrolling and whether they respond to mouse events), where elements appear, and how elements look. Some of the styles applied by our CSS are essential to how the grid works, and the grid depends on those rules not being overridden. There is nothing that we can do to prevent themes overriding critical rules, so as a theme author you need to be careful not to break the grid. Here's a guide:

  • Visual styles including margins, paddings, sizes, colours, fonts, borders etc are all fine to change in a theme.

  • Setting a component to display: flex and changing flex child layout properties like align-items, align-self and flex-direction is probably OK if you're trying to change how something looks on a small scale, e.g. to change the alignment of some text or icons within a container; but if you're trying to change the layout of the grid on a larger scale e.g. turning a vertical scrolling list into a horizontal one, you are likely to break Grid features.

  • The style properties position, overflow and pointer-events are intrinsic to how the grid works. Changing these values will change how the grid operates, and may break functionality now or in future minor releases.