CSS Syntax and Basic Data Types
wgpu-html parses CSS values into strongly-typed Rust enums and structs. This page documents the supported syntax for each value type.
Length Units
Length values are parsed into the CssLength enum:
pub enum CssLength {
Px(f32),
Percent(f32),
Em(f32),
Rem(f32),
Vw(f32),
Vh(f32),
Vmin(f32),
Vmax(f32),
Auto,
Zero,
Calc(Box<CssMathExpr>),
Min(Vec<CssLength>),
Max(Vec<CssLength>),
Clamp { min: Box<CssLength>, preferred: Box<CssLength>, max: Box<CssLength> },
Raw(String),
}
Absolute Units
| Unit | Description | Example | Rust |
|---|---|---|---|
px | CSS pixels | 16px | CssLength::Px(16.0) |
0 | Zero (unitless) | 0 | CssLength::Zero |
The bare value 0 is treated as a special zero that does not require a unit, matching CSS specification behaviour.
Relative Units
| Unit | Description | Example |
|---|---|---|
% | Percentage of containing block | 50% |
em | Relative to element's font-size | 1.5em |
rem | Relative to root font-size | 1.2rem |
vw | 1% of viewport width | 100vw |
vh | 1% of viewport height | 50vh |
vmin | 1% of viewport's smaller dimension | 50vmin |
vmax | 1% of viewport's larger dimension | 50vmax |
Note: em and rem use a hard-coded 16px fallback when no font-size is inherited.
Auto
The auto keyword resolves to CssLength::Auto and is used for automatic sizing in width, height, margins, and positioned offsets.
Math Functions as Lengths
Length values can be produced by CSS math functions:
width: calc(100% - 20px);
height: min(50vh, 400px);
font-size: max(12px, 2vw);
padding: clamp(8px, 2vw, 32px);
These parse into CssLength::Calc(Box<CssMathExpr>), CssLength::Min(Vec<CssLength>), CssLength::Max(Vec<CssLength>), and CssLength::Clamp { ... } respectively. See the Math Functions page for full details.