Styling with Inline Styles
Learn how to use inline style objects to customize @c15t/react components for dynamic and programmatic styling.
Inline styles provide a direct way to style @c15t/react components using JavaScript objects. This approach is particularly useful for dynamic styles that need to be calculated at runtime or when you need to apply styles programmatically.
Basic Usage
You can apply inline styles to components using the style object:
This approach allows you to:
- Apply styles directly without external CSS
- Calculate style values dynamically at runtime
- Maintain all styling within your JavaScript code
Example
Here's a live example of inline styles in action:
Style Property Names
When using inline styles, CSS property names are written in camelCase rather than kebab-case:
CSS Property | JavaScript Style Property |
---|---|
background-color | backgroundColor |
font-size | fontSize |
border-radius | borderRadius |
padding-top | paddingTop |
Dynamic Styling
Inline styles excel at dynamic styling based on component state or props:
Combining with Other Styling Methods
You can combine inline styles with class names for maximum flexibility:
Nested Styles
You can target nested elements with their own style objects:
Units and Values
When specifying dimensions in inline styles:
- Pixel values can be provided as numbers:
width: 100
(becomeswidth: 100px
) - Other units must be strings:
fontSize: '1.25rem'
,width: '50%'
- Unitless values like
fontWeight
andopacity
are provided as numbers
Best Practices for Inline Styles
-
Performance Considerations
- Prefer CSS classes for static styles
- Use inline styles for truly dynamic values
-
Readability and Maintenance
- Group related styles together
- Use descriptive variable names for complex style calculations
-
Browser Compatibility
- Be aware that some CSS features like media queries can't be used directly in inline styles
- Consider CSS Variables or class-based styling for more complex responsive designs
-
Avoid Overuse
- Inline styles don't benefit from CSS cascading and inheritance
- Use sparingly for specific dynamic styling needs