search

Tailwind

You can style your Dioxus application with whatever CSS framework you choose, or just write vanilla CSS.

One popular option for styling your Dioxus application is Tailwind. Tailwind allows you to style your elements with CSS utility classes. This guide will show you how to setup tailwind CSS with your Dioxus application.

Setup

  1. Install the Dioxus CLI:
cargo install dioxus-cli@0.5
  1. Install npm: https://docs.npmjs.com/downloading-and-installing-node-js-and-npm
  2. Install the tailwind css cli: https://tailwindcss.com/docs/installation
  3. Initialize the tailwind css project:
npx tailwindcss init

This should create a tailwind.config.js file in the root of the project.

  1. Edit the tailwind.config.js file to include rust files:
module.exports = {
    mode: "all",
    content: [
        // include all rust, html and css files in the src directory
        "./src/**/*.{rs,html,css}",
        // include all html files in the output (dist) directory
        "./dist/**/*.html",
    ],
    theme: {
        extend: {},
    },
    plugins: [],
}
  1. Create a input.css file in the root of your project with the following content:
@tailwind base;
@tailwind components;
@tailwind utilities;
  1. Add Manganis to your project to handle asset collection.
cargo add manganis
  1. Create a link to the tailwind.css file using manganis somewhere in your rust code:
// Urls are relative to your Cargo.toml file
const _TAILWIND_URL: &str = manganis::mg!(file("public/tailwind.css"));

Bonus Steps

  1. Install the tailwind css vs code extension
  2. Go to the settings for the extension and find the experimental regex support section. Edit the setting.json file to look like this:
"tailwindCSS.experimental.classRegex": ["class: \"(.*)\""],
"tailwindCSS.includeLanguages": {
    "rust": "html"
},

Development

  • Run the following command in the root of the project to start the tailwind css compiler:
npx tailwindcss -i ./input.css -o ./public/tailwind.css --watch

Web

  • Run the following command in the root of the project to start the dioxus dev server:
dx serve

Desktop

  • Launch the dioxus desktop app:
dx serve --platform desktop