# Applying AdapTable Style

Canonical page: https://www.adaptabletools.com/docs/handbook-column-formatting-adaptable-style

- Format Columns can be given an AdapTable Style
- Styles can be either always rendered or when a Condition is met
- An AdapTable Style can set:
  - Colours — text, cell background and border (plus an optional border radius)
  - Font — weight, style, size and decoration
  - Alignment — Left, Right or Center
- Alternatively a supplied CSS Class can be used

Format Columns in AdapTable can be provided with a Style which is used when the column is rendered.

This style can be one of 2 types:

- an **AdapTable Style** object
- a **CSS Style**

Use [Conditional Styling](https://www.adaptabletools.com/docs/handbook-column-formatting-conditions/index.md) when the style should apply only when a particular rule is met

## AdapTable Style

Cells styled using Format Column leverage the [Adaptable Style](https://www.adaptabletools.com/docs/ui-tutorial-providing-adaptable-style/index.md).

This will render all Scoped Cells in the Format Column (or those which meet the Condition, if one is set).

[`AdaptableStyle`](https://www.adaptabletools.com/docs/reference/adaptablestyle.md) is composed of [`CellFontStyle`](https://www.adaptabletools.com/docs/reference/cellfontstyle.md) (text), [`CellBoxStyle`](https://www.adaptabletools.com/docs/reference/cellboxstyle.md) (cell chrome) and an optional `ClassName`:

All properties are **optional** — set only those that should differ from the grid default

The [Adaptable Style](https://www.adaptabletools.com/docs/ui-tutorial-providing-adaptable-style/index.md) is used in a number of places in AdapTable where styling is required

### Colours

You can set the colours for the text, background and (optional) border of the cell. 

```ts
Style: {
  ForeColor: 'var(--ab-color-palette-21)',
  BackColor: '#2966a8',
  BorderColor: '#80cbc4',
}
```

Additionally you can set `BorderRadius` to configure the Border to your requirements.

```ts
Style: {
  BorderRadius: 8,
}
```

See [Colour Properties](https://www.adaptabletools.com/docs/ui-tutorial-providing-adaptable-style/index.md#colour-properties) for hex, name, rgb(a) and theme CSS variables

### Font

The Font in the Cell is fully stylable via the `FontWeight`, `FontStyle`, `FontSize` and `TextDecoration` properties.

`TextDecoration` values are mutually exclusive

```ts
Style: {
  FontWeight: 'Bold',
  FontStyle: 'Italic',
  FontSize: 'Large',
  TextDecoration: 'Underline',
}
```

### Alignment

Cell Alignment also lives in `AdaptableStyle` (i.e. it is not a separate Format Column setting). 

Use `Left`, `Right` or `Center` to set the Alignment.

```ts
Style: {
  Alignment: 'Center',
}
```

Omitting `Alignment`, or setting `Alignment: 'Default'`, leaves the column / grid default in place

**Example: Column Formatting: Column Styles**

Column Formatting using Adaptable Styles

- This example has 3 Format Columns provided which display a Column Style:
  - `Name` is bold, large and underlined
  - `Language` has a blue background, yellow text, a rounded teal border and is centre-aligned
  - all `Number` columns are italic

Demo project (Vanilla); the files without a marker are shown below:

```text
src/
  adaptableOptions.ts
  agGridModules.ts     [template]
  columnDefs.ts        [template]
  gridOptions.ts       [template]
  index.ts             [template]
  onAdaptableReady.ts  [template]
  rowData.ts           [template]
  styles.css           [template]
index.html             [template]
package.json           [template]
tsconfig.json          [template]
```

Files marked `[template]` are the same in every demo; see the [demo template](https://www.adaptabletools.com/llms/demo-template.md#vanilla).

**`src/adaptableOptions.ts`**

```ts
import {AdaptableOptions} from '@adaptabletools/adaptable';
import {WebFramework} from './rowData';

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'Column Formats with Column Style',

  initialState: {
    Dashboard: {
      ModuleButtons: ['FormatColumn'],
    },
    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'Standard Layout',
      Layouts: [
        {
          Name: 'Standard Layout',
          TableColumns: [
            'name',
            'github_stars',
            'language',
            'closed_issues_count',
            'open_pr_count',
            'closed_pr_count',
          ],
        },
      ],
    },
    FormatColumn: {
      FormatColumns: [
        {
          Name: 'formatColumn-name',
          Scope: {
            ColumnIds: ['name'],
          },
          Style: {
            FontWeight: 'Bold',
            FontSize: 'Large',
            TextDecoration: 'Underline',
          },
        },
        {
          Name: 'formatColumn-language',
          Scope: {
            ColumnIds: ['language'],
          },
          Style: {
            BackColor: '#2966a8',
            ForeColor: 'var(--ab-color-palette-21)',
            BorderColor: '#80cbc4',
            BorderRadius: 8,
            Alignment: 'Center',
          },
        },
        {
          Name: 'formatColumn-number',
          Scope: {
            DataTypes: ['number'],
          },
          Style: {
            FontStyle: 'Italic',
          },
        },
      ],
    },
  },
};
```

## CSS Style

It is possible to apply **existing** CSS styles to the cell instead of defining the styles individually.

This is done by specifying the property `ClassName` which references a defined CSS class

See [Setting CSS Style for Adaptable Style](https://www.adaptabletools.com/docs/ui-tutorial-providing-adaptable-style/index.md#css-class-option) for more information

**Example: Column Formatting: CSS Classes**

Format Column with an external CSS Class

- This demo showcases applying external CSS classes to Format Columns:
  - all numeric columns show the `rednumber` CSS class
  - the `Language` and `License` columns show the `bluetext` CSS class

Demo project (Vanilla); the files without a marker are shown below:

```text
src/
  adaptableOptions.ts
  agGridModules.ts     [template]
  columnDefs.ts        [template]
  gridOptions.ts       [template]
  index.ts             [template]
  onAdaptableReady.ts  [template]
  rowData.ts           [template]
  styles.css           [template + shown below]
index.html             [template]
package.json           [template]
tsconfig.json          [template]
```

Files marked `[template]` are the same in every demo; see the [demo template](https://www.adaptabletools.com/llms/demo-template.md#vanilla).

**`src/adaptableOptions.ts`**

```ts
import {AdaptableOptions} from '@adaptabletools/adaptable';
import {WebFramework} from './rowData';

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'Format Column using CSS ClassName',
  userInterfaceOptions: {
    styleClassNames: ['rednumber', 'bluetext'],
  },
  initialState: {
    Theme: {CurrentTheme: 'dark'},
    FormatColumn: {
      FormatColumns: [
        {
          Name: 'formatColumn-number',
          Style: {
            ClassName: 'rednumber',
          },
          Scope: {
            DataTypes: ['number'],
          },
        },
        {
          Name: 'formatColumn-language',
          Style: {
            ClassName: 'bluetext',
          },
          Scope: {
            ColumnIds: ['language', 'license'],
          },
        },
      ],
    },
    Layout: {
      CurrentLayout: 'Standard Layout',
      Layouts: [
        {
          TableColumns: [
            'name',
            'language',
            'github_stars',
            'license',
            'has_projects',
            'has_pages',
            'created_at',
            'has_wiki',
            'updated_at',
            'pushed_at',
            'github_watchers',
            'week_issue_change',
          ],
          Name: 'Standard Layout',
          AutoSizeColumns: true,
        },
      ],
    },
  },
};
```

**`src/styles.css`**

```css
.rednumber {
  text-align: right !important;
  color: red !important;
  font-weight: bolder !important;
}

.bluetext {
  text-align: center !important;
  color: lightblue !important;
  font-style: italic !important;
}
```
