Styling

Properties, keywords, selectors, queries and at-rules as Go values the compiler checks.

The style package describes CSS as Go values. The css , kw , media , container and supports packages supply the typed values and queries those rules are made of. A property that does not exist or a misspelled keyword fails to compile, and so does a value of the wrong kind wherever the property's grammar can be typed exactly (see Values ).

This page covers writing rules. The Applying page covers delivering them: stylesheets, the handler, the browser engine, actions and errors.

  • style holds Rule and the at-rule types, Style and Resolve , and Run .
  • app holds Handler , whose Styles takes a list of styles.
  • css holds the value types ( Px , Rem , Hex , Var , Calc , Raw and the rest), one function per CSS property, such as css.Padding , and one function per at-rule descriptor, such as css.Src .
  • kw holds one constant per CSS keyword and per named color, such as kw.Underline and kw.Rebeccapurple .
  • media , container and supports hold the queries of @media , @container and @supports .

The import paths are github.com/Toms223/WebGo/style , github.com/Toms223/WebGo/css , github.com/Toms223/WebGo/kw , github.com/Toms223/WebGo/media , github.com/Toms223/WebGo/container and github.com/Toms223/WebGo/supports . None of them imports page , data or markdown , so they work in any WebGo application.

Every example on this page is rendered live by the rule shown. Its CSS tab is that rule's CSS() output.

§1 Rules

A Rule is one selector and an ordered list of blocks of properties. It has no exported fields. It is normally a package-level variable. Pass a class rule straight to an element with Style , as in app.Div().Style(card) ; for an id rule, Name() gives the id to pass to ID .

                  
                    func
                     
                    (
                    r
                     
                    *
                    Rule
                    )
                     
                    Set
                    (
                    props
                     
                    ...
                    css
                    .
                    Property
                    )
                     
                    *
                    Rule
                    

                    func
                     
                    (
                    r
                     
                    *
                    Rule
                    )
                     
                    Media
                    (
                    q
                     
                    media
                    .
                    Query
                    ,
                     
                    props
                     
                    ...
                    css
                    .
                    Property
                    )
                     
                    *
                    Rule
                    

                    func
                     
                    (
                    r
                     
                    *
                    Rule
                    )
                     
                    Container
                    (
                    q
                     
                    container
                    .
                    Query
                    ,
                     
                    props
                     
                    ...
                    css
                    .
                    Property
                    )
                     
                    *
                    Rule
                    

                    func
                     
                    (
                    r
                     
                    *
                    Rule
                    )
                     
                    Supports
                    (
                    q
                     
                    supports
                    .
                    Query
                    ,
                     
                    props
                     
                    ...
                    css
                    .
                    Property
                    )
                     
                    *
                    Rule
                    

                    func
                     
                    (
                    r
                     
                    *
                    Rule
                    )
                     
                    Nest
                    (
                    children
                     
                    ...
                    *
                    Rule
                    )
                     
                    *
                    Rule
                    

                    func
                     
                    (
                    r
                     
                    *
                    Rule
                    )
                     
                    CSS
                    (
                    )
                     
                    string
                    

                    func
                     
                    (
                    r
                     
                    *
                    Rule
                    )
                     
                    Err
                    (
                    )
                     
                    error
                  
                
card.go
                  
                    var
                     
                    Card
                     
                    =
                     
                    style
                    .
                    Class
                    (
                    "demo-card"
                    )
                    .
                    
	
                    Set
                    (
                    css
                    .
                    Padding
                    (
                    css
                    .
                    Rem
                    (
                    1
                    )
                    )
                    ,
                     
                    css
                    .
                    Background
                    (
                    css
                    .
                    Var
                    (
                    "--mint"
                    )
                    )
                    )
                    .
                    
	
                    Hover
                    (
                    css
                    .
                    Background
                    (
                    css
                    .
                    Var
                    (
                    "--pink"
                    )
                    )
                    )
                  
                
Result
Hover this card

The element is app.Div().Style(Card) .

  • Set adds properties to the base block, the one written under the bare selector.
  • A pseudo method adds properties to a block for the selector plus that pseudo-class or pseudo-element: Hover writes .demo-card:hover .
  • Media , Container and Supports add properties to a block wrapped in that at-rule. The block holds the rule's own selector: @media print { .demo-card { … } } .
  • Nest puts other rules inside this one. See Nesting .
  • Set , Media , Container , Supports , Nest , the pseudo methods and the binding methods change the rule they are called on and return it, so calls chain. The combinators return a new rule.

Block order

*style.Rule

style.Class("card")

base block .card { … } · Set
→
pseudo blocks .card:hover · in first-call order
→
@container blocks in first-call order
→
@supports blocks in first-call order
→
@media blocks in first-call order
The blocks of one rule, in the order CSS() writes them, whatever order the calls were made in. Empty blocks are left out.
  • A rule owns the base block, then one block per pseudo in the order each was first called, then the Container blocks, the Supports blocks and the Media blocks, each kind in the order its query was first called.
  • Calling the same pseudo method, or a conditional method with the same query, again appends to that block.
  • The writer emits blocks in that order, whatever order the calls were made in.
  • A block with no properties is not written.
  • Properties are written in call order. Duplicates are written as given, and CSS's last-wins rule does the rest. A nil css.Property is skipped.
  • Pseudo and conditional blocks apply to the subject of the rule, its rightmost compound selector: .doc a:hover , never .doc:hover a .

CSS

CSS returns the rule's full text, every block, exactly as the writer emits it into a stylesheet. Blocks are separated by one blank line and the text ends with a newline. A rule without properties returns "" .

Err returns the first error any call latched. A rule never panics on bad input. The errors are listed on the Applying page under Errors .

01