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.
-
styleholdsRuleand the at-rule types,StyleandResolve, andRun. -
appholdsHandler, whoseStylestakes a list of styles. -
cssholds the value types (Px,Rem,Hex,Var,Calc,Rawand the rest), one function per CSS property, such ascss.Padding, and one function per at-rule descriptor, such ascss.Src. -
kwholds one constant per CSS keyword and per named color, such askw.Underlineandkw.Rebeccapurple. -
media,containerandsupportshold the queries of@media,@containerand@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
var
Card
=
style
.
Class
(
"demo-card"
)
.
Set
(
css
.
Padding
(
css
.
Rem
(
1
)
)
,
css
.
Background
(
css
.
Var
(
"--mint"
)
)
)
.
Hover
(
css
.
Background
(
css
.
Var
(
"--pink"
)
)
)
The element is
app.Div().Style(Card)
.
-
Setadds 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:
Hoverwrites.demo-card:hover. -
Media,ContainerandSupportsadd properties to a block wrapped in that at-rule. The block holds the rule's own selector:@media print { .demo-card { … } }. -
Nestputs 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.Class("card")
-
A rule owns the base block, then one block per pseudo in the order each was first called, then the
Containerblocks, theSupportsblocks and theMediablocks, 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.Propertyis 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
.