---
title: "Find CSS declarations invalid at computed-value time in Go"
url: https://recursiveai.org/onehtml/go/cookbook/find-invalid-css-computed-declarations/
description: "Report which CSS declarations win the cascade but fail after var() substitution, with the affected element, the original value, and the selector."
version: "1.1.1"
---

# Find CSS declarations invalid at computed-value time 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.

[pkg.go.dev](https://pkg.go.dev/recursiveai.org/go/onehtml) · [GitHub](https://github.com/recursive-ai-oss/onehtml-go) · [Docs](https://recursiveai.org/onehtml/go/docs/) · [Download example (.zip)](https://recursiveai.org/onehtml/go/cookbook/find-invalid-css-computed-declarations/find-invalid-css-computed-declarations.zip)

A CSS declaration can win the cascade and still fail when `var()` is resolved. Find those failures with `Styles.InvalidDeclarations()`. Requires Go 1.24 or later.

```shell
go get recursiveai.org/go/onehtml
```

```go
package main

import (
	"fmt"
	"log"
	"strings"

	"recursiveai.org/go/onehtml"
)

func main() {
	const html = `<style>
p {
  --pad: 4px;
  padding: var(--pad) var(--missing);
  width: var(--pad) var(--pad);
}
</style>
<p id="card">Card</p>`
	doc, err := onehtml.Parse(strings.NewReader(html))
	if err != nil {
		log.Fatal(err)
	}
	for node, decl := range doc.Style().InvalidDeclarations() {
		id, _ := node.Get("id") // Optional label.
		fmt.Printf("%s %s: %s (%s)\n",
			id, decl.Property, decl.Value,
			decl.Source.Selector)
	}
}
```

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](https://recursiveai.org/onehtml/go/cookbook/find-invalid-css-computed-declarations/find-invalid-css-computed-declarations.zip) and `go run .`.

```text
card padding: var(--pad) var(--missing) (p)
card width: var(--pad) var(--pad) (p)
```

The report includes the element ID, property, original value, and selector. An empty report does not mean the source has no CSS errors: declarations rejected during parsing are not listed.

## Variations

To check only the card, replace the document-wide loop with this loop. `doc` is the document from the example.

```go
node := doc.ElementByID("card")
style := doc.Style().ComputedStyle(node)
for _, decl := range style.InvalidDeclarations() {
    fmt.Println(decl.Property)
}
```

This prints `padding` and `width`, one per line. If no element has the requested ID, `ElementByID` returns the zero `onehtml.Node`, whose `Valid()` method returns `false`.

## Related recipes

- [Read CSS custom property registrations](https://recursiveai.org/onehtml/go/cookbook/register-css-custom-properties/)
- [Find elements with CSS selectors in Go](https://recursiveai.org/onehtml/go/cookbook/parse-html-and-query-css-selectors/)

## The short answer

Range over `doc.Style().InvalidDeclarations()` to get the affected node and the failed declaration. Read `Property`, `Value`, and `Source.Selector` to find the rule to fix. This is a computed-style diagnostic, not a CSS syntax checker: it tells you what failed after the cascade picked a winner for an element.

## What does “invalid at computed-value time” mean in CSS?

A declaration using `var()` can look valid until the engine substitutes the variable. In the example, `padding` fails because `--missing` has no value or fallback. `width` fails for a different reason: both variables resolve, leaving `4px 4px`, which is not a valid single width. A variable can be valid in its own right and still make the property that consumes it invalid.

The [CSS Custom Properties specification](https://www.w3.org/TR/css-variables-1/#invalid-variables) explains how these failures differ from parse-time ones. A declaration discarded while parsing never has a chance to win. A declaration that fails at computed-value time has already won; CSS doesn’t go back and try a lower-priority declaration for that property. It behaves as if `unset`: an inherited property takes its inherited value, and a non-inherited one takes its initial value.

For a shorthand such as `padding`, one failing substitution can invalidate the whole shorthand. The Go report uses the shorthand’s own name rather than repeating it for each side of the box.

## How do you fix a CSS variable error with a fallback?

First distinguish a missing variable from a value of the wrong type or shape. A [fallback inside `var()`](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Values/var) can fix a missing variable: replacing `var(--missing)` with `var(--missing, 8px)` lets this `padding` declaration resolve to `4px 8px`. The fallback must be valid where it lands. It would not fix `width`: two valid lengths are still invalid there, so that declaration needs just one `var(--pad)`.

```css
padding: var(--pad) var(--missing, 8px);
width: var(--pad);
```

With both changes, the example’s loop prints no rows. A fallback supplied as a separate, earlier `width` declaration is not the same fix. By the time the later `var()` declaration fails, the cascade has already selected it.

## Why does a CSS variable work on one element but not another?

The same rule can see different custom property values on different elements. A selector tells you where the rule came from; the node tells you which instance actually failed. Keep both in reports for repeated components. For a page-wide check, consume the whole [`Styles.InvalidDeclarations()` iterator](https://pkg.go.dev/recursiveai.org/go/onehtml@v1.1.1#Styles.InvalidDeclarations). It computes styles as it walks the document. If you stop after the first failure, later elements are not checked by that walk. For an inspector where you’ve already selected an element, use the single-style method shown in the variation.
