Lengths and units
px, rem, pct, and the other unit functions build a Length or a Percentage. A property's value type decides which of the two it accepts.
Length and Percentage
Length is a number with a length unit, and Percentage is a number with %. Each unit has a function, such as fn px(value: impl Into<f64>) -> Length, and fn pct(value: impl Into<f64>) -> Percentage builds a percentage. Both types implement Display, which writes the value as the stylesheet holds it:
use hypersig::style::value::{Length, ch, em, pct, px, rem};
assert_eq!(px(16).to_string(), "16px");
assert_eq!(rem(1.5).to_string(), "1.5rem");
assert_eq!(em(-0.25).to_string(), "-0.25em");
assert_eq!(ch(65).to_string(), "65ch");
assert_eq!(pct(50).to_string(), "50%");
// A zero length has no unit, and a zero percentage keeps its `%`.
assert_eq!(px(0).to_string(), "0");
assert_eq!(Length::ZERO.to_string(), "0");
assert_eq!(pct(0).to_string(), "0%");A length of zero is written 0, which CSS accepts without a unit, and Length::ZERO is the same value. A percentage of zero keeps its %.
The units
| Function | CSS | One unit is |
|---|---|---|
px(1) | 1px | One CSS pixel |
rem(1) | 1rem | The root element's font size |
em(1) | 1em | The element's font size |
ch(1) | 1ch | The width of 0 in the element's font |
ex(1) | 1ex | The x-height of the element's font |
lh(1) | 1lh | The element's line height |
vw(1), vh(1) | 1vw, 1vh | 1% of the viewport's width or height |
vmin(1), vmax(1) | 1vmin, 1vmax | 1% of the viewport's smaller or larger side |
svh(1), lvh(1), dvh(1) | 1svh, 1lvh, 1dvh | 1% of the viewport's height with the browser's toolbars shown, hidden, or as they are now |
cqw(1), cqh(1) | 1cqw, 1cqh | 1% of the query container's width or height |
cm(1), mm(1), pt(1) | 1cm, 1mm, 1pt | A centimeter, a millimeter, or a point |
pct(1) | 1% | 1% of what the property measures against |
cqw and cqh measure the nearest ancestor that is a query container, which Container queries covers. A unit with no function here, such as vb or Q, goes through raw.
Which properties take which
Whether a property takes a length, a percentage, or both is decided by its value type, and the type's rustdoc lists its From implementations. Width converts from both. BorderWidth converts from a length only, because CSS has no percentage border width, so pct there is E0277:
use hypersig::style::Style;
use hypersig::style::value::pct;
// error[E0277]: the trait bound `BorderWidth: From<Percentage>` is not satisfied
let frame = Style::new().border_width(pct(1));A bare number is not a length. CSS needs a unit on every length but zero, and Width does not convert from an integer, so .width(240) is E0277. Write px(240):
use hypersig::style::Style;
// error[E0277]: the trait bound `hypersig::hypersig_style::value::Width: From<{integer}>`
// is not satisfied
let sidebar = Style::new().width(240);A few types that take a length also take a plain number, with a meaning of its own. line-height: 2 is twice the element's font size, not 2px, so LineHeight takes a multiplier only as an f64, and line_height(1.5) writes line-height: 1.5. An integer such as line_height(16) is E0277, and the error lists the conversions LineHeight has. Write px(16) for a length or 1.0 for a multiplier:
use hypersig::style::Style;
// error[E0277]: the trait bound `hypersig::hypersig_style::value::LineHeight: From<{integer}>`
// is not satisfied
// help: `LineHeight` implements trait `From<T>`: `From<Length>`, `From<Percentage>`, `From<f64>`
let body = Style::new().line_height(16);Math functions
clamp, min, and max write the CSS functions of the same names. + and - between two values, and * and / by an f64, write a calc(). Each takes values of one type and returns that type, so the result goes wherever its arguments would. The types are those that implement Quantity: Length, Percentage, Time, and Angle.
use hypersig::style::Style;
use hypersig::style::value::{clamp, max, pct, px, rem, vw};
// A heading that grows with the viewport, from 1.5rem up to 3rem.
let title = Style::new().font_size(clamp(rem(1.5), vw(4), rem(3)));
// `+`, `-`, `*`, and `/` write a `calc()`.
let sidebar = Style::new()
.width(max(pct(25), pct(10) + pct(5)))
.padding_left(px(16) * 1.5);
assert_eq!(clamp(rem(1.5), vw(4), rem(3)).to_string(), "clamp(1.5rem, 4vw, 3rem)");
assert_eq!((rem(1) - px(2) * 2.0).to_string(), "calc(1rem - (2px * 2))");A length and a percentage are different types, so pct(100) - rem(2) is E0308. A property such as scroll_margin_top takes only a length, and the browser would drop a percentage there. Where a property takes both, write the mixed calc() with raw:
use hypersig::style::value::{pct, rem};
// error[E0308]: mismatched types: expected `Percentage`, found `Length`
let inner = pct(100) - rem(2);What is not checked
The sign is not checked. A negative length is right for margin_top or letter_spacing, and wrong for width, where the browser drops the declaration and the element renders as if width were not set.
Every unit function panics on NaN or infinity, as Numbers shows. An f32 converts into the f64 nearest to it, which is written with all its digits:
use hypersig::style::value::px;
let gap: f32 = 0.1;
// The `f32` widens to the `f64` nearest to it, which is not 0.1.
assert_eq!(px(gap).to_string(), "0.10000000149011612px");
assert_eq!(px(0.1).to_string(), "0.1px");A literal without a suffix, such as px(0.1), is an f64 and is written as 0.1px. A calc() that mixes a length and a percentage goes through raw, which Unchecked values covers, and a length shared across the page is a token, whose var() is a Length.
Example: a reading column
The column is as wide as the screen up to 65 characters of its font, at least as tall as the viewport, and centered with margin-inline: auto. The paragraph's top margin is one line of its own text:
use hypersig::html::{El, article, h1, p};
use hypersig::style::Style;
use hypersig::style::value::{Length, MarginInline, ch, dvh, lh, pct, rem};
use hypersig::{Page, Signals};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Post {}
fn post(title: &str, body: &str) -> El<Post> {
article()
.css(
Style::new()
.width(pct(100))
.max_width(ch(65))
.min_height(dvh(100))
.margin_inline(MarginInline::Auto)
.padding_inline(rem(1.5)),
)
.child(h1().css(Style::new().margin_top(Length::ZERO)).text(title))
.child(p().css(Style::new().margin_top(lh(1))).text(body))
}
let html = Page::new("Post", Post {})
.child(post("Lengths", "A line of this column holds about 65 characters."))
.render();Next, Colors covers the Color type.