# Themes

A theme sets the look of a whole table in one call. **Great Tables** has two kinds: the premade themes of [opt_stylize()](../reference/GT.opt_stylize.md#great_tables.GT.opt_stylize), which give a table a colorful look in a single line, and the themes you write yourself with [Theme](../reference/Theme.md#great_tables.Theme), a reusable set of colors, fonts, and [tab_options()](../reference/GT.tab_options.md#great_tables.GT.tab_options) values that you apply with [opt_theme()](../reference/GT.opt_theme.md#great_tables.GT.opt_theme). This page covers both, how to give a theme colors for dark mode, how to build one from a [brand.yml](https://posit-dev.github.io/brand-yml/) file, and which value wins when a theme and other options set the same thing.

Premade themes are ideal for getting a polished look quickly, especially during exploratory work or when building dashboards where visual consistency matters more than pixel-perfect custom styling. For publication or brand-specific tables, or tables that should look right on both light and dark pages, write a [Theme](../reference/Theme.md#great_tables.Theme) of your own.

We'll use the basic GT object below for the examples on this page.

``` python
from great_tables import GT, Theme, data, md

lil_exibble = data.pl.exibble.head(5).select("num", "char", "currency", "row", "group")

gt_ex = (
    GT(lil_exibble, rowname_col="row", groupname_col="group")
    .tab_header(
        title=md("Data listing from exibble"),
        subtitle=md("This is a **Great Tables** dataset."),
    )
    .tab_stubhead(label="row")
    .fmt_number(columns="num")
    .fmt_currency(columns="currency")
    .tab_source_note(source_note="This is only a portion of the dataset.")
)

gt_ex
```

| Data listing from exibble              |          |         |             |
|----------------------------------------|----------|---------|-------------|
| This is a **Great Tables** dataset.    |          |         |             |
| row                                    | num      | char    | currency    |
| grp_a                                  |          |         |             |
| row_1                                  | 0.11     | apricot | \$49.95     |
| row_2                                  | 2.22     | banana  | \$17.95     |
| row_3                                  | 33.33    | coconut | \$1.39      |
| row_4                                  | 444.40   | durian  | \$65,100.00 |
| grp_b                                  |          |         |             |
| row_5                                  | 5,550.00 | None    | \$1,325.81  |
| This is only a portion of the dataset. |          |         |             |

## Premade Themes with `opt_stylize()`

Below are the first two premade styles. The first uses `color="blue"`, and the second uses `color="red"`.

``` python
gt_ex.opt_stylize(style=1, color="blue")
```

| Data listing from exibble              |          |         |             |
|----------------------------------------|----------|---------|-------------|
| This is a **Great Tables** dataset.    |          |         |             |
| row                                    | num      | char    | currency    |
| grp_a                                  |          |         |             |
| row_1                                  | 0.11     | apricot | \$49.95     |
| row_2                                  | 2.22     | banana  | \$17.95     |
| row_3                                  | 33.33    | coconut | \$1.39      |
| row_4                                  | 444.40   | durian  | \$65,100.00 |
| grp_b                                  |          |         |             |
| row_5                                  | 5,550.00 | None    | \$1,325.81  |
| This is only a portion of the dataset. |          |         |             |

``` python
gt_ex.opt_stylize(style=2, color="red")
```

| Data listing from exibble              |          |         |             |
|----------------------------------------|----------|---------|-------------|
| This is a **Great Tables** dataset.    |          |         |             |
| row                                    | num      | char    | currency    |
| grp_a                                  |          |         |             |
| row_1                                  | 0.11     | apricot | \$49.95     |
| row_2                                  | 2.22     | banana  | \$17.95     |
| row_3                                  | 33.33    | coconut | \$1.39      |
| row_4                                  | 444.40   | durian  | \$65,100.00 |
| grp_b                                  |          |         |             |
| row_5                                  | 5,550.00 | None    | \$1,325.81  |
| This is only a portion of the dataset. |          |         |             |

Notice that the first table (blue) emphasizes the row labels with a solid background color. The second table (red) emphasizes the column labels, and uses solid lines to separate the body cell values. There are six colors to choose from: `"blue"` (the default), `"cyan"`, `"pink"`, `"green"`, `"red"`, and `"gray"`. Each is a family of shades, with darker and lighter ones for different parts of the table. Here is the main shade of each.

Colors of opt_stylize()

blue \#0076BA

cyan \#01837B

pink \#CB2A7B

green \#038901

red \#E4220C

gray \#5F5F5F

There are six styles available, each emphasizing different table parts. The `style=` values are numbered from `1` to `6`:

1

Data listing from exibble

This is a Great Tables dataset.

row

num

char

currency

grp_a

row_1

0.11

apricot

\$49.95

row_2

2.22

banana

\$17.95

row_3

33.33

coconut

\$1.39

row_4

444.40

durian

\$65,100.00

grp_b

row_5

5,550.00

None

\$1,325.81

This is only a portion of the dataset.

2

Data listing from exibble

This is a Great Tables dataset.

row

num

char

currency

grp_a

row_1

0.11

apricot

\$49.95

row_2

2.22

banana

\$17.95

row_3

33.33

coconut

\$1.39

row_4

444.40

durian

\$65,100.00

grp_b

row_5

5,550.00

None

\$1,325.81

This is only a portion of the dataset.

3

Data listing from exibble

This is a Great Tables dataset.

row

num

char

currency

grp_a

row_1

0.11

apricot

\$49.95

row_2

2.22

banana

\$17.95

row_3

33.33

coconut

\$1.39

row_4

444.40

durian

\$65,100.00

grp_b

row_5

5,550.00

None

\$1,325.81

This is only a portion of the dataset.

4

Data listing from exibble

This is a Great Tables dataset.

row

num

char

currency

grp_a

row_1

0.11

apricot

\$49.95

row_2

2.22

banana

\$17.95

row_3

33.33

coconut

\$1.39

row_4

444.40

durian

\$65,100.00

grp_b

row_5

5,550.00

None

\$1,325.81

This is only a portion of the dataset.

5

Data listing from exibble

This is a Great Tables dataset.

row

num

char

currency

grp_a

row_1

0.11

apricot

\$49.95

row_2

2.22

banana

\$17.95

row_3

33.33

coconut

\$1.39

row_4

444.40

durian

\$65,100.00

grp_b

row_5

5,550.00

None

\$1,325.81

This is only a portion of the dataset.

6

Data listing from exibble

This is a Great Tables dataset.

row

num

char

currency

grp_a

row_1

0.11

apricot

\$49.95

row_2

2.22

banana

\$17.95

row_3

33.33

coconut

\$1.39

row_4

444.40

durian

\$65,100.00

grp_b

row_5

5,550.00

None

\$1,325.81

This is only a portion of the dataset.

When choosing a style, consider your audience and context. Styles 2, 4, and 5 draw vertical lines between the columns and an outline around the table, giving a gridded look, while styles 1, 3, and 6 leave those out for a more open layout (style 3 separates its rows with dashed lines). Every style but the first puts a solid fill behind the column labels, and the stub is filled with the theme color in styles 1 and 6, with gray in styles 3, 4, and 5, and not at all in style 2. Heavier styling works well for standalone presentations, while lighter styling is better suited for tables embedded in text-heavy reports where the table should complement rather than dominate the page. By default, each style also stripes the body rows (turn this off with `add_row_striping=False`).

[opt_stylize()](../reference/GT.opt_stylize.md#great_tables.GT.opt_stylize) sets its colors with [tab_options()](../reference/GT.tab_options.md#great_tables.GT.tab_options), so a stylized table has the same colors on light and dark pages: it stays light where a default table would turn dark. You can still adjust any part of the look with [tab_options()](../reference/GT.tab_options.md#great_tables.GT.tab_options) or the `opt_*()` shortcuts afterward.

## Writing a Theme

A [Theme](../reference/Theme.md#great_tables.Theme) holds values for a few tokens, plus any option that [tab_options()](../reference/GT.tab_options.md#great_tables.GT.tab_options) accepts. Give only what you want to change. Everything else keeps its built-in value.

| Argument | CSS variable | What it sets |
|----|----|----|
| `background` | `--gt-color-background` | The table background. Sections without a color of their own use it. |
| `foreground` | `--gt-color-foreground` | The text color on that background. |
| `border` | `--gt-color-border` | The lines and borders that are light gray (`#D3D3D3`) by default. |
| `border_strong` | `--gt-color-border-strong` | The top and bottom table borders. |
| `stripe` | `--gt-color-stripe` | The background of striped rows. |
| `accent` | `--gt-color-accent` | For your own CSS. Interactive tables' controls get it as `--gt-i-accent`. |
| `font` | `--gt-font-family` | The font, one name or a list. It goes ahead of the default font stack. |
| `font_size` | `--gt-font-size` | The base font size, as a CSS length or a number of pixels. |

Any other argument is the name of a [tab_options()](../reference/GT.tab_options.md#great_tables.GT.tab_options) option, checked the same way. An unknown name raises an error.

The five color tokens have these built-in values, which a theme's colors replace. A table uses the dark ones on a dark page (see [Colors for Dark Mode](#colors-for-dark-mode)).

Built-in light colors

background \#FFFFFF

foreground \#333333

border \#D3D3D3

border_strong \#A8A8A8

stripe \#F4F4F4

Built-in dark colors

background \#1E1E1E

foreground \#E0E0E0

border \#4A4A4A

border_strong \#6E6E6E

stripe \#2A2A2A

A theme that sets `background` but not `foreground` keeps the built-in text colors, and the table picks the light or the dark one for that background, as it does for `table_background_color=` in [tab_options()](../reference/GT.tab_options.md#great_tables.GT.tab_options).

``` python
compact = Theme(
    border="#9AA5B1",
    font="Georgia",
    data_row_padding="3px",
    column_labels_font_weight="bold",
)

gt_ex.opt_theme(compact)
```

| Data listing from exibble              |          |         |             |
|----------------------------------------|----------|---------|-------------|
| This is a **Great Tables** dataset.    |          |         |             |
| row                                    | num      | char    | currency    |
| grp_a                                  |          |         |             |
| row_1                                  | 0.11     | apricot | \$49.95     |
| row_2                                  | 2.22     | banana  | \$17.95     |
| row_3                                  | 33.33    | coconut | \$1.39      |
| row_4                                  | 444.40   | durian  | \$65,100.00 |
| grp_b                                  |          |         |             |
| row_5                                  | 5,550.00 | None    | \$1,325.81  |
| This is only a portion of the dataset. |          |         |             |

There are no tokens for spacing, since the default padding differs by table part. Set the padding options (`data_row_padding`, `column_labels_padding`, and so on) in the theme instead.

A theme is a value like any other, so you can define it once (in a module of your project, say) and apply it to every table that should share the look. A table has one theme at a time: a second [opt_theme()](../reference/GT.opt_theme.md#great_tables.GT.opt_theme) call with a theme replaces the first. `opt_theme(False)` removes the theme, and leaves any `mode=` as it is.

### Colors for Dark Mode

A table turns dark when the page around it is dark (the next page, [Dark Mode and Web Pages](dark-mode-and-web-pages.md), covers how it decides and how to pin it with `opt_theme(mode=)`). A theme's light color tokens are not used in dark mode: a theme with no colors for dark mode still goes dark, with the built-in dark colors.

`dark=` takes a second [Theme](../reference/Theme.md#great_tables.Theme) with the values to use in dark mode. It can hold the five color tokens (`background`, `foreground`, `border`, `border_strong`, `stripe`) and color options that are written to the table's stylesheet, such as `table_body_hlines_color`. Anything else (a padding or a font, for example) raises an error, because it cannot vary by mode in CSS. A `dark` theme cannot have its own `dark=`.

``` python
navy = Theme(
    border="#9AA5B1",
    font="Georgia",
    dark=Theme(background="#101418", border="#2A3038"),
)

gt_ex.opt_theme(navy)
```

| Data listing from exibble              |          |         |             |
|----------------------------------------|----------|---------|-------------|
| This is a **Great Tables** dataset.    |          |         |             |
| row                                    | num      | char    | currency    |
| grp_a                                  |          |         |             |
| row_1                                  | 0.11     | apricot | \$49.95     |
| row_2                                  | 2.22     | banana  | \$17.95     |
| row_3                                  | 33.33    | coconut | \$1.39      |
| row_4                                  | 444.40   | durian  | \$65,100.00 |
| grp_b                                  |          |         |             |
| row_5                                  | 5,550.00 | None    | \$1,325.81  |
| This is only a portion of the dataset. |          |         |             |

A theme that sets a background option (such as `heading_background_color`) keeps the table light on dark pages, unless its `dark=` gives that option a value of its own. Then the table has a color for each mode, and it follows the page.

## Themes from brand.yml

If your organization describes its colors and fonts in a brand.yml file, [Theme.from_brand()](../reference/Theme.md#great_tables.Theme.from_brand) turns it into a [Theme](../reference/Theme.md#great_tables.Theme). This needs the [brand_yml](https://pypi.org/project/brand-yml/) package, which is installed with `pip install great_tables[brand]`. Without it, [Theme.from_brand()](../reference/Theme.md#great_tables.Theme.from_brand) raises an `ImportError` that names the extra.

Take this `_brand.yml`.

``` yaml
color:
  palette:
    navy: "#001F3F"
  background: "#FDF6E3"
  foreground: "#073642"
  primary: navy
typography:
  fonts:
    - family: Inter
      source: system
  base:
    family: Inter
    size: 14px
```

Pass its path to [Theme.from_brand()](../reference/Theme.md#great_tables.Theme.from_brand) and apply the result like any other theme.

``` python
from great_tables import GT, Theme, data

theme = Theme.from_brand("_brand.yml")

GT(data.pl.exibble).opt_theme(theme)
```

These fields are used:

| brand.yml field | [Theme](../reference/Theme.md#great_tables.Theme) argument |
|----|----|
| `color.background` | `background` |
| `color.foreground` | `foreground` |
| `color.primary` | `accent` |
| `typography.base.family` | `font` |
| `typography.base.size` | `font_size` |

A color that names an entry of `color.palette` becomes that entry's color. A field the file does not have is left out of the theme. The `border`, `border_strong`, and `stripe` tokens are not taken from the brand, so they keep their built-in values. Font files and Google Font imports are not loaded; only the family name is used.

A brand.yml file has one set of colors. For dark mode, pass a second file with `dark=`. Only its `background` and `foreground` are used, as the colors of the theme's `dark=`.

``` python
theme = Theme.from_brand("_brand.yml", dark="_brand-dark.yml")
```

Each argument can also be a `brand_yml.Brand` object. With no argument, [Theme.from_brand()](../reference/Theme.md#great_tables.Theme.from_brand) leaves the search for a file to `brand_yml`. In `brand_yml` 0.2.0 that needs the `BRAND_YML_PATH` environment variable to be set, so pass a path when you can.

## What Wins

When several things set the same option, the first of these that exists is used:

1.  A value set with [tab_options()](../reference/GT.tab_options.md#great_tables.GT.tab_options) or an `opt_*()` method ([opt_stylize()](../reference/GT.opt_stylize.md#great_tables.GT.opt_stylize) included). It applies in both modes (a border color is blended in dark mode, as described in [Dark Mode and Web Pages](dark-mode-and-web-pages.md#how-colors-go-dark)).
2.  In dark mode, the option in the theme's `dark=`.
3.  The option in the theme.
4.  In dark mode, for an option fed by a color token: the token in the theme's `dark=`, then the built-in dark token.
5.  The token in the theme.
6.  The built-in value.

Setting an option to its default value with [tab_options()](../reference/GT.tab_options.md#great_tables.GT.tab_options) still counts as setting it.

Values from [tab_options()](../reference/GT.tab_options.md#great_tables.GT.tab_options) and the `opt_*()` methods are absolute. They are not scaled from the theme. [opt_vertical_padding()](../reference/GT.opt_vertical_padding.md#great_tables.GT.opt_vertical_padding), [opt_horizontal_padding()](../reference/GT.opt_horizontal_padding.md#great_tables.GT.opt_horizontal_padding), and [opt_table_font()](../reference/GT.opt_table_font.md#great_tables.GT.opt_table_font) with `add=True` compute from the built-in values, and what they compute then replaces the theme's value. Below, the theme asks for `3px` of padding, and the `scale=2` in [opt_vertical_padding()](../reference/GT.opt_vertical_padding.md#great_tables.GT.opt_vertical_padding) doubles the built-in `8px`, so the rows get `16px`.

``` python
gt_ex.opt_theme(Theme(data_row_padding="3px")).opt_vertical_padding(scale=2)
```

| Data listing from exibble              |          |         |             |
|----------------------------------------|----------|---------|-------------|
| This is a **Great Tables** dataset.    |          |         |             |
| row                                    | num      | char    | currency    |
| grp_a                                  |          |         |             |
| row_1                                  | 0.11     | apricot | \$49.95     |
| row_2                                  | 2.22     | banana  | \$17.95     |
| row_3                                  | 33.33    | coconut | \$1.39      |
| row_4                                  | 444.40   | durian  | \$65,100.00 |
| grp_b                                  |          |         |             |
| row_5                                  | 5,550.00 | None    | \$1,325.81  |
| This is only a portion of the dataset. |          |         |             |

The order of the calls does not matter. A theme is stored apart from the table's options, so [opt_theme()](../reference/GT.opt_theme.md#great_tables.GT.opt_theme) before or after [tab_options()](../reference/GT.tab_options.md#great_tables.GT.tab_options) gives the same table.

A theme gives your tables a consistent look wherever they are shown. The next page, [Dark Mode and Web Pages](dark-mode-and-web-pages.md), covers how tables follow light and dark pages, how a web page can restyle its tables with CSS variables, and the outputs that have only one mode.
