日本語

Pseudo-elements

Style::pseudo styles a part of an element that is not an element of its own, such as ::before, ::placeholder, or the marker of a list item.

Declarations for a part of an element

Style::pseudo, fn pseudo(self, element: PseudoElement, style: Style) -> Style, puts the declarations of style on a pseudo-element of the element. PseudoElement is generated from MDN's CSS data and has Before, After, Placeholder, Marker, Selection, FileSelectorButton, Backdrop, and the rest of the standard pseudo-elements.

use hypersig::html::{a, input, li, ul};
use hypersig::style::value::{Color, quoted};
use hypersig::style::{PseudoClass, PseudoElement, Style};
use hypersig::{Page, SafeUrl, Signals};
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
struct Search {
    query: String,
}

// `::after` is drawn only when `content` is set.
let more = a::<Search>()
    .href(SafeUrl::new("/docs"))
    .css(
        Style::new()
            .pseudo(PseudoElement::After, Style::new().content(quoted(" →")))
            // `:hover::after`: the pseudo-class is tested on the link.
            .on(
                PseudoClass::Hover,
                Style::new().pseudo(PseudoElement::After, Style::new().color(Color::RED)),
            ),
    )
    .text("More");

let field = input::<Search>()
    .bind(Search::query())
    .css(Style::new().pseudo(PseudoElement::Placeholder, Style::new().color(Color::GRAY)));

// `::marker` is the bullet of a list item.
let step = Style::new().pseudo(PseudoElement::Marker, Style::new().color(Color::hex(0x0025_63eb)));
let steps = ul::<Search>().child(li().css(step).text("Install"));

Writing .more, .field, and .step for the link, the field, and the list item, the browser applies:

.more::after { content: " →" }
.more:hover::after { color: red }
.field::placeholder { color: gray }
.step::marker { color: #2563eb }

Pseudo-classes inside a pseudo-element

A pseudo-class always applies to the element, never to the pseudo-element. pseudo(After, Style::new().on(Hover, ..)) and on(Hover, Style::new().pseudo(After, ..)) both become :hover::after, which matches while the pointer is over the element.

A pseudo-element is not a condition

on takes a Condition, and PseudoElement does not convert into one, because a pseudo-element is a part of the element and not a state it is in. Passing one to on is a compile error:

use hypersig::style::value::quoted;
use hypersig::style::{PseudoElement, Style};

// error[E0277]: the trait bound `Condition: From<PseudoElement>` is not satisfied
let more = Style::new().on(PseudoElement::After, Style::new().content(quoted(" →")));

One pseudo-element deep

CSS has no pseudo-element of a pseudo-element, so pseudo panics when the style it is given already puts a declaration on one. The panic happens where the style is built, so a test that builds the page finds it:

use hypersig::style::value::Color;
use hypersig::style::{PseudoElement, Style};

// Panics: a pseudo-element holds no pseudo-element
let style = Style::new().pseudo(
    PseudoElement::Before,
    Style::new().pseudo(PseudoElement::Marker, Style::new().color(Color::RED)),
);

What the types leave unchecked

  • ::before and ::after are drawn only when content is set. Without it, the other declarations compile and nothing appears.
  • Nothing checks that the element has the pseudo-element. ::placeholder on a <div> or ::marker on an element that is not a list item applies to nothing.
  • Some pseudo-elements accept only a few properties. ::marker takes color, content, and the font properties, among others, and the browser ignores the rest.

Next

Media queries applies a style from a viewport width or under a user setting. URLs and strings covers quoted, which builds the value of content.