CSS with classes!

Yew does not natively provide a css in rust solution, but helps with styling by providing programmatic ways to interact with the html class attribute.

Classes

The classes! macro and associated Classes struct simplify the use of HTML classes:

  • Literal
  • Multiple
  • String
  • Optional
  • Vector
  • Slice
  1. use yew::{classes, html};
  2. html! {
  3. <div class={classes!("container")}></div>
  4. };
  1. use yew::{classes, html};
  2. html! {
  3. <div class={classes!("class-1", "class-2")}></div>
  4. };
  1. use yew::{classes, html};
  2. html! {
  3. <div class={classes!(String::from("class-1 class-2"))}></div>
  4. };
  1. use yew::{classes, html};
  2. html! {
  3. <div class={classes!(Some("class"))} />
  4. };
  1. use yew::{classes, html};
  2. html! {
  3. <div class={classes!(vec!["class-1", "class-2"])}></div>
  4. };
  1. use yew::{classes, html};
  2. html! {
  3. <div class={classes!(["class-1", "class-2"].as_ref())}></div>
  4. };

We will expand upon this concept in more CSS.

Inline Styles

Currently Yew does not provide any special help with inline styles specified via the styles attribute, but you can use it like any other html attribute:

  1. use yew::{classes, html};
  2. html! {
  3. <div style="color: red;"></div>
  4. };

We will expand upon this concept in more CSS.