CSS-wide keywords
inherit, initial, unset, revert, and revert-layer apply to every property, so Style::keyword takes the property as a Property value.
The keywords
CSS defines five keywords that every property accepts. CssWideKeyword has one variant for each:
| Variant | CSS | The property takes |
|---|---|---|
Inherit | inherit | The parent's computed value |
Initial | initial | The property's initial value |
Unset | unset | inherit for an inherited property, such as color, and initial for any other |
Revert | revert | The value from the browser's or the user's stylesheet |
RevertLayer | revert-layer | The value from the previous cascade layer |
For a declaration of El::css, the previous layers hold the rules of a Global, so revert-layer gives the element the value a Global rule sets for it, or the browser's default where no rule does.
Setting a property to a keyword
Style::keyword(self, property: Property, value: CssWideKeyword) -> Self writes property: keyword. The property is a variant of Property, so any property can take any of the five. It replaces an earlier value for the same property, as a second call to the property's method does:
use hypersig::html::div;
use hypersig::style::value::{Color, CssWideKeyword};
use hypersig::style::{Property, Style};
use hypersig::{Page, Signals};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Note {}
let style = Style::new()
.color(Color::CRIMSON)
// Replaces `color: crimson`, as a second `color` call would.
.keyword(Property::Color, CssWideKeyword::Inherit)
.keyword(Property::Margin, CssWideKeyword::Revert);
let html = Page::new("Note", Note {}).child(div().css(style)).render();
assert!(html.contains("{color:inherit}") && !html.contains("{color:crimson}"));
assert!(html.contains("{margin:revert}"));The element gets color: inherit and margin: revert. To set a keyword under a condition, call keyword on the style passed to on.
What does not compile
The property methods do not take a CssWideKeyword, because their value types have no conversion from it, so .color(CssWideKeyword::Inherit) is E0277. Use keyword with Property::Color:
use hypersig::style::Style;
use hypersig::style::value::CssWideKeyword;
// error[E0277]: the trait bound `hypersig::hypersig_style::value::Color: From<CssWideKeyword>`
// is not satisfied
let link = Style::new().color(CssWideKeyword::Inherit);A misspelled Property variant, such as Property::Colour, has no variant and is E0599.
Resetting with `all`
all is the shorthand of every property except direction and unicode-bidi, and keyword(Property::All, CssWideKeyword::Unset) resets them. The stylesheet writes all before the properties it resets, as it writes padding before padding-top, so in one style a declaration such as color or accent-color wins over all: unset whatever the order of the calls. The example below relies on that.
Example: a button that reads as a link
all: unset removes the browser's button look, and inherit gives the button the color and the font of the sentence around it. An underline and a pointer cursor make it read as a link:
use hypersig::html::{El, button, p};
use hypersig::style::value::{CssWideKeyword, Cursor, TextDecorationLine};
use hypersig::style::{Property, Style};
use hypersig::{Page, Signals};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Terms {}
/// A button that reads as a link inside a sentence: the browser's button
/// look removed, then the surrounding text's color and font.
fn text_button(label: &str) -> El<Terms> {
button()
.css(
Style::new()
.keyword(Property::All, CssWideKeyword::Unset)
.keyword(Property::Color, CssWideKeyword::Inherit)
.keyword(Property::Font, CssWideKeyword::Inherit)
.text_decoration_line(TextDecorationLine::Underline)
.cursor(Cursor::Pointer),
)
.text(label)
}
let html = Page::new("Terms", Terms {})
.child(p().text("By signing up you accept the ").child(text_button("terms")))
.render();Next, the Pseudo-classes page applies a style only under a condition, such as while the pointer is over the element.