Skip to main content
Gradio gives you several ways to customize the appearance and behavior of your app beyond the built-in themes and components. You can add custom CSS for styling, JavaScript for client-side functionality, and HTML elements for analytics or meta tags.

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:
Gradio includes several prebuilt themes accessible via 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 the css parameter in launch():
The base class for the Gradio app is gradio-container, which you can use to style the entire app.

Loading CSS from files

You can also load CSS from external files using the css_paths parameter:

Referencing external files

If your CSS references external files (like images), prefix the file path with "/gradio_api/file=":
By default, most files on the host machine are not accessible to users. Make sure referenced files (like clouds.jpg) are either URLs or allowed paths.

Using elem_id and elem_classes

Every Gradio component accepts elem_id and elem_classes parameters to help you target specific elements with CSS:
  • elem_id: Adds an HTML id attribute (must be unique)
  • elem_classes: Adds one or more HTML class names (can be a string or list)
The CSS:
  • #warning targets only the second textbox (using its unique ID)
  • .feedback targets both textboxes (using their shared class)
Tip: When targeting classes, you may need to use !important to override Gradio’s default styles.
Using query selectors with Gradio’s internal HTML elements is not guaranteed to work across versions, as the DOM structure may change. Use elem_id and elem_classes for stability.

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 the js parameter:

2. JavaScript event handlers

Run JavaScript when an event is triggered using the js parameter on event listeners:
You can also use JavaScript without a Python function:

3. Adding code to the HTML head

Add scripts, meta tags, or other HTML elements to the page <head> using the head parameter:
The 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:
Accessibility note: Custom JavaScript can affect browser behavior and accessibility. Test your interface across different browsers and be mindful of how scripts may interact with:
  • Screen readers
  • Browser extensions
  • Default keyboard shortcuts
  • Focus management

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: