Crate inline_css
source ·Expand description
Embed CSS directly into your Rust code.
Example
use inline_css::*;
let x = 42;
let css = css! {
h1 {
color: red;
background-color: #0x00ffff; // not #00ffff
padding: 10px;
margin-left: 10m; // not 10em
margin-right: -4%;
}
};
println!("{css}");
How to use
Use the css! {..}
macro to embed CSS directly into your Rust code.
During the compile-time of your Rust program, the content inside of your css!
invocations
will be checked and converted to an instance of CSS
.
This makes sure that all invocations of the css!
macro are guaranteed to be valid CSS at runtime.
Variants
There are 3 macros:
css!
Parse an entire snippet of CSS.
use inline_css::*;
let rule: CSS = css! {
h1 {
color: red;
}
.button {
color: blue;
}
#text {
color: green;
}
};
css_rule!
Parse a single CSS Rule.
use inline_css::*;
let rule: Rule = css_rule! {
h1 {
color: red;
}
};
css_value!
Parse a CSS Value.
use inline_css::*;
let value: Value = css_value! { 10px };
Dynamic Data
You can include dynamically-generated data into the css!
macro invocation.
Only types implementing Into<Value>
are supported.
Example
use inline_css::*;
let x = 42;
let css = css! {
h1 {
attr: {x + 1};
}
};
Macros
Structs
- CSS holds an entire snippet of CSS.
- A CSS declaration.
- A single CSS rule.
Enums
- A CSS selector.
- A CSS value.