Styling a theme
Theme integrations can be styled just like any other Astro project, but there are a few conventions that theme authors should follow to create themes that are customizable and compatible with each other.
Virtual modules
Astro Theme Provider uses virtual modules for styling, enabling complete customization for theme users through overrides:
When styling a theme, always use virtual imports so that theme users can override the styles:
Default style module
By default, all styles inside the src/styles
directory will be combined into a virtual module:
Directorypackage
Directorysrc
Directorystyles
- reset.css
- styles.css
- utilities.css
The default style module can be overridden using the imports
option.
This may be useful for cases like importing fonts or controlling the order of styles inside the module. For example:
Custom style modules
Themes have access to a default style module, but authors can create as many style modules as they want using the imports
option.
Creating overridable styles
All themes should be authored with style systems that are easy to customize, compatible with user projects, and compatible with other theme integrations.
Importing styles
Styles should always be imported using virtual modules:
Styles will apply as a cascade, in the order they are imported. Always place virtual style imports below all other imports to avoid issues with theme users overriding styles.
CSS Layers
All styles inside a theme should be authored using CSS layers.
CSS layers allows users to have full control over the order that styles cascade. For example:
Read more about CSS layers here:
CSS variables
Theme authors should make use of CSS variables to enable style themes that are easy to customize and override. For example:
CSS variables should always include a unique prefix to avoid collisions with existing styles.
Read more about creating style systems with CSS variables here:
- Building Design Systems with CSS Variables - Nicolas Pardo
- Building your own Modern CSS Design System - Rocky Kev
- How to create better themes with CSS variables - Michelle Barker
Class names
Theme authors should include classes on elements, even if the element is not being styled. Classes allow users to target elements inside a theme with style changes. For example:
Class names should always include a unique prefix to prevent collisions with existing styles.
Read more about choosing names for classes here:
Style patterns to avoid
Style attributes
Avoid using style attributes as they have a high specificity, making them hard for users override.