Styling with CSS Classes
Learn how to customize @c15t/react components using CSS class names and class-based styling.
CSS classes provide a powerful and flexible way to style @c15t/react components. This approach works well with any CSS methodology, including custom CSS, utility classes, or CSS modules.
Basic Class-Based Styling
The simplest way to style a component is by providing class names directly to theme keys:
This approach allows you to:
- Use your own custom CSS classes
- Apply multiple classes to the same element
- Create reusable styling patterns
Object Syntax for Classes
You can also use the object syntax with the className
property:
This syntax is especially useful when you need to combine class names with inline styles:
Combining Class Names
You can provide multiple class names as a space-separated string:
Using CSS Modules
CSS Modules work perfectly with our theming system:
Dynamic Class Names
You can use conditional logic to apply classes dynamically:
Creating Reusable Themes with Class Names
You can create reusable themes by defining class name configurations:
Best Practices for Class-Based Styling
-
Consistent Naming Conventions
- Use a consistent naming methodology (BEM, SMACSS, etc.)
- Keep class names descriptive and purpose-driven
-
Avoid Collisions
- Use namespacing or CSS Modules to prevent class name collisions
- Consider component-specific prefixes for custom classes
-
Separation of Concerns
- Use classes for styling and not for functionality
- Keep component structure and styling separate
-
Performance
- Minimize the number of classes applied to an element
- Use CSS custom properties for values that change frequently