|
| 1 | +--- |
| 2 | +title: Colors and borders |
| 3 | +slug: /develop/concepts/configuration/theming-customize-colors-and-borders |
| 4 | +--- |
| 5 | + |
| 6 | +# Customize colors and borders in your Streamlit app |
| 7 | + |
| 8 | +## Color values |
| 9 | + |
| 10 | +For all configuration options that accept a color, you can specify the value with one of the following strings: |
| 11 | + |
| 12 | +- A CSS [`<named-color>`](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color) like `"darkBlue"` or `"maroon"`. |
| 13 | +- A HEX string like `"#483d8b"` or `"#6A5ACD"`. |
| 14 | +- An RGB string like `"rgb(106, 90, 205)"` or `"RGB(70, 130, 180)"`. |
| 15 | +- An HSL string like `"hsl(248, 53%, 58%)"` or `"HSL(147, 50%, 47%)"`. |
| 16 | + |
| 17 | +<Tip> |
| 18 | + |
| 19 | +Although you can specify an alpha value for your colors, this isn't recommended. Streamlit adjusts the alpha value of colors to ensure contextually appropriate shading between background and foreground. |
| 20 | + |
| 21 | +</Tip> |
| 22 | + |
| 23 | +## Default Streamlit colors |
| 24 | + |
| 25 | +Streamlit comes with two preconfigured themes: light and dark. If you don't specify any theme configuration options, Streamlit will attempt to use the preconfigured theme that best matches each user's browser settings. |
| 26 | + |
| 27 | +## Color and border configuration options |
| 28 | + |
| 29 | +Most theme configuration options can be set for your whole app, but you can override some with a different value for the sidebar. For example, your app's primary color (`primaryColor`) is used to highlight interactive elements and show focus. If you set `theme.primaryColor`, this will change the primary color for your whole app. However, if you set `theme.sidebar.primaryColor`, this will override `theme.primaryColor` in the sidebar, allowing you to use two different primary colors. |
| 30 | + |
| 31 | +The following two configuration options can only be applied to the whole app: |
| 32 | + |
| 33 | +- `theme.base` sets the default colors for your app's theme to match one of Streamlit's two default themes (`"light"` or `"dark"`). If any theme configuation option is used and `theme.base` is not set, then Streamlit will use `"light"`. |
| 34 | +- `theme.showSidebarBorder` sets the visibility of the border between the sidebar and the main body of your app. |
| 35 | + |
| 36 | +The following configuration options can be set separately for the sidebar by using the `[theme.sidebar]` table instead of the `[theme]` table in `config.toml`: |
| 37 | + |
| 38 | +- `theme.primaryColor` |
| 39 | +- `theme.backgroundColor` |
| 40 | +- `theme.secondaryBackgroundColor` |
| 41 | +- `theme.textColor` |
| 42 | +- `theme.linkColor` |
| 43 | +- `theme.codeBackgroundColor` |
| 44 | +- `theme.baseRadius` |
| 45 | +- `theme.borderColor` |
| 46 | +- `theme.showWidgetBorder` |
| 47 | + |
| 48 | +For brevity, on the rest of this page, theming configuration options will not include the `theme.` or `theme.sidebar.` prefix. |
| 49 | + |
| 50 | +### `primaryColor` |
| 51 | + |
| 52 | +`primaryColor` defines the accent color most often used throughout your Streamlit |
| 53 | +app. The following features and effects use your primary color: |
| 54 | + |
| 55 | +- Button hover effects |
| 56 | +- Elements in focus |
| 57 | +- Selected elements |
| 58 | + |
| 59 | +<Tip> |
| 60 | + |
| 61 | +When your primary color is used as a background, Streamlit changes the text color to white. For example, this happens for `type="primary"` buttons and for selected items in `st.multiselect`. |
| 62 | + |
| 63 | +For legibility, always choose a primary color that is dark enough to contrast well with white text. |
| 64 | + |
| 65 | +</Tip> |
| 66 | + |
| 67 | +#### Example 1: Primary color |
| 68 | + |
| 69 | +The following configuration example has a `"forestGreen"` primary color. In the sidebar, the configuration overrides the primary color to `"darkGoldenrod"`. If you click inside a widget to give it focus, Streamlit displays a primary-color border around the widget. Additionally, if you hover over the secondary and tertiary buttons, the hover color matches the primary color. |
| 70 | + |
| 71 | +```toml |
| 72 | +[theme] |
| 73 | +base="dark" |
| 74 | +primaryColor="forestGreen" |
| 75 | + |
| 76 | +[theme.sidebar] |
| 77 | +primaryColor="darkGoldrod" |
| 78 | +``` |
| 79 | + |
| 80 | +<Cloud name="doc-theming-color-primarycolor" height="350px" /> |
| 81 | + |
| 82 | +### `backgroundColor`, `secondaryBackgroundColor`, and `codeBackgroundColor` |
| 83 | + |
| 84 | +`backgroundColor` defines the background color of your app. |
| 85 | + |
| 86 | +`secondaryBackgroundColor` is used for contrast in the following places: |
| 87 | + |
| 88 | +- The background of input or selection regions for widgets |
| 89 | +- Headers within elements like `st.dataframe` and `st.help` |
| 90 | +- Code blocks and inline code (if `codeBackgroundColor` is not set) |
| 91 | + |
| 92 | +`codeBackgroundColor` sets the background for code blocks and line code. If `codeBackgroundColor` is not set, Streamlit uses `secondaryBackgroundColor` instead. |
| 93 | + |
| 94 | +<Note> |
| 95 | + |
| 96 | +If you do not define background colors for the sidebar, Streamlit will swap `backgroundColor` and `secondaryBackgroundColor` in the sidebar: |
| 97 | + |
| 98 | +- If `theme.sidebar.backgroundColor` is not defined, Streamlit uses `theme.secondaryBackgroundColor`. |
| 99 | +- If `theme.sidebar.secondaryBackgroundColor` is not defined, Streamlit uses `theme.backgroundColor`. |
| 100 | + |
| 101 | +</Note> |
| 102 | + |
| 103 | +#### Example 2: Background colors |
| 104 | + |
| 105 | +The following configuration example has a `"white"` background, with a lavender-tinted `"ghostWhite"` sidebar background. The secondary color for the whole app is `"lavender"` and the code background color is `"powderBlue"`. The code background color is configured once in `[theme]` and inherited in the sidebar. However, because Streamlit swaps background colors when the sidebar inherits them, the secondary background color is set in both `[theme]` and `[theme.sidebar]`. To see the secondary color used for a hover effect, hover over a dataframe cell or open the multiselect drop-down menu. |
| 106 | + |
| 107 | +```toml |
| 108 | +[theme] |
| 109 | +base="light" |
| 110 | +backgroundColor="white" |
| 111 | +secondaryBackgroundColor="lavender" |
| 112 | +codeBackgroundColor="powderBlue" |
| 113 | + |
| 114 | +[theme.sidebar] |
| 115 | +backgroundColor="ghostWhite" |
| 116 | +secondaryBackgroundColor="lavender" |
| 117 | +``` |
| 118 | + |
| 119 | +<Cloud name="doc-theming-color-backgroundcolor" height="450px" /> |
| 120 | + |
| 121 | +### `textColor` and `linkColor` |
| 122 | + |
| 123 | +You can configure the color of body text and links. |
| 124 | + |
| 125 | +`textColor` sets the default text color for all text in the app except language-highlighting in code blocks, inline code, and links. `linkColor` sets the default font color for all Markdown links in the app. |
| 126 | + |
| 127 | +The following elements are impacted by `textColor`: |
| 128 | + |
| 129 | +- Markdown text, except links |
| 130 | +- Text in code blocks that's not colored otherwise from language highlighting |
| 131 | +- App-chrome and sidebar menu icons |
| 132 | +- Widget labels, icons, option text, and placeholder text |
| 133 | +- Dataframe and table text |
| 134 | +- Non-Markdown links, like `st.page_link`, `st.link_button`, and the navigation menu |
| 135 | + |
| 136 | +As noted previously, Streamlit changes the text color to white when text is displayed against your primary color. |
| 137 | + |
| 138 | +#### Example 3: Text colors |
| 139 | + |
| 140 | +The following configuration example has `"darkGoldenrod"` text and `"darkOrchid"` links on a `"dark"` base. Buttons (including `st.link_button`) use the `"darkGoldenrod"` text color. In the multiselect widget, the placeholder text, drop-down menu, and tooltip all have `"darkGoldenrod"` text. If you hover over the sidebar, the scrollbar and collapse icon (<i style={{ verticalAlign: "-.25em" }} className={{ class: "material-icons-sharp" }}>chevron_left</i>) are `"darkGoldenrod"`. |
| 141 | + |
| 142 | +```toml |
| 143 | +[theme] |
| 144 | +base="dark" |
| 145 | +textColor="darkGoldenrod" |
| 146 | +linkColor="darkOrchid" |
| 147 | +``` |
| 148 | + |
| 149 | +<Cloud name="doc-theming-color-textcolor" height="400px" /> |
| 150 | + |
| 151 | +### `baseRadius` |
| 152 | + |
| 153 | +`baseRadius` defines the radius of borders and backgrounds for the following elements: |
| 154 | + |
| 155 | +- Buttons and input areas on widgets |
| 156 | +- Selected items, including items in `st.multiselect` and the navigation menu |
| 157 | +- Code blocks and inline code |
| 158 | +- Dataframes (exterior) |
| 159 | +- Badges and Markdown-text backgrounds |
| 160 | +- Containers with borders, including expanders, forms, dialogs, popovers, and toasts |
| 161 | +- Tooltips, including tooltips within charts |
| 162 | +- Status and exception message blocks |
| 163 | +- Images, including `st.graphviz` and `st.pyplot`, which display as static images |
| 164 | + |
| 165 | +A few elements are notably not fully affected by `baseRadius`. Interactive charts and videos, which have a more complex underlying HTML, will always have square corners. This includes `st.video`, `st.map`, and `st.pydeck_chart`. Conversely, `st.chat_input` and `st.audio_input` will always be fully rounded. Sub-elements like tooltips are still affected by `baseRadius`. |
| 166 | + |
| 167 | +#### Example 4: Border radius |
| 168 | + |
| 169 | +In the following configuration example, the main body of the app uses a `"full"` (1rem) base radius, and the sidebar uses `"none"` (0rem). To better highlight this difference, the example includes contrasting primary and background colors. |
| 170 | + |
| 171 | +```toml |
| 172 | +[theme] |
| 173 | +base="light" |
| 174 | +primaryColor="slateBlue" |
| 175 | +backgroundColor="mintCream" |
| 176 | +secondaryBackgroundColor="darkSeaGreen" |
| 177 | +baseRadius="full" |
| 178 | + |
| 179 | +[theme.sidebar] |
| 180 | +backgroundColor="aliceBlue" |
| 181 | +secondaryBackgroundColor="skyBlue" |
| 182 | +baseRadius="none" |
| 183 | +``` |
| 184 | + |
| 185 | +<Cloud name="doc-theming-color-baseradius" height="500px" /> |
| 186 | + |
| 187 | +### `borderColor` and `showWidgetBorder` |
| 188 | + |
| 189 | +Streamlit does not display borders for unfocused widgets by default (except for buttons). When a user focuses on a widget, Streamlit displays a border around the input area in your `primaryColor`. When the user removes focus, Streamlit hides the border. |
| 190 | + |
| 191 | +If you set `showWidgetBorder=true`, Streamlit will display widget borders when the widget is not in focus. For those widgets, the border color is set by `borderColor`. If `borderColor` is not set, Streamlit infers a color by adding transparency to your `textColor`. |
| 192 | + |
| 193 | +The following elements have borders that you can modify: |
| 194 | + |
| 195 | +- Containers with borders, including expanders, forms, dialogs, popovers, and toasts |
| 196 | +- The sidebar, including the right edge and the boundary below the navigation menu |
| 197 | +- Dataframes and tables |
| 198 | +- `st.tabs` (bottom border) |
| 199 | +- Buttons, including `st.button`, `st.pills`, and `st.segmented_control` |
| 200 | +- Borders on input regions |
| 201 | + |
| 202 | +#### Example 5: Border color and visibility |
| 203 | + |
| 204 | +The following configuration example uses a `"mediumSlateBlue"` border color throughout the app. In the sidebar, widget borders are shown. In the main body of the app, widget borders are not shown, and there is no border around the multiselect, text, or chat input regions except when they are in focus. However, many other elements, like buttons and dataframes, have always-visible borders. |
| 205 | + |
| 206 | +```toml |
| 207 | +[theme] |
| 208 | +base="dark" |
| 209 | +borderColor="mediumSlateBlue" |
| 210 | +showWidgetBorder=false |
| 211 | + |
| 212 | +[theme.sidebar] |
| 213 | +showWidgetBorder=true |
| 214 | +``` |
| 215 | + |
| 216 | +<Cloud name="doc-theming-color-bordercolor" height="400px" /> |
0 commit comments