How to Style a Streamlit App With Custom CSS
Since Streamlit 1.39, a widget’s key becomes a CSS class: key="green" adds st-key-green to the element. Put your rules in a stylesheet, inject it with st.html at the top of the script, and target .st-key-green button. For app-wide colors and fonts, use the theme in .streamlit/config.toml. Everything below is tested with Streamlit 1.67.
Table of contents
Load a stylesheet
Keep your CSS in its own file and inject it once at the top of the app, before any widget renders. Styling by key needs Streamlit 1.39 or newer, so upgrade first if your version is older.
pip install streamlit --upgrade
The demo has two files: streamlit_app.py and assets/styles.css. This function reads the stylesheet and injects it inside a style tag:
import streamlit as st
import pathlib
# Function to load CSS from the 'assets' folder
def load_css(file_path):
with open(file_path) as f:
st.html(f"<style>{f.read()}</style>")
# Load the external CSS
css_path = pathlib.Path("assets/styles.css")
load_css(css_path)
The code in my GitHub repository uses st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True) instead of st.html. Both inject the stylesheet the same way. The path is relative to the folder you start streamlit run from; see Troubleshooting for a version that works from anywhere.
Style one widget by key
Give the widget a key, and Streamlit adds the class st-key- plus that key to the element’s outer container. The class sits on a wrapper div, so target the actual element inside it, such as button, input or textarea.
st.header("Buttons")
st.button("I'm a green button", key="green")
st.button("I'm a red button", key="red")
.st-key-green button {
background-color: green;
}
.st-key-red button {
background-color: red;
}

Spaces in a key become hyphens: key="my key 1" produces st-key-my-key-1. Underscores and hyphens stay as they are. Short keys without spaces are easiest to read in the CSS.
Hover, focus and animation
Normal CSS pseudo-classes and animations work on keyed widgets. Add :hover for the mouse-over state and :focus for the state after a click: a clicked button keeps the focus, and without a focus rule Streamlit’s own focus colors replace yours.
The demo’s “Click Me!” button, st.button("Click Me!", key="pulse"), pulses, turns darker on hover and keeps that look after a click:
/* Animated button with pulse effect */
.st-key-pulse button {
background-color: #4CAF50;
border-radius: 10px;
padding: 20px 60px;
animation: pulse 2s infinite;
}
.st-key-pulse button p {
font-size: 24px;
font-family: 'Comic Sans MS', sans-serif;
}
/* Hover effect for the button */
.st-key-pulse button:hover {
background-color: #45a049; /* Darker green */
border: none;
}
.st-key-pulse button:hover p {
color: #FFD700; /* Gold color */
}
/* Override the focus states to prevent default red border */
.st-key-pulse button:focus {
background-color: #45a049; /* Keep the same as hover */
border: none;
}
/* Apply the same text color during focus states */
.st-key-pulse button:focus p {
color: #FFD700;
}
@keyframes pulse {
0% {
box-shadow: 0 0 0 0 rgba(76, 175, 80, 0.7);
}
70% {
box-shadow: 0 0 0 20px rgba(76, 175, 80, 0);
}
100% {
box-shadow: 0 0 0 0 rgba(76, 175, 80, 0);
}
}
The button label is a p element inside the button, which is why font and text color rules target button p.

Inputs, text areas and radio buttons
Text inputs and text areas are styled through the input and textarea elements inside the keyed container. Radio button labels are p elements inside the .stRadio block.
st.header("Styled Text Input")
st.text_input("Enter your favorite quote:", key="styledinput")
st.header("Styled Text Area")
st.text_area("Your thoughts:", key="styledtextarea")
st.header("Styled Radio Buttons")
st.radio("Pick a choice:", ["Choice A", "Choice B", "Choice C"], key="styledradio")
/* Text input with custom font and color */
.st-key-styledinput input {
border: 5px solid #3cff1e;
border-radius: 8px;
}
.st-key-styledinput input:focus {
border: none;
}
/* Text area with custom font */
.st-key-styledtextarea textarea {
background-color: #e6e6fa;
border: 4px solid #9370DB;
border-radius: 8px;
font-family: 'Lucida Handwriting', cursive;
color: #4B0082;
}
/* Radio buttons with custom styles */
.st-key-styledradio .stRadio p {
color: #8b2e86;
font-family: 'Comic Sans MS', sans-serif;
font-size: 24px;
}

