- Contents
- Accessing theme values
- Using CSS custom properties
- Convert theme configuration option names to CSS custom property names
- Computed CSS Custom Properties
- CSS Custom Property arrays
- Directly mapped CSS custom properties
- Practical theming examples
- Basic themed component
- Status message component
- Data table component
- Style isolation
- Isolated styles (default)
- Non-isolated styles
- Responsive design
- Best practices
- Always use theme variables
- Test in different themes
- Use semantic color names
- Respect accessibility
- What's next?
Component theming and styling
Custom components v2 provides seamless integration with Streamlit's theming system, allowing your components to automatically adapt to different themes, including dark and light modes. This integration is achieved through CSS Custom Properties that expose Streamlit's theme values directly to your component styles.
Accessing theme values
Streamlit automatically injects CSS Custom Properties into a wrapper element around your component instance. These properties are derived from the current Streamlit theme and are prefixed with --st- for easy identification. These values respect the viewer's theme selection, so you don't need separate logic to handle light and dark modes.
Using CSS custom properties
Reference Streamlit theme values in your component styles using the var() CSS function. If your component has an HTML element with the class my-component, the following CSS will use the following theme values:
--st-text-colorfortheme.textColor--st-background-colorfortheme.backgroundColor--st-border-colorfortheme.borderColor--st-fontfortheme.font
If your component is mounted in the sidebar, these values will correctly inherit from theme.sidebar.
Convert theme configuration option names to CSS custom property names
In general, for any theme configuration option, use the CSS custom property --st-<option-name> to reference the value. <option-name> is the name of the option in the theme configuration in dash-case, also known as kebab-case.
For example, to reference the primary color (theme.primaryColor), use --st-primary-color. To reference the background color (theme.backgroundColor), use --st-background-color. For a description of all theme configuration options, see the config.toml API reference.
If a theme value is not configured, the CSS Custom Properties will have a valid value inherited from the current base theme.
Computed CSS Custom Properties
There are a few computed CSS Custom Properties that don't come directly from a theme configuration option. The following CSS Custom Properties are computed:
| CSS Custom Property | Used for |
|---|---|
--st-heading-color | Heading font color (placeholder); same as text color |
--st-border-color-light | Lighter border color for stale or deactivated elements |
--st-widget-border-color | Widget borders (when theme.showWidgetBorder is true) |
CSS Custom Property arrays
Some theme properties are arrays. These are exposed as comma-separated strings. You can parse these in JavaScript if needed for dynamic styling.
| CSS Custom Property | Used for |
|---|---|
--st-heading-font-sizes | theme.headingFontSizes |
--st-heading-font-weights | theme.headingFontWeights |
--st-chart-categorical-colors | theme.chartCategoricalColors |
--st-chart-sequential-colors | theme.chartSequentialColors |
--st-chart-diverging-colors | theme.chartDivergingColors |
Heading font sizes and weights are also available as individual CSS Custom Properties for each heading level (1–6), so you don't need to parse the arrays when styling specific headings:
| CSS Custom Property | Used for |
|---|---|
--st-heading-font-size-1 | theme.headingFontSizes[0] |
--st-heading-font-size-2 | theme.headingFontSizes[1] |
--st-heading-font-size-3 | theme.headingFontSizes[2] |
--st-heading-font-size-4 | theme.headingFontSizes[3] |
--st-heading-font-size-5 | theme.headingFontSizes[4] |
--st-heading-font-size-6 | theme.headingFontSizes[5] |
--st-heading-font-weight-1 | theme.headingFontWeights[0] |
--st-heading-font-weight-2 | theme.headingFontWeights[1] |
--st-heading-font-weight-3 | theme.headingFontWeights[2] |
--st-heading-font-weight-4 | theme.headingFontWeights[3] |
--st-heading-font-weight-5 | theme.headingFontWeights[4] |
--st-heading-font-weight-6 | theme.headingFontWeights[5] |
Directly mapped CSS custom properties
The rest of the CSS Custom Properties are directly mapped to theme configuration options and are usable without parsing or modification:
| CSS Custom Property | config.toml theme option |
|---|---|
--st-primary-color | theme.primaryColor |
--st-background-color | theme.backgroundColor |
--st-secondary-background-color | theme.secondaryBackgroundColor |
--st-text-color | theme.textColor |
--st-link-color | theme.linkColor |
--st-link-underline | theme.linkUnderline |
--st-heading-font | theme.headingFont |
--st-code-font | theme.codeFont |
--st-base-radius | theme.baseRadius |
--st-button-radius | theme.buttonRadius |
--st-base-font-size | theme.baseFontSize |
--st-base-font-weight | theme.baseFontWeight |
--st-code-font-weight | theme.codeFontWeight |
--st-code-font-size | theme.codeFontSize |
--st-code-text-color | theme.codeTextColor |
--st-border-color | theme.borderColor |
--st-dataframe-border-color | theme.dataframeBorderColor |
--st-dataframe-header-background-color | theme.dataframeHeaderBackgroundColor |
--st-code-background-color | theme.codeBackgroundColor |
--st-font | theme.font |
--st-red-color | theme.redColor |
--st-orange-color | theme.orangeColor |
--st-yellow-color | theme.yellowColor |
--st-blue-color | theme.blueColor |
--st-green-color | theme.greenColor |
--st-violet-color | theme.violetColor |
--st-gray-color | theme.grayColor |
--st-red-background-color | theme.redBackgroundColor |
--st-orange-background-color | theme.orangeBackgroundColor |
--st-yellow-background-color | theme.yellowBackgroundColor |
--st-blue-background-color | theme.blueBackgroundColor |
--st-green-background-color | theme.greenBackgroundColor |
--st-violet-background-color | theme.violetBackgroundColor |
--st-gray-background-color | theme.grayBackgroundColor |
--st-red-text-color | theme.redTextColor |
--st-orange-text-color | theme.orangeTextColor |
--st-yellow-text-color | theme.yellowTextColor |
--st-blue-text-color | theme.blueTextColor |
--st-green-text-color | theme.greenTextColor |
--st-violet-text-color | theme.violetTextColor |
--st-gray-text-color | theme.grayTextColor |
--st-metric-value-font-size | theme.metricValueFontSize |
--st-metric-value-font-weight | theme.metricValueFontWeight |
Practical theming examples
Basic themed component
Here's a simple component that uses Streamlit's theming. Instead of using pixels for spacing, the component uses rem values. This ensures that the component will adjust to different font sizes. The font family and size are set on the parent container so they can be inherited by other elements. Execeptions like headers are styled in later lines. In genral, set colors, borders, border radii, and fonts from CSS Custom Properties.
Status message component
The following example demonstrates using Streamlit's basic color palette to set semantic colors. This is a component that creates color-coded alert banners:
Data table component
You can use CSS Custom Properties to style a data table to match Streamlit's dataframe styling.
Style isolation
Custom components v2 provides style isolation options to control how your component styles interact with the rest of the page.
Isolated styles (default)
By default, Streamlit sets isolate_styles=True when registering a component, which wraps the instance in a Shadow DOM:
Benefits of isolation:
- Component styles won't leak to the rest of the page.
- Page styles won't interfere with your component.
- Safer for third-party components.
Non-isolated styles
If you want your component's style to affect the rest of the page, you can set isolate_styles=False when mounting. This is uncommon.
Responsive design
Create components that work well across different screen sizes. This makes your component more accessible and compatible with the Streamlit layout system. The following example uses @media (max-width: 768px) to create a responsive grid layout that adapts when the screen width is less than 768px.
Best practices
Always use theme variables
Instead of hardcoding colors, always use Streamlit's theme variables:
Test in different themes
Always test your components in both light and dark base themes. Preferably, test your component with a custom theme as well, especially using different font sizes.
Use semantic color names
Choose colors from the basic color palette based on their semantic meaning. Each color in the basic color palette has a text and background variation, in addition to its base color.
Respect accessibility
Streamlit's theme colors are designed with accessibility in mind. Maintain proper contrast ratios when creating custom color combinations.
What's next?
Now that you understand theming and styling:
- Explore Package-based components for advanced development workflows.
- Learn about State vs triggers for interactive components.
- Check out the Quickstart examples for more examples.
Still have questions?
Our forums are full of helpful information and Streamlit experts.
