d2lang/d2 · error

unknown style key

Error message

unknown style key: %s

What it means

SetStyle on a d2graph object only recognizes known style keys (fill, stroke, font, text-transform, etc.). An unrecognized key hits the default branch and returns "unknown style key" naming the key.

Solutions

  1. Correct the key spelling against D2's documented style keys
  2. Use the D2 equivalent name (e.g. "stroke-width" not "strokeWidth")
  3. Upgrade D2 if the key was added in a newer version

Example fix

// before
x.style.colour: red
// after
x.style.fill: red
Defensive patterns

Strategy: validation

Validate before calling

var knownStyleKeys = map[string]bool{"fill":true, "stroke":true, "font":true, "font-size":true, "text-transform":true /* ... */}
if !knownStyleKeys[key] { /* reject before SetStyle */ }

Type guard

func knownStyleKey(k string) bool {
	switch k { case "fill","stroke","stroke-width","fill-pattern","font","font-size","text-transform","border-radius","double-border", "shadow","opacity","multiple","animated","link","stroke-dash","bold","italic","underline","font-color": return true }
	return false
}

Try / catch

if err := obj.SetStyle(key, val); err != nil {
	return fmt.Errorf("check style key %q against d2 docs: %v", key, err)
}

Prevention

When it happens

Trigger: Passing any key that is not in SetStyle's switch — e.g. `style.colour`, `style.bg-color`, or a misspelled known key like `style.storke-width`.

Common situations: Typos in .d2 style blocks, using CSS property names instead of D2's keys, or using keys from newer/older D2 versions than the installed binary.

Related errors


AI-assisted analysis of d2lang/d2@0d69dca6f5 (2026-08-31). Data as JSON: /api/errors/6f7184438e99395a. Report an issue: GitHub.

Appendix: source

Thrown at d2graph/d2graph.go:492

	case "double-border":
		if s.DoubleBorder == nil {
			break
		}
		_, err := strconv.ParseBool(value)
		if err != nil {
			return errors.New(`expected "double-border" to be true or false`)
		}
		s.DoubleBorder.Value = value
	case "text-transform":
		if s.TextTransform == nil {
			break
		}
		if !go2.Contains(d2ast.TextTransforms, strings.ToLower(value)) {
			return fmt.Errorf(`expected "text-transform" to be one of (%s)`, strings.Join(d2ast.TextTransforms, ", "))
		}
		s.TextTransform.Value = value
	default:
		return fmt.Errorf("unknown style key: %s", key)
	}

	return nil
}

type ContainerLevel int

func (l ContainerLevel) LabelSize() int {
	// Largest to smallest
	if l == 1 {
		return d2fonts.FONT_SIZE_XXL
	} else if l == 2 {
		return d2fonts.FONT_SIZE_XL
	} else if l == 3 {
		return d2fonts.FONT_SIZE_L
	}
	return d2fonts.FONT_SIZE_M
}

View on GitHub (pinned to 0d69dca6f5)