---
title: "Get computed CSS styles without a browser in Go"
url: https://recursiveai.org/onehtml/go/cookbook/compute-css-style-without-browser/
description: "Compute an element’s CSS styles in Go from HTML, stylesheets, and viewport settings, without running JavaScript or laying out the page."
version: "1.1.1"
---

# Get computed CSS styles without a browser 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/compute-css-style-without-browser/compute-css-style-without-browser.zip)

Call `doc.Style()` and read an element’s `ComputedStyle`. No layout or JavaScript runs.

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

Requires Go 1.24 or later.

```go
package main

import (
	"fmt"
	"recursiveai.org/go/onehtml"
)

func main() {
	html := `<style>
   body { font-size: 20px }
   .note { margin-left: 2em }
   @media (min-width: 800px) {
     .note { margin-left: 1em }
   }
 </style><p id="note" class="note">Hi</p>`
	doc, err := onehtml.ParseBytes([]byte(html))
	if err != nil {
		panic(err)
	}
	note := doc.ElementByID("note")
	if !note.Valid() {
		panic("#note not found")
	}
	styles := doc.Style()
	value := styles.ComputedStyle(note)
	fmt.Println(value.Get("margin-left"))
}
```

The default viewport is 1,280 CSS pixels wide, so `min-width: 800px` matches. The media rule sets the margin to `1em`, which resolves to `20px`.

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/compute-css-style-without-browser/compute-css-style-without-browser.zip) and `go run .`.

`go run .` prints:

```text
20px
```

## Variations

Use a narrower viewport to see the value when the media query no longer matches. Add this inside `main`; it prints `40px`.

```go
ctx := onehtml.StyleContext{
    ViewportWidth: 500,
}
narrow := styles.WithContext(ctx)
value = narrow.ComputedStyle(note)
fmt.Println(value.Get("margin-left"))
```

## Related recipes

- [Check HTML for WCAG 2.2 AA and export SARIF in Go](https://recursiveai.org/onehtml/go/cookbook/check-wcag-22-aa-and-export-sarif/)
- [Parse an HTML fragment with element context in Go](https://recursiveai.org/onehtml/go/cookbook/parse-html-fragment-in-context/)

## The short answer

Parse the HTML, call `doc.Style()`, then select an element and read a property with `styles.ComputedStyle(node).Get(name)`. Include the stylesheets and viewport you want the cascade to use. This computes CSS values without starting a browser; it does not measure boxes or run scripts.

## How do I get computed CSS styles for a given element?

Start with the element’s parsed `Node`, not just a tag name. Two paragraphs can match different rules, and values can depend on a parent. For instance, the sample’s `.note` has no `font-size` of its own. It inherits `20px` from `body`, so its `2em` margin resolves to `40px` when the media query does not match. With the example’s initial style context, the media rule wins and `1em` resolves to `20px`. Reading the `style` attribute alone would miss both rules.

If your page has linked CSS, supply the sheet text with `WithStylesheets`, keyed by absolute URL. For relative links, supply the page URL with `WithBaseURL`. The library [never fetches stylesheets](https://recursiveai.org/onehtml/go/docs/overview/). Check `styles.Diagnostics()` for missing sheets before treating the values as representative of the original page. When comparing results from several pages, use the same viewport and the same sheet inputs, or differences in those inputs can change which rules win.

## Can I get the actual pixel width from a computed style?

A value such as the sample’s `2em` can be converted to pixels from the element’s font size. A pixel width that depends on layout is a different question. For example, knowing that an element has `width: 50%` does not tell you how wide its containing block ended up after the page was laid out. The library does not perform that layout step. In browsers, [`getComputedStyle`](https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle#description) can expose a used value for properties that depend on layout, so a browser value is not always the right comparison for this recipe.

The same distinction applies to scripts. You can inspect styles for the HTML you supply; a script that adds a class or a `<style>` element will not run. To inspect markup after such a change, supply that markup and its sheets yourself. For a check on static output, fix the viewport, read the specific properties you care about, and keep geometry checks separate.
