If you're using Leptos (a Rust web framework for building client- and server-side user interfaces) and trying to use stylance (a CSS-in-Rust tool that lets you import CSS files as class names), but your styles aren't showing up even though the class is correctly applied in the DOM, there are a few possible issues and solutions:
✅ 1. Ensure stylance is properly configured
Make sure you've followed all setup instructions from stylance's documentation.
In particular:
- You must have a
build.rs file that runs the stylance_codegen.
- Your CSS file should be referenced with a
#[component] macro or include_css!() if not using components.
Example setup (build.rs):
fn main() {
stylance_codegen::start();
}
✅ 2. Check that CSS is being emitted and loaded
Even if the class name appears in the DOM, the CSS might not be injected into the page.
🔍 How to verify:
- Open browser dev tools.
- Go to the "Network" tab and check if any
.css files are loaded.
- Or go to the "Elements" tab and look for a
<style> tag containing your CSS.
If no styles appear, it means stylance isn't injecting the styles correctly.
🛠️ Solution:
Make sure you're using include_css!() somewhere in your app root or component module:
include_css!("path/to/your_component.module.css");
This macro ensures that the associated CSS gets included and injected at runtime.
Note: If you're using stylance with scoped modules (e.g., .module.css), the generated class names will be unique, so ensure they match what's expected.
✅ 3. Use class= instead of class: when setting dynamic classes
Leptos has two ways to set classes:
class=... — sets a single class dynamically.
class:name=... — conditionally toggles a class.
class:{name} — dynamically computed class name.
If you're doing something like this:
view! { <div class=custom_class /> }
It may not work if custom_class is a String or &'static str. Instead, wrap it in attr::
view! { <div attr:class=custom_class /> }
Or use class:{} syntax:
view! { <div class:my-class=move || true /> }
But if you're directly assigning a known class name:
let cls = "my_custom_class_from_stylance";
view! { <div class=cls /> }
That won’t work. Use:
view! { <div attr:class=cls /> }
✅ 4. Check for conflicting or overridden styles
Even if the class is applied and the style exists, it could be overridden by other styles.
Use the browser dev tools to inspect the element and see which styles are being applied. Look for:
- Styles from other sources overriding your
stylance styles.
- Whether the CSS rules are actually present in the
<style> tag.
✅ 5. Are you using target correctly in build.rs?
If you're using conditional compilation or different targets, make sure the codegen is running on the right one.
✅ 6. Try a global stylesheet temporarily
As a test, try moving your CSS into a global stylesheet (like output.css) and link it in index.html.
If the styles appear then, it confirms the issue is with how stylance is bundling or injecting styles.
✅ 7. Check stylance version compatibility with Leptos
Make sure both crates are compatible versions. Try updating them in Cargo.toml:
[dependencies]
leptos = "0.5"
stylance = "0.8"
Also check the feature flags (e.g., csr, ssr, etc.).
✅ Example Working Setup
File structure:
src/
├── main.rs
├── build.rs
└── components/
├── my_component.rs
└── my_component.module.css
my_component.rs
use leptos::*;
include_css!("my_component.module.css");
#[component]
pub fn MyComponent() -> impl IntoView {
let my_class = "my_component_module_css__myClass";
view! { <div attr:class=my_class>"Styled content"</div> }
}
my_component.module.css
.myClass {
color: red;
font-size: 2rem;
}
🧪 Debugging Tips
-
Add a simple inline style to test rendering:
view! { <div style="color: blue">"Test"</div> }
If this works, it proves your DOM updates are visible.
-
Print the class name to console:
leptos::logging::log!("{}", my_class);
Still stuck?
Let me know:
- What version of
leptos and stylance you're using.
- A small code snippet showing how you're importing and applying the styles.
- Whether you're using CSR, SSR, or Hydration.
I can help debug further!