Module lightningcss::rules

source ·
Expand description

CSS rules.

The CssRule enum includes all supported rules, and can be used to parse and serialize rules from CSS. Lists of rules (i.e. within a stylesheet, or inside another rule such as @media) are represented by CssRuleList.

Each rule includes a source location, which indicates the line and column within the source file where it was parsed. This is used when generating source maps.

Example

This example shows how you could parse a single CSS rule, and serialize it to a string.

use lightningcss::{
  rules::CssRule,
  traits::ToCss,
  stylesheet::{ParserOptions, PrinterOptions}
};

let rule = CssRule::parse_string(
  ".foo { color: red; }",
  ParserOptions::default()
).unwrap();

assert_eq!(
  rule.to_css_string(PrinterOptions::default()).unwrap(),
  ".foo {\n  color: red;\n}"
);

If you have a cssparser::Parser already, you can also use the parse and to_css methods instead, rather than parsing from a string.

See StyleSheet to parse an entire file of multiple rules.

Modules

Structs

Enums