Styled is a Rust crate that provides a simple way to create and apply styles to your Rust code. It provides a Styled
component that you can use to apply styles to any other component in your project.
Add styled
to your Cargo.toml
file:
toml
[dependencies]
styled = "0.1"
To use the Styled
component, import it into your Rust file:
rust
use styled::{Styled, style};
You can then use the Styled component to apply styles to any other component:
```rust fn main() { let cx = Context::new(); let styles = style!( "background-color": "red"; "color": "white"; "font-size": "20px"; );
view! {
cx,
<Styled styles={styles}>
<div>"Hello, world!"</div>
</Styled>
}
} ```
In this example, we're creating a simple <div>
element and applying some styles to it using the Styled component. The styles variable is a stylist::Style object that contains the CSS styles we want to apply.
You can use any valid CSS properties in the style!() macro, and the values can be either string literals or Rust expressions that evaluate to strings.
When you run your Rust code, the Styled component will apply the specified styles to the <div>
element, resulting in a red background with white text and a font size of 20 pixels.