> For the complete documentation index, see [llms.txt](https://docs.blynk.io/en/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.blynk.io/en/html-widget-developer-guide/theme-and-layout.md).

# Theme & Layout

### Get the app theme

> Returns the app's colors and font families, already resolved for the current light or dark mode.

```js
bridge.getTheme()  // → Promise<Theme>
```

**Parameters:** none.

**Resolves with** colors matched to the current Blynk theme — no `prefers-color-scheme` adaptation needed — plus the font families the app uses.

| Field         | Maps to attribute       | Typical use                          |
| ------------- | ----------------------- | ------------------------------------ |
| `brand`       | `blynkBrandColor6`      | Primary accent, buttons, focus rings |
| `primary`     | `blynkPrimaryColor6`    | Secondary accent                     |
| `positive`    | `blynkPositiveColor6`   | Success, online status               |
| `warning`     | `blynkWarningColor6`    | Warning badges                       |
| `critical`    | `blynkCriticalColor6`   | Errors, offline status               |
| `background`  | `blynkBackgroundColor`  | Page and widget background           |
| `textPrimary` | `blynkTextPrimaryColor` | Body text                            |
| `onPrimary`   | `blynkOnPrimaryColor`   | Text on primary-colored surfaces     |
| `onBrand`     | `blynkOnBrandColor`     | Text on brand-colored surfaces       |
| `neutral`     | `blynkNeutralColor9`    | Muted text, borders                  |

Colors are CSS color strings and may carry an alpha channel (`#rrggbbaa`), so derive tints with `color-mix()` rather than by appending hex digits.

| Font field         | Typical use                       |
| ------------------ | --------------------------------- |
| `primaryFont`      | Body text and general labels      |
| `secondaryFont`    | Sub-labels, captions, helper text |
| `buttonsFont`      | Buttons and interactive elements  |
| `widgetValuesFont` | Numeric and data-value display    |

Font fields are `string | null` — present only when the app has custom typography. Use them as CSS `font-family` values with a system-font fallback. The host injects the matching `@font-face` rules for you.

**Rejects with** `request_timeout` (web, 10 seconds — `Theme request timeout (10s)`), `device_unavailable`, `bridge_destroyed`.

**Reacting to theme changes** — register [`onThemeUpdated`](https://docs.blynk.io/en/html-widget-developer-guide/callbacks.md) and apply the theme both at boot and in the callback. That covers every host:

* **Web:** `onThemeUpdated` fires on a light/dark switch and on organization branding changes.
* **Mobile:** a light/dark switch either reloads the widget or is delivered through `onThemeUpdated`, depending on the app. The callback also fires once when custom fonts finish loading after an earlier `getTheme()` returned them incomplete.

**Example**

```js
function applyTheme(theme) {
  if (!theme) return;
  const r = document.documentElement.style;
  const set = (name, value) => { if (value) r.setProperty(name, value); };

  set('--accent',        theme.brand);
  set('--bg',            theme.background);
  set('--text',          theme.textPrimary);
  set('--color-online',  theme.positive);
  set('--color-offline', theme.critical);
  set('--color-warning', theme.warning);
  set('--color-neutral', theme.neutral);

  set('--font-primary',       theme.primaryFont);
  set('--font-secondary',     theme.secondaryFont);
  set('--font-buttons',       theme.buttonsFont);
  set('--font-widget-values', theme.widgetValuesFont);
}

bridge.setCallbacks({ onThemeUpdated: applyTheme });
bridge.getTheme().then(applyTheme).catch(() => {});
```

```css
:root {
  --bg:      #f7f7f8;
  --text:    #111114;
  --accent:  #22c55e;
  --accent-ghost: color-mix(in srgb, var(--accent) 15%, transparent);

  --font-primary:       system-ui, sans-serif;
  --font-secondary:     system-ui, sans-serif;
  --font-buttons:       system-ui, sans-serif;
  --font-widget-values: system-ui, monospace;
}

body       { font-family: var(--font-primary); }
.value     { font-family: var(--font-widget-values); }
button     { font-family: var(--font-buttons); }
.sub-label { font-family: var(--font-secondary); }
```

> Keep fallback values in `:root` so the widget looks right in the browser and editor before `getTheme()` resolves.

***

### CSS custom properties

```css
:root {
  /* — updated by getTheme() — */
  --bg:             #f7f7f8;
  --text:           #111114;
  --accent:         #22c55e;
  --accent-ghost:   color-mix(in srgb, var(--accent) 15%, transparent);
  --color-online:   #22c55e;
  --color-offline:  #ef4444;

  /* — static — */
  --surface:        #ffffff;
  --text-muted:     #74747a;
  --border:         #e5e5ea;
  --radius:         10px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg:          #0f0f11;
    --text:        #f4f4f5;
    --surface:     #18181b;
    --text-muted:  #8a8a92;
    --border:      #2a2a2f;
  }
}
```

The `prefers-color-scheme` block is a fallback for the editor and browser only. Inside a Blynk host, `getTheme()` returns colors for the current Blynk theme — which can differ from the OS preference — so always call it and apply its values.

***

### Responsive layout with container queries

The widget tile can be any size, so adapt with CSS container queries rather than viewport media queries:

```css
.stage {
  container-type: size;
  container-name: widget;
  width: 100%;
  height: 100%;
  display: flex;
}

@container widget (max-width: 220px) {                         /* compact tile */
  .widget { padding: 8px; font-size: 11px; }
}

@container widget (min-width: 420px) and (min-height: 320px) {  /* wide, tall tile */
  .widget { padding: 16px; gap: 14px; }
}
```

Use `clamp()` with container units for fluid type: `font-size: clamp(12px, 3cqmin, 15px);`. Container query units (`cqw`, `cqh`, `cqmin`, `cqmax`) work in Blynk WebViews.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.blynk.io/en/html-widget-developer-guide/theme-and-layout.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