Segmented control and toggle
A segmented control renders each option as a button with role="radio", and the selected one has aria-checked="true". Streamlit styles the selected and hovered segment with a more specific rule than yours, so that one rule needs !important to win.
st.segmented_control("View", ["Day", "Week", "Month"], default="Week", key="view")
st.toggle("Dark chart", key="darktoggle")
/* Segmented control: all segments */
.st-key-view button {
border-color: #1f6feb;
}
/* Segmented control: the selected segment, also while hovered */
.st-key-view button[aria-checked="true"] {
background-color: #1f6feb !important;
border-color: #1f6feb !important;
color: white !important;
}
/* Toggle: the track is the first div inside the label */
.st-key-darktoggle label:has(input:checked) > div:first-of-type {
background-color: #1f6feb;
}
st.pills uses the same button group markup as the segmented control, so the same pattern works for pills. A toggle is built like a checkbox: its data-testid is stCheckbox, and the hidden input has role="switch". The toggle rule above relies on that inner order of elements, so check it after a Streamlit upgrade.
Containers and cards
st.container also takes a key, and there the class lands directly on the container block. That turns any group of elements into a styled card. Inside it, target Streamlit’s elements by their data-testid, for example a metric.
with st.container(key="kpi"):
st.metric("Revenue", "1,204", "+4%")
with st.container(key="card"):
st.write("A card made from a keyed container.")
/* A metric as a card */
.st-key-kpi [data-testid="stMetric"] {
background-color: #f0f6ff;
border: 1px solid #c8dafc;
border-radius: 10px;
padding: 12px 16px;
}
/* A keyed container as a card */
.st-key-card {
background-color: #fff8e1;
border: 1px solid #ffd54f;
border-radius: 12px;
padding: 16px;
}
Your own HTML
For text no widget covers, write your own HTML with st.html and give it a class. The class needs no key: a normal CSS rule in the same stylesheet styles it.
# HTML with Custom Font and Color
st.header("Styled Markdown")
st.html(
'<p class="custom-markdown">This is <strong>bold text</strong> with a custom font and color.</p>'
)
/* Styled markdown */
.custom-markdown {
font-family: 'Comic Sans MS', cursive, sans-serif;
color: #DA70D6;
font-size: 34px;
}
Selector cheat sheet
Keys are the stable way to style one element. To style every element of a type, use its data-testid attribute. I read these from a Streamlit 1.67 app in Chrome’s developer tools (right-click an element, Inspect).
| Element | Selector |
|---|---|
| One keyed widget | .st-key-yourkey button, input, textarea |
| One keyed container | .st-key-yourkey |
| All buttons | [data-testid="stButton"] button |
| Text input / text area | [data-testid="stTextInput"] input, [data-testid="stTextArea"] textarea |
| Radio group | [data-testid="stRadio"] |
| Segmented control and pills | [data-testid="stButtonGroup"] |
| Checkbox and toggle | [data-testid="stCheckbox"] |
| Metric (label, value, delta) | [data-testid="stMetric"], stMetricLabel, stMetricValue, stMetricDelta |
| Markdown text | [data-testid="stMarkdownContainer"] |
| Expander | [data-testid="stExpander"] details |
| Columns | [data-testid="stHorizontalBlock"], [data-testid="stColumn"] |
| Sidebar | [data-testid="stSidebar"] |
| Main content area | [data-testid="stMainBlockContainer"] |
| Top header bar | [data-testid="stHeader"] |
Two tested examples for the whole app:
/* Light blue sidebar */
[data-testid="stSidebar"] {
background-color: #f0f6ff;
}
/* Wider page with layout="centered" */
[data-testid="stMainBlockContainer"] {
max-width: 1100px;
}
Never target the generated class names such as st-emotion-cache-17hm1wy: they change between Streamlit versions. To hide the header, the menu or the “Made with Streamlit” footer instead of restyling them, see hiding the Streamlit menu and header.
Theme colors in config.toml
For colors and fonts across the whole app, the built-in theme is simpler than CSS and survives upgrades. Create .streamlit/config.toml next to your app and set the colors there; primaryColor also recolors selected segments and primary buttons.
[theme]
base = "light"
primaryColor = "#1f6feb"
backgroundColor = "#ffffff"
secondaryBackgroundColor = "#f0f6ff"
textColor = "#1b1f24"
font = "sans serif"
Restart the app after changing the file. Use the theme for the overall look and keyed CSS for the few elements that need to stand out. The Streamlit theming docs list every option.
Troubleshooting
When a rule has no effect, one of four things is usually wrong: the stylesheet did not load, the Streamlit version has no key classes, the selector misses the element, or Streamlit’s own rule is more specific. Developer tools show which one in a minute.
- FileNotFoundError for styles.css. The relative path depends on where you ran
streamlit run. Build the path from the script’s folder instead:css_path = pathlib.Path(__file__).parent / "assets" / "styles.css". - The CSS appears as text on the page. You used
st.markdownwithoutunsafe_allow_html=True. Usest.htmlor add the argument. - No
st-key-class in the HTML. Your Streamlit is older than 1.39. Check withstreamlit versionand upgrade. - The rule is crossed out in developer tools. A more specific Streamlit rule wins. Make your selector more specific, for example by adding an attribute like
[aria-checked="true"], or add!importantto that one property. - The style changes after a click. That is the focus state. Repeat the rule with
:focus, as in the pulse example. - The rule styles the wrong element. The key class sits on a wrapper. Inspect the widget and target the element inside it.
Resources
- Live demo app (Streamlit Community Cloud may ask you to wake it up)
- Source code on GitHub
- Streamlit theming documentation
