# Dark Mode and Web Pages

A table follows the light or dark look of the page it is shown in, with no code from you. This page covers how a table decides which to use, how to pin it to one mode, how colors carry over to dark mode, the CSS variables that let a web page restyle the tables on it, and the outputs and pages where a table can't follow the page. To write a theme of your own, with colors for dark mode, see [Themes](themes.md).

The examples use a small piece of [exibble](../reference/data.exibble.md#great_tables.data.exibble).

``` python
from great_tables import GT, data

small = data.pl.exibble.select("num", "char", "currency", "row")

gt_small = GT(small, rowname_col="row").tab_header("A small table")
```

## Automatic Light and Dark

By default a table is in `"auto"` mode. It is light, and it turns dark when the page around it is dark. It reads that from two places:

- The page's own theme, when the page is one of these hosts: JupyterLab and Notebook 7, VS Code notebooks, Quarto, marimo, or a page using Bootstrap 5.3's `data-bs-theme` attribute. When themes are nested, the nearest one wins, so a `data-bs-theme="light"` section of a dark page keeps its table light.
- The page's color scheme, the CSS `color-scheme` property. A page declares `dark` to be dark, or `light dark` to follow the reader's system setting. The pages that Great Tables makes (with [show()](../reference/GT.show.md#great_tables.GT.show) or `as_raw_html(make_page=True)`) declare `light dark`.

A page that says neither stays light, so a default table is never dark on a light page.

You do not need to do anything for this. The table below is a default table, so it changes with the page it is shown in.

``` python
gt_small
```

| A small table |           |            |          |
|---------------|-----------|------------|----------|
|               | num       | char       | currency |
| row_1         | 0.1111    | apricot    | 49.95    |
| row_2         | 2.222     | banana     | 17.95    |
| row_3         | 33.33     | coconut    | 1.39     |
| row_4         | 444.4     | durian     | 65100.0  |
| row_5         | 5550.0    | None       | 1325.81  |
| row_6         | None      | fig        | 13.255   |
| row_7         | 777000.0  | grapefruit | None     |
| row_8         | 8880000.0 | honeydew   | 0.44     |

[opt_theme()](../reference/GT.opt_theme.md#great_tables.GT.opt_theme) takes a `mode=` to pin the table instead:

- `"light"`: always the light colors.
- `"dark"`: always the dark colors.
- `"auto"`: the default.

``` python
gt_small.opt_theme(mode="dark")
```

| A small table |           |            |          |
|---------------|-----------|------------|----------|
|               | num       | char       | currency |
| row_1         | 0.1111    | apricot    | 49.95    |
| row_2         | 2.222     | banana     | 17.95    |
| row_3         | 33.33     | coconut    | 1.39     |
| row_4         | 444.4     | durian     | 65100.0  |
| row_5         | 5550.0    | None       | 1325.81  |
| row_6         | None      | fig        | 13.255   |
| row_7         | 777000.0  | grapefruit | None     |
| row_8         | 8880000.0 | honeydew   | 0.44     |

``` python
gt_small.opt_theme(mode="light")
```

| A small table |           |            |          |
|---------------|-----------|------------|----------|
|               | num       | char       | currency |
| row_1         | 0.1111    | apricot    | 49.95    |
| row_2         | 2.222     | banana     | 17.95    |
| row_3         | 33.33     | coconut    | 1.39     |
| row_4         | 444.4     | durian     | 65100.0  |
| row_5         | 5550.0    | None       | 1325.81  |
| row_6         | None      | fig        | 13.255   |
| row_7         | 777000.0  | grapefruit | None     |
| row_8         | 8880000.0 | honeydew   | 0.44     |

Use `mode="dark"` for a table on a page that is always dark but doesn't say so, and `mode="light"` for a table that should stay light on a dark page.

## How Colors Go Dark

A table with a background color set through [tab_options()](../reference/GT.tab_options.md#great_tables.GT.tab_options) or [opt_stylize()](../reference/GT.opt_stylize.md#great_tables.GT.opt_stylize) stays light in `"auto"` mode. Those colors were picked for a light table, and they apply in both modes, so switching would give pale stripes on a dark table. `mode="dark"` still forces dark. The same goes for a background option set in a [Theme](../reference/Theme.md#great_tables.Theme), unless the theme's `dark=` gives that option a value of its own: then the table has a color for each mode, and it follows the page.

The built-in dark theme supplies a background, text, border, strong border, and stripe color (the [Themes](themes.md#writing-a-theme) page shows them beside the light ones). A theme's light color tokens are not used in dark mode. A theme that sets `background` and has no `dark=` still goes dark with the built-in dark background, and `mode="light"` keeps it light. Spacing, fonts, the accent, and any color option the theme sets directly carry over to dark mode unless `dark=` replaces them. A border or line color that carries over is blended toward the dark text color, since a color chosen for a light table (black lines, say) would barely show on a dark one. The same is done for border colors set with [tab_options()](../reference/GT.tab_options.md#great_tables.GT.tab_options). Give the option a value in the theme's `dark=` to choose the dark color yourself.

## CSS Variables

In HTML, every option that is written to the table's stylesheet is a CSS variable on that table, so the CSS of a web page can restyle the tables on it. This section covers how the variables are named, how page CSS sets them, how text colors work with them, and how they reach a table inside another.

### Variable Names

The name of a variable is `--gt-` followed by the option name, with underscores turned into hyphens. `table_font_names` is the one exception.

| Option                    | CSS variable                   |
|---------------------------|--------------------------------|
| `data_row_padding`        | `--gt-data-row-padding`        |
| `table_body_hlines_color` | `--gt-table-body-hlines-color` |
| `table_font_names`        | `--gt-font-family`             |

The tokens of a [Theme](../reference/Theme.md#great_tables.Theme) have variables too, listed on the [Themes](themes.md#writing-a-theme) page.

The options below are not part of the stylesheet, so they have no variable.

| Kind of option | Options |
|----|----|
| Footnote marks, order, and spec | `footnotes_marks`, `footnotes_order`, `footnotes_spec_ref`, `footnotes_spec_ftr` |
| Source note separator and multiline | `source_notes_sep`, `source_notes_multiline` |
| Container | `container_width`, `container_height`, `container_padding_x`, `container_padding_y`, `container_overflow_x`, `container_overflow_y` |
| Quarto | `quarto_disable_processing`, `quarto_use_bootstrap` |
| Whether a part is included or shown | `table_border_top_include`, `table_border_bottom_include`, `row_striping_include_stub`, `row_striping_include_table_body`, `column_labels_hidden`, `row_group_as_column` |
| Table ID, caption, and added CSS | `table_id`, `table_caption`, `table_additional_css` |
| Interactive HTML | the `ihtml_*` options |

`--gt-color-accent` is not read by any CSS. A static table has no accent, and an interactive table reads `--gt-i-accent`: a theme's `accent` is written to that variable on the interactive table's element, and page CSS can set `--gt-i-accent` itself.

### Setting Variables from Page CSS

A table declares a variable only for a value that was set from Python. Every other value is a fallback in the CSS, so page CSS can set the variables on a parent of the table, and this works for anything the Python code left alone. The page below sets a border color and tighter padding on every table inside `.tight`.

``` python
gt_small
```

| A small table |           |            |          |
|---------------|-----------|------------|----------|
|               | num       | char       | currency |
| row_1         | 0.1111    | apricot    | 49.95    |
| row_2         | 2.222     | banana     | 17.95    |
| row_3         | 33.33     | coconut    | 1.39     |
| row_4         | 444.4     | durian     | 65100.0  |
| row_5         | 5550.0    | None       | 1325.81  |
| row_6         | None      | fig        | 13.255   |
| row_7         | 777000.0  | grapefruit | None     |
| row_8         | 8880000.0 | honeydew   | 0.44     |

A value set from Python wins over the page's CSS, because the table declares it on its own element. In the next table `data_row_padding` is set in Python, so the `2px` from the page CSS is not used, while the border color from the page still is.

``` python
gt_small.tab_options(data_row_padding="10px")
```

| A small table |           |            |          |
|---------------|-----------|------------|----------|
|               | num       | char       | currency |
| row_1         | 0.1111    | apricot    | 49.95    |
| row_2         | 2.222     | banana     | 17.95    |
| row_3         | 33.33     | coconut    | 1.39     |
| row_4         | 444.4     | durian     | 65100.0  |
| row_5         | 5550.0    | None       | 1325.81  |
| row_6         | None      | fig        | 13.255   |
| row_7         | 777000.0  | grapefruit | None     |
| row_8         | 8880000.0 | honeydew   | 0.44     |

The same holds for a theme: its values count as set from Python. A table cannot be restyled from page CSS for any value its theme sets.

### Text Colors

Text colors are not picked again for a background set in page CSS. Python picks the text color of each part of the table for the background that part has in Python, so page CSS that sets a background should set the text color with it. `--gt-color-foreground` is the text color, in both modes, of every part whose background was not set from Python. The page below makes every table inside `.ink` dark.

``` python
gt_small
```

| A small table |           |            |          |
|---------------|-----------|------------|----------|
|               | num       | char       | currency |
| row_1         | 0.1111    | apricot    | 49.95    |
| row_2         | 2.222     | banana     | 17.95    |
| row_3         | 33.33     | coconut    | 1.39     |
| row_4         | 444.4     | durian     | 65100.0  |
| row_5         | 5550.0    | None       | 1325.81  |
| row_6         | None      | fig        | 13.255   |
| row_7         | 777000.0  | grapefruit | None     |
| row_8         | 8880000.0 | honeydew   | 0.44     |

There is no text color variable for a single part of the table. A part whose background is set only in page CSS (with `--gt-heading-background-color`, say) keeps the text color picked for the table's background, so set that background from Python, with [tab_options()](../reference/GT.tab_options.md#great_tables.GT.tab_options), to have its text color picked for it.

### Tables Inside Tables

Custom properties are inherited. A table placed inside another table (for example, one pasted into a cell as raw HTML) reads the variables that Python set on the outer table. The row details of interactive tables are the exception: the outer table's variables are reset around them. That reset also blocks page CSS, so a variable that the outer table sets does not reach a table in its row details from the page either.

## Limits

Light and dark switching has limits in three places: the colors you set on cells, the outputs that aren't live HTML, and the pages and browsers that don't tell a table their theme.

### Colors You Set on Cells

Colors you set on cells are not themed. Literal colors from [tab_style()](../reference/GT.tab_style.md#great_tables.GT.tab_style), [data_color()](../reference/GT.data_color.md#great_tables.GT.data_color), and [opt_stylize()](../reference/GT.opt_stylize.md#great_tables.GT.opt_stylize) are written as you gave them, in both modes. A few cases are adjusted so the text stays readable, as the table below shows.

| What you set | In light mode | In dark mode |
|----|----|----|
| A fill, and no text color | A text color chosen for contrast with the fill | A text color chosen for contrast with the fill |
| A text color, and no fill | Left as written | Blended toward the section's text color |
| A fill and a text color | Left as written | Left as written |
| A fill from [data_color()](../reference/GT.data_color.md#great_tables.GT.data_color) with its defaults | Left as written | Left as written |
| A fill from [data_color()](../reference/GT.data_color.md#great_tables.GT.data_color) with `autocolor_text=False` | The table's text color | A text color chosen for contrast with the fill, so that light fills don't show the table's light text |
| A fill that is translucent or a keyword (`transparent`, `inherit`, and so on) | Left alone | Left alone |
| A border color from [style.borders()](../reference/style.borders.md#great_tables.style.borders) | Left as written | Blended toward the section's text color, as text colors are |

A few more things to know about these colors:

- The contrast for a fill with no text color changes the text in light mode on dark and mid-tone fills alike (red, blue, or green, for example), not only on dark fills. A text color you write with [style.css()](../reference/style.css.md#great_tables.style.css) still wins.
- Email HTML (`inline_css=True`) keeps the text and border colors you set as written, in both modes.
- Forcing dark can lower contrast. In `mode="dark"`, text on a mid-tone fill that you set (the header of an [opt_stylize()](../reference/GT.opt_stylize.md#great_tables.GT.opt_stylize) table, for example) uses the dark theme's text color. It can have lower contrast than in light mode.
- Content in a cell keeps its own colors. Plots from extension packages, raw HTML or SVG, and images inside a cell are not adapted to dark mode.

### Outputs With One Mode

The output of [as_raw_html()](../reference/GT.as_raw_html.md#great_tables.GT.as_raw_html) needs CSS custom properties, so it only renders as intended where the renderer supports them. `as_raw_html(inline_css=True)` writes literal styles instead, for one mode. That email HTML, and images, LaTeX, Word, and Typst output, cannot follow a preference.

| Output | What `mode=` does |
|----|----|
| Email HTML (`inline_css=True`) | `"auto"` gives the light table and `"dark"` the dark one |
| Output made through gridwell ([as_gridwell()](../reference/GT.as_gridwell.md#great_tables.GT.as_gridwell), and [gtsave()](../reference/GT.gtsave.md#great_tables.GT.gtsave) to formats such as SVG or Word), and LaTeX | Ignored: the table is light, even with `mode="dark"`. The theme values those outputs can express, such as border colors, still apply |

Quarto renders use literal styles unless the format is HTML. HTML shown in a Quarto render is written the same way as email HTML when the format is not HTML, and for any format when Quarto does not report it (older Quarto versions).

### Pages and Browsers

- **Iframes and shadow roots do not see the host's theme.** A table in a shadow root follows the color scheme it inherits from the page, and a table in an iframe follows the color scheme of the iframe's own page (light, if that page does not declare one).
- **Other dark pages get a light table.** Colab and Databricks give no theme signal. Sites built with Sphinx or MkDocs may have a dark theme of their own, but the table follows it only if the theme sets `color-scheme`. Add `color-scheme: dark` to the page's dark CSS, or use `mode="dark"` if the page is always dark.
- **Older browsers show the light table.** Switching needs CSS `light-dark()` (in browsers since 2024); in a browser without it, an `"auto"` table is always light.

For the image, document, and email formats a table can be saved to, see [Exporting and Saving](exporting-and-saving.md). The next page, [Nanoplots](nanoplots.md), puts small line, bar, and box plots in cells.
