Inline Styles
Using the style Attribute
You can apply inline styles using thestyle attribute with CSS syntax:
Individual Style Properties
Dioxus allows you to set individual CSS properties as attributes using snake_case:Common Style Properties
Dynamic Styles
You can use Rust expressions for dynamic styling:CSS Classes
Using the class Attribute
Multiple Classes
You can specify multiple classes by including theclass attribute multiple times:
Conditional Classes
Use unterminated if statements for conditional classes:External CSS Files
Loading CSS with asset!
Use theasset! macro to include CSS files:
assets/style.css:
CSS Modules
CSS Modules provide scoped styling to avoid naming conflicts:assets/component.css:
CSS Module Options
You can configure CSS modules with options:Global Classes
To use global classes within CSS modules, prefix them with:global:
Tailwind CSS
Dioxus works seamlessly with Tailwind CSS:Setup
- Install Tailwind CSS in your project
- Configure
tailwind.config.js:
- Create your Tailwind CSS file:
- Include it in your app:
Using Tailwind Classes
Conditional Tailwind Classes
Best Practices
Component-Scoped Styles
Keep styles close to components using CSS modules:Responsive Design
Use media queries in CSS or Tailwind’s responsive utilities:Style Organization
- Use inline styles for component-specific, dynamic styles
- Use CSS modules for component-scoped static styles
- Use global CSS for site-wide styles and resets
- Use Tailwind for utility-first styling
Performance Tips
- Prefer CSS classes over inline styles for static styling
- Use CSS modules to avoid global namespace pollution
- Minimize dynamic style calculations
- Use the
asset!macro for optimal asset loading
Next Steps
Events
Add interactivity to your styled components
Assets
Learn more about asset management