Styling with CSS Variables
Learn how to use CSS custom properties (CSS variables) to create flexible and dynamic themes for @c15t/react components.
CSS Variables (also known as CSS Custom Properties) provide a powerful way to create dynamic, maintainable themes for @c15t/react components. This approach is particularly useful for dark mode, theming, and responsive design.
Basic Usage
Each @c15t/react component has a set of predefined CSS variables that you can override:
This approach allows you to:
- Apply consistent theming across components
- Create dynamic themes that respond to user preferences
- Implement dark mode with minimal code duplication
Example
Here's a live example of CSS variables in action:
Available CSS Variables
Each component in @c15t/react has its own set of CSS variables. Common patterns include:
--{component}-background-color
: Background color of the component--{component}-text-color
: Text color of the component--{component}-border-radius
: Border radius of the component--{component}-border-color
: Border color of the component--{component}-padding
: Padding of the component
Check each component's reference documentation for a complete list of available CSS variables.
Implementing Dark Mode
CSS Variables are particularly effective for implementing dark mode:
Nested CSS Variables
You can target nested elements with their specific CSS variables:
Responsive Design with CSS Variables
CSS Variables work well with media queries for responsive design:
Creating a Theme System with CSS Variables
You can create a comprehensive theme system using CSS variables:
Best Practices for CSS Variables
-
Naming Convention
- Use a consistent naming pattern:
--component-property-state
- Use kebab-case for variable names
- Use a consistent naming pattern:
-
Fallback Values
- Always provide fallback values for critical styling
- Use the
var()
function's second parameter for fallbacks
-
Scoping
- Scope variables appropriately to avoid leakage
- Consider organizing variables by component or feature
-
Performance
- Minimize the number of CSS variable changes during runtime
- Batch variable updates when possible