Recursive AI

Read registered CSS custom properties in Go

onehtml-go is a free, open-source HTML parser for Go from Recursive AI, with CSS selectors, computed styles, lossless editing and accessibility checks. MIT or Apache-2.0, no dependencies.

A CSS @property rule gives a variable a type, a default, and an inheritance rule. Read those descriptors with Styles.Registrations(). Requires Go 1.24 or later.

Terminal
go get recursiveai.org/go/onehtml
GO
package main
 
import (
	"fmt"
	"log"
	"strings"
 
	"recursiveai.org/go/onehtml"
)
 
func main() {
	const html = `<style>
@property --gap {
  syntax: "<length>";
  inherits: false;
  initial-value: 4px;
}
.box { margin-left: var(--gap) }
</style>
<div id="outer" style="--gap: 12px">
  <p id="inner" class="box">Card</p>
</div>`
	doc, err := onehtml.Parse(strings.NewReader(html))
	if err != nil {
		log.Fatal(err)
	}
	styles := doc.Style()
	for _, rule := range styles.Registrations() {
		fmt.Println(rule.Name, rule.Syntax,
			rule.InitialValue, rule.Inherits)
	}
	outer := doc.ElementByID("outer")
	inner := doc.ElementByID("inner")
	fmt.Println("outer:", styles.ComputedStyle(outer).Get("--gap"))
	style := styles.ComputedStyle(inner)
	fmt.Println("inner:", style.Get("--gap"), style.Get("margin-left"))
}

Paste the code above into main.go, run go mod init example and go get recursiveai.org/go/onehtml, then go run .; or unzip the example and go run ..

Text
--gap <length> 4px false
outer: 12px
inner: 4px 4px

The rule is printed as name, syntax, initial value, and inheritance flag. The child uses 4px, not the parent’s 12px. If an @property rule is invalid, it is omitted from Registrations() rather than returned with partial descriptors.

Variations

Set inherits: true to let the child receive the parent’s 12px. Replace the registration in the HTML with:

CSS
@property --gap {
  syntax: "<length>";
  inherits: true;
  initial-value: 4px;
}

The last output line becomes inner: 12px 12px.

The short answer

Call doc.Style().Registrations() to list valid @property rules. Each entry gives you the name, syntax, initial value, and inheritance flag. To see the value on one element, use ComputedStyle(node).Get("--gap") instead. A registration can say that the default is 4px even while a particular element uses 12px.

What is @property in CSS?

@property registers a custom property. An ordinary --gap declaration supplies a value in a selector’s scope. Registration defines how that name works throughout the document: in this case syntax: "<length>" expects a length, inherits: false disables default inheritance, and initial-value: 4px supplies a default. The var(--gap) reference in margin-left uses the custom property’s computed value for that element.

The registration inspector returns one entry per name, sorted by name. The last valid registration in stylesheet order wins. That makes the list useful for inventorying design tokens, but it doesn’t tell you which selector assigned a value to a given component. It isn’t a list of every --name declaration either: unregistered variables may be in use without appearing in the list.

What does inherits control in a custom @property declaration?

A custom property declared without registration inherits by default. With a registration, the inherits descriptor lets you change that. In the example, the outer element explicitly sets --gap to 12px. The inner element doesn’t set --gap, so with inherits: false it uses 4px. If you switch to inherits: true as in the variation, the same inner element receives 12px. An explicit valid assignment on the inner element can still set its own value.

For a component where callers are meant to set a spacing token on an ancestor, check the inheritance flag before looking for a selector bug. For a token that’s supposed to reset inside each component, check the opposite: compare a parent with an explicit value and a child without one. Reading only the registration won’t tell you whether the component’s own styles also assign the token.

When does CSS use the @property initial value?

The initial value is the registered default. A non-inheriting registration uses it when the element has no assignment. A value that doesn’t fit the registered syntax is treated as unset by onehtml-go, so whether inheritance applies still matters. This is distinct from the optional fallback in var(): if a custom property already resolves to its registered 4px, the consumer has a value and needs no substitution fallback.

When reading the Go result, check HasInitialValue if the distinction between an omitted default and a supplied one matters. The universal syntax "*" is the case where the rule can omit it. For typed registrations, inspect the reported initial value and then compute the value of the element you actually care about.

All onehtml Go recipes