> For the complete documentation index, see [llms.txt](https://docs.dbnetsoft.com/dbnetsoft/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.dbnetsoft.com/dbnetsoft/screenspro/screenspro-native/html-layouts.md).

# HTML layouts

How an HTML layout is built so ScreensPro clients can load, show and control it.

HTML is the native layout format of ScreensPro Native. An HTML layout is a plain `.html` file in the layout folder (or a subfolder). ScreensPro clients load it by its name, exactly as they load a XAML layout from ScreensPro Server: a packet asking for `Lower.xaml` or `Lower` loads `Lower.html` when there is one, else `Lower.xaml`. So existing client configurations keep working when you replace a XAML layout with an HTML one.

Everything the layout uses - stylesheets, scripts, images, fonts, videos - is in the layout folder and referenced with relative paths.

## The minimum

```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Lower third</title>
<script src="/screenspro/layout.js"></script>
<style>
  html, body { margin: 0; width: 100vw; height: 100vh; overflow: hidden; background: transparent;
               font-family: "Segoe UI", sans-serif; }

  .bar { position: absolute; left: 120px; bottom: 120px; padding: 24px 48px; color: #fff;
         background: rgba(8, 28, 72, 0.92); transform: translateX(-130%); }

  /* StoryboardIn / StoryboardOut */
  html.sp-in  .bar { animation: slide-in 0.6s ease-out forwards; }
  html.sp-out .bar { animation: slide-out 0.4s ease-in forwards; }
  @keyframes slide-in  { from { transform: translateX(-130%); } to { transform: none; } }
  @keyframes slide-out { from { transform: none; } to { transform: translateX(-130%); } }

  /* AnimateStoryboard "Flash" */
  .Flash { animation: flash 0.5s 2; }
  @keyframes flash { 50% { background: #f5b700; } }
</style>
</head>
<body>
  <div class="bar" id="Bar">
    <div id="Name" data-bind="Name">Name Surname</div>
    <div id="Title" data-bind="Title">Title</div>
  </div>
</body>
</html>
```

What makes it a ScreensPro layout:

| Rule                                                                   | Why                                                                                                                                                                                                                                                                                      |
| ---------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Transparent background** on `html` and `body`                        | Everything without a background is keyed out (fill + key on NDI and SDI). A full-screen layout simply sets its own background                                                                                                                                                            |
| **The layout fills the output**: `100vw` × `100vh`, `overflow: hidden` | The layout is shown at the size of the video format (1920 × 1080 for 1080p). Use pixels for that size, or `vw` / `vh` to scale with any format                                                                                                                                           |
| **`id` on every element a client controls**                            | The `id` (or `data-name`) is the `x:Name` of XAML: the element name of SetProperty, CallMethod and AnimateStoryboard                                                                                                                                                                     |
| **In and out animations on `html.sp-in` / `html.sp-out`**              | ScreensPro Native adds `sp-in` when the layout is shown and `sp-out` when it is unloaded, and waits for those CSS animations and transitions to finish, like StoryboardIn / StoryboardOut                                                                                                |
| **`<script src="/screenspro/layout.js">`**                             | The ScreensPro layout API and the [custom elements](https://docs.dbnetsoft.com/dbnetsoft/screenspro/screenspro-native/custom-elements.md). ScreensPro Native adds it to a layout that doesn't include it; include it yourself when your own scripts use `screens` while the layout loads |

{% hint style="info" %}
Before it is shown, a loaded layout is in the preview: its elements are there, but `sp-in` is not set yet. Hide what should come in (as `transform: translateX(-130%)` above) so the layout doesn't flash before its in animation.
{% endhint %}

## How packets reach the layout

| ScreensPro                                               | In an HTML layout                                                                                                                                                                                |
| -------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `x:Name`                                                 | `id` (or `data-name`). `*` in a packet's element name matches the beginning: `Row*` reaches `Row1`, `Row2`…                                                                                      |
| StoryboardIn / StoryboardOut                             | CSS on `html.sp-in` / `html.sp-out`; also promises returned by `screens.onIn()` / `screens.onOut()`                                                                                              |
| SetProperty `Text`                                       | The element's text                                                                                                                                                                               |
| SetProperty `Visibility`                                 | `Visible`, `Hidden` (`visibility: hidden`), `Collapsed` (`display: none`)                                                                                                                        |
| SetProperty `Visible`                                    | `true` / `false` (RaceResult Exchange's panels): shown, or `display: none`. Shown again, an element has its own `display` back (a flex box stays one) and its CSS animations play from the start |
| SetProperty `Foreground`, `Background`, `Fill`, `Stroke` | `color`, `background`, `fill` / `stroke`. WPF colors like `#AARRGGBB` work                                                                                                                       |
| SetProperty `Source`, `Path`, `Filename`, `Uri`, `Url`   | `src` of an `img` / `video` / `iframe` (a file of the layout folder), else the background image                                                                                                  |
| SetProperty `Width`, `Height`, `FontSize`                | The CSS property, in pixels when the value is a number                                                                                                                                           |
| SetProperty, any other name                              | The element's property of that name (custom elements have the ScreensPro names: `TargetTime`, `Items`…), else its CSS property, else an attribute                                                |
| CallMethod                                               | The element's method (`Start`, `Stop`, `Play`…). Without an element name: a function of the layout (`window.MyFunction`)                                                                         |
| AnimateStoryboard                                        | A function of the layout by that name, else the CSS class of that name restarted on the element (or on `html` without an element name)                                                           |
| SetDataContextProperty                                   | The layout's local data: `data-bind` and the other bindings, `screens.local`                                                                                                                     |
| SetGlobal                                                | `screens.globals`, `Globals.X` in bindings                                                                                                                                                       |
| ElementFinished                                          | `screens.finished('Name')`                                                                                                                                                                       |

## Bindings

Bindings are attributes; ScreensPro Native updates the elements whenever the data changes. Paths are relative to the element's data context - the layout's local data, or what `data-context` or a list row sets. `Globals.X`, `Local.X` and `Sources.X` always start at the top. A name that is not in the local data is looked up in the globals.

| Attribute                                               | XAML equivalent                                 | Does                                                                                                                                                                                                            |
| ------------------------------------------------------- | ----------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `data-bind="Lastname"`                                  | `Text="{Binding Lastname}"`                     | The element's text (`img` / `video` / `iframe`: its source). The element's own content is the FallbackValue, shown while the path doesn't exist                                                                 |
| `data-context="Left"`                                   | `DataContext="{Binding Local.Left}"`            | The data context of the element and everything in it                                                                                                                                                            |
| `data-show="Subline"`                                   | ObjectToVisibility- / BoolToVisibilityConverter | Shown when the value is there (not null, empty or false). `data-show-default="false"` hides it while the path doesn't exist                                                                                     |
| `data-hide="IsHidden"`                                  |                                                 | The opposite of `data-show`                                                                                                                                                                                     |
| `data-src="images/flags/{Nation}.png"`                  | ImageElement Path + Filename                    | An attribute from a template; hidden while a part is empty. `{PortraitName}[.png]` adds the extension only when the value has none; `{Nation\|match:…}` a part with a format (as `data-format`)                 |
| `data-set-targettime="TargetTime"`                      | other bound properties                          | Sets a property of the element (here `TargetTime` of `<sp-targettime>`)                                                                                                                                         |
| `data-class-review="IsUnderReview"`                     | DataTrigger                                     | The class `review` while the value is there                                                                                                                                                                     |
| `data-show="Diff < 0"`, `data-class-leader="Rank == 1"` | DataTrigger with a value                        | A condition instead of a path: `==` `!=` `<` `<=` `>` `>=` with a number, `'text'`, `true`, `false`, `empty` or another path; `!IsFinished` is the opposite. Numbers compare as numbers                         |
| `data-var-accent="TeamColor"`                           | a DynamicResource bound to data                 | The CSS custom property `--accent` of the element and everything in it (see Variables)                                                                                                                          |
| `data-format="0.00"`                                    |                                                 | The bound value formatted: numbers `0.00`, `#,##0`, `+0.0;-0.0;0`, `0 km/h`, `0%`; `date:dd.MM.yyyy HH:mm`; `time:mm:ss.ff`; `upper`, `lower`, `capitalize`; several: `0.0\|upper`                              |
| `data-empty="–"`                                        |                                                 | Shown when the bound value is empty or not there                                                                                                                                                                |
| `data-restart`                                          |                                                 | On air it starts again: the element's `Restart()` (gauges, bars, own elements), a frame's page of the layout folder loaded again - what ran while the layout was only loaded begins with it                     |
| `data-fit`                                              | `TextTrimming="Scale"` + `MaxWidth`             | Text wider than the CSS `max-width` is condensed. `data-fit="size"`: a smaller font instead. `data-fit="lines"`: text over several lines, its font smaller until it fits the element's height (or `max-height`) |
| `data-styled`                                           | Runs, G-Studio's TextStyle2D                    | The text is HTML (see below)                                                                                                                                                                                    |

Example with a data context `{"Left": {"Firstname": "Jane", "Lastname": "Doe", "Nation": "GER"}, "Rows": [...]}`:

```html
<div data-context="Left">
  <img data-src="images/flags/{Nation}.png">
  <span data-bind="Firstname"></span>
  <span data-bind="Lastname" data-fit style="max-width: 600px">Lastname</span>
</div>
<div data-bind="Globals.EventName"></div>
<div data-bind="Sources.Results.Value.Title"></div>
```

### Styled text

With `data-styled` the text - from a binding, from SetProperty `Text` or written in the layout - is HTML: `Hello <b>Mr</b> President`, any tags and attributes, `style="color:red"` and `<style>` rules (they apply to the whole layout). Write `&lt;` and `&amp;` for `<` and `&`; `<script>` doesn't run.

Parts of a text in a class of the layout: `Welcome \Style:red\Mr\Style\ President` shows "Mr" in the class `red` (several classes: `\Style:red big\`). G-Studio's `\tsmaller;DANNY\t; MARTIN` works the same way. In JSON each `\` is written `\\`.

G-Studio's indexes work in every text, also without `data-styled`: `1\ilST\in` shows "ST" as lower index (the span `.index-l`), `\iu` is the upper index (`.index-u`), and `\in` or the end of the text goes back to normal. Their size and position are CSS of the layout (`gse2html.css` has them).

{% hint style="warning" %}
Styled text comes from the clients as it is: event attributes in it (`onerror=...`) run in the layout. Only use `data-styled` for text from sources you trust.
{% endhint %}

## Variables (like DynamicResource)

The CSS custom properties of a layout's stylesheets are its variables: defined once, mostly in the shared stylesheet (`:root { --accent: #00c2ff; }`), used anywhere as `var(--accent)` - in the stylesheets, the layout's `<style>` and inline styles. Like `{DynamicResource}` every use follows a change at once.

|                                                                                                                                | Does                                                                                                                                                                                                                |
| ------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Global `Resources.Accent = "#E30613"` (SetGlobalPacket, Key `Resources.Accent`; or Key `Resources` with an object)             | Sets `--accent` of every layout (`PanelSoft` → `--panel-soft`, `--x` as it is). Empty or not there: the stylesheet's value again. A file name of a picture becomes `url(...)` (for `background-image: var(--logo)`) |
| Global `Resources.Theme = "night"`                                                                                             | The class `theme-night` on `<html>`: the values of the stylesheet's `html.theme-night { --panel: ...; }` block. Empty: none                                                                                         |
| `data-var-accent="TeamColor"`                                                                                                  | `--accent` of the element and everything in it from the data (as a resource of an element's `Resources` in WPF): a component colours itself per use, from its data context                                          |
| `@property --accent { syntax: '<color>'; inherits: true; initial-value: #00c2ff; }` and `:root { transition: --accent 0.4s; }` | Changes of the variable fade over 0.4 s instead of jumping                                                                                                                                                          |

A variable works in property values only (not in selectors or media queries). The designer edits them in Styles → Variables and Themes, and offers them in every colour, font and size field.

## The `screens` API

`layout.js` gives every layout a `screens` object for its own scripts:

| Member                                                | Does                                                                 |
| ----------------------------------------------------- | -------------------------------------------------------------------- |
| `screens.onIn(fn)` / `screens.onOut(fn)`              | Called with the in / out animation. A returned promise is waited for |
| `screens.onState(fn)`                                 | The layout's state: `Preview` (loaded) → `OnAir` → `None`            |
| `screens.onData(fn)`                                  | Globals, local data or a data source changed                         |
| `screens.globals`, `screens.local`, `screens.sources` | The data                                                             |
| `screens.get('Globals.Score')`                        | A value by its path                                                  |
| `screens.finished('Name')`                            | Sends ElementFinished to the clients (also counts for sequences)     |
| `screens.bind(element)`                               | Applies the bindings to elements you added yourself                  |
| `screens.setText(element, text)`                      | Sets a text as a binding does (with `data-fit`, `data-styled`)       |
| `screens.log(text)`                                   | A line in the channel's ScreensPro log                               |
| `screens.state`                                       | The current state                                                    |

The events `screens:data` and `screens:state` are also dispatched on `window`.

```html
<script>
  screens.onIn(async () => {
    await document.querySelector('#Logo').animate(
      [{ opacity: 0 }, { opacity: 1 }], { duration: 500, fill: 'forwards' }).finished;
  });
  screens.onData((s) => {
    document.querySelector('#Score').textContent = `${s.local?.Home ?? 0} : ${s.local?.Away ?? 0}`;
  });
  // CallMethod "Highlight" without an element name
  window.Highlight = () => document.body.classList.add('highlight');
</script>
```

## CasparCG templates

CasparCG HTML templates work too: ScreensPro Native calls their `update(json)` with each new data context and waits for `play()` / `stop()` like for the in and out animations.

## Named animations

An AnimateStoryboardPacket with a name and no element restarts the class of that name on `<html>`, and `sp-anim-<name in lower case>` too, so a layout can react to the name in any case: `.sp-anim-score [data-on-score="pulse"] { animation: … }`. The layout designer writes these rules for its named animations.

## Toggle and what stays

`<html data-toggle>`: a client loading the layout again while it is on air gets a new copy - the properties and data it sends go into it, and when it is shown it comes in while the old copy goes out (clients see one layout, its state as before). An element with `data-keep` (and an id) that both the leaving and the coming layout have - a toggle copy, or another layout replacing it on the main layer - doesn't go out and come in: the new one is there at once and moves and sizes from where the old one was (`data-keep="0.6"`: in 0.6 s, default 0.5), the old one hides while what is in it goes out. Both get the class `sp-change` on `<html>` for In / Out animations of their own with a change. Not with a channel transition (Settings → transition), which moves whole layouts.

```html
<html lang="en" data-toggle>
…
<div id="Box" class="panel rise" data-keep>
  <div class="rise" data-bind="Name"></div>
</div>
```

## Frame size

`<meta name="screenspro-size" content="1920x1080">` says the size a layout is made for. On a channel of another size (720p, 2160p...) ScreensPro Native draws it as made and scales it to fit, centred. Use pixel lengths (`vw` / `vh` are the channel's). The layout designer writes it into every layout.

## Fonts, images and videos

* Put fonts next to the layout and load them with `@font-face`, or use installed fonts.
* Images and other files are referenced relative to the layout: `images/flags/GER.png`.
* Videos and NDI sources are shown with [`<sp-video>` and `<sp-ndi>`](https://docs.dbnetsoft.com/dbnetsoft/screenspro/screenspro-native/custom-elements/video-and-live.md#sp-video): the application decodes and composites them at full quality.

## Reloading

With **Reload layouts when their files change** on, a layout loads again as soon as you save it - even on air. A changed stylesheet, script, image or font reloads all loaded layouts. A reloaded layout keeps its state and gets what the clients set on it again, without the in animation. This makes it easy to work on a layout in an editor while it is on air in the control window.

## XAML layouts

The XAML layouts of ScreensPro Server (WPF `.xaml` files) keep working: ScreensPro Native renders them with its HTML engine, with the XAML features they use. Copy them, their `Styles.xaml`, fonts, images and videos into the layout folder. A `Name.xaml` without a `Name.html` next to it is a layout; clients use the same layout names, element names and data as with ScreensPro Server.

* `StoryboardIn` (or `In`) runs when the layout is shown, `StoryboardOut` (or `Out`) when it is unloaded, `EventTrigger`s on `Loaded` when it is loaded.
* `Local` is the layout's data context, `Globals` the global values, `Sources` the data sources.
* `Styles.xaml` next to the layout, else in the layout folder, is the application's resources; the dbnetsoft converters (`ObjectToVisibilityConverter`, `BoolToVisibilityConverter`…) are there.
* A layout made for another size is scaled to the output.
* Panels, shapes, brushes, effects (drop shadow, blur), transforms, styles, triggers, bindings and storyboards work as in WPF; so do the Screens.Controls elements (`TextElement`, `ImageElement`, `ClockElement`, `StopwatchElement`, `TargetTimeElement`, `ListElement`, `ItemsElement`, `SlideshowElement`, `VideoElement`, `NdiElement`, `WebbrowserElement`, `FlagWaveEffect`).
* Font files in the layout folder (`Fonts\*.ttf`, `.otf`) are found by their family names without installing them.

Not supported in XAML layouts: code-behind (`x:Class` is ignored, as ScreensPro Server does), controls of other assemblies, 3D, `LayoutTransform` and input controls. Unknown elements are left empty with a warning in the ScreensPro log. Text is laid out by the HTML engine, so a line may come out a pixel wider or narrower than in WPF.
