Adding custom CSS
Gradio themes are the easiest way to customize your app’s look. You can choose from built-in themes or create your own:gr.themes.*. You can extend these or create entirely new themes.
Learn more about themes in the Theming guide.
Adding custom CSS strings
For additional styling, pass CSS as a string to thecss parameter in launch():
gradio-container, which you can use to style the entire app.
Loading CSS from files
You can also load CSS from external files using thecss_paths parameter:
Referencing external files
If your CSS references external files (like images), prefix the file path with"/gradio_api/file=":
Using elem_id and elem_classes
Every Gradio component acceptselem_id and elem_classes parameters to help you target specific elements with CSS:
elem_id: Adds an HTMLidattribute (must be unique)elem_classes: Adds one or more HTML class names (can be a string or list)
#warningtargets only the second textbox (using its unique ID).feedbacktargets both textboxes (using their shared class)
Tip: When targeting classes, you may need to use
!important to override Gradio’s default styles.Adding custom JavaScript
There are three ways to add JavaScript to your Gradio app:1. JavaScript on page load
Run JavaScript when the demo first loads using thejs parameter:
2. JavaScript event handlers
Run JavaScript when an event is triggered using thejs parameter on event listeners:
3. Adding code to the HTML head
Add scripts, meta tags, or other HTML elements to the page<head> using the head parameter:
head parameter accepts any HTML tags you would normally put in the <head> section.
Adding meta tags for social sharing
You can customize how your app appears when shared on social media:Example: Custom keyboard shortcuts
Here’s a complete example that adds keyboard shortcuts to your app:Best practices
1
Start with themes
Use Gradio’s theming system before writing custom CSS. Themes are maintainable and work across Gradio versions.
2
Use elem_id and elem_classes
Target specific components with
elem_id and elem_classes instead of relying on Gradio’s internal class names.3
Test across browsers
Custom CSS and JavaScript may behave differently in different browsers. Test thoroughly.
4
Avoid breaking changes
Gradio’s internal DOM structure may change between versions. Use stable APIs like
elem_id and avoid deeply nested selectors.5
Validate file paths
When referencing external files, ensure they’re in allowed paths or are public URLs.
Next steps
Now that you know how to customize your app’s appearance and behavior:- Learn about theming for systematic styling
- Build custom HTML components for reusable UI elements
- Explore controlling layout for structural customization