SelfscribeDownloadOpen the app

Live HTML sketches

Try out a bit of HTML and CSS right in a document, safely boxed in.

Interactive · Advanced · uses code, animation

Edit it in the playgroundRead the file

What it shows

Put live HTML and CSS in a document, each piece in a sealed frame.

What you learn

A block rule that hands its lines to the Live HTML plugin, colours that follow light and dark inside a frame, CSS animation, scripts and when they run, line rules for labels and a kicker, a block rule for an aside.

Try this

  • In the first sketch, change the card's gradient from #2f6fdf to #7b4ae2, and the sky turns violet.
  • In the week, change Saturday's 18° to 21°, and the frame redraws as you type.
examples/html-widget.self · 145 lines
~~~ selfscribe
# Lesson: Put live HTML and CSS in a document, each piece in a sealed frame.
# You learn: a block rule that hands its lines to the Live HTML plugin, colours that follow light and dark inside a frame, CSS animation, scripts and when they run, line rules for labels and a kicker, a block rule for an aside.
# Try this: in the first sketch, change the card's gradient from #2f6fdf to #7b4ae2, and the sky turns violet.
# Try this: in the week, change Saturday's 18° to 21°, and the frame redraws as you type.
# summary: Try out a bit of HTML and CSS right in a document, safely boxed in.
# category: diagrams
# tags: interactive, advanced, creative
# uses: code, animation
use markdown
set title Live HTML sketches
set style sans
set heading-font Space Grotesk

# ---- "<html>" ... "</html>": real HTML, drawn live in a frame of its own ----------------
# verbatim keeps the rules of this page out of the HTML.
block  <html> ... </html>   -> html   verbatim

# ---- The page's own colour, with its shade for dark pages ------------------------------
palette  accent  #2f6fdf  dark #8db4ff

# ---- "project text": the small capitals above the title ---------------------------------
line   project _            -> div.kicker  { font: 700 0.74em/1 var(--doc-font-ui); letter-spacing: 0.16em; text-transform: uppercase; color: var(--accent); margin-bottom: -2.6em }  as hw-kicker

# ---- "Sketch 1 · name": the label above each frame --------------------------------------
line   Sketch _             -> div.sketch-label keep { font: 700 0.8em/1 var(--doc-font-ui); letter-spacing: 0.02em; margin: 2.2em 0 0.7em; opacity: 0.75 }  as hw-label

# ---- "::: sandbox" ... ":::": a dashed aside for the note on safety ---------------------
block  ::: sandbox ... :::  -> aside.sandbox { margin: 1.2em 0; padding: 0.8em 1em 0.1em; border-radius: 12px; border: 1px dashed color-mix(in srgb, var(--accent) 45%, transparent); font-size: 0.92em }  as hw-sandbox
~~~
project Weather app · forecast screen
# Three sketches for the forecast

Drawn in HTML for Thursday's review, so we argue about real pixels and not about mock-ups.

Sketch 1 · Today, with the next six hours

<html>
<style>
  body { margin: 0; font: 15px/1.4 system-ui, "Segoe UI", sans-serif; }
  .card { position: relative; overflow: hidden; border-radius: 22px; padding: 22px 26px 18px; color: #fff;
          background: linear-gradient(160deg, #2f6fdf 0%, #4f9cf0 55%, #8fcaf7 100%); }
  .sun { position: absolute; top: -46px; right: -40px; width: 190px; height: 190px; border-radius: 50%;
         background: radial-gradient(circle, #ffe37a 0 29%, #fff4c4cc 30%, #ffffff00 56%);
         animation: glow 5s ease-in-out infinite; }
  @keyframes glow { 50% { transform: scale(1.07); } }
  .place { font-weight: 600; opacity: .92; }
  .now { display: flex; align-items: flex-end; gap: 16px; margin: 4px 0 0; }
  .temp { font-size: 76px; font-weight: 200; line-height: 1; letter-spacing: -3px; }
  .cond { padding-bottom: 10px; opacity: .95; }
  .cond b { display: block; font-size: 18px; font-weight: 600; }
  .hours { display: grid; grid-template-columns: repeat(6, 1fr); gap: 6px; margin-top: 18px; padding-top: 14px;
           border-top: 1px solid #ffffff4d; text-align: center; font-size: 13px; }
  .hours span { display: block; opacity: .8; }
  .hours i { display: block; font-style: normal; font-size: 18px; margin: 3px 0; }
  .hours b { font-size: 16px; font-weight: 600; }
</style>
<div class="card">
  <div class="sun"></div>
  <div class="place">Valencia · Thursday 26 November</div>
  <div class="now"><div class="temp">19°</div><div class="cond"><b>Sunny</b>High 20° · Low 11°</div></div>
  <div class="hours">
    <div><span>Now</span><i>☀️</i><b>19°</b></div>
    <div><span>14:00</span><i>☀️</i><b>20°</b></div>
    <div><span>15:00</span><i>☀️</i><b>20°</b></div>
    <div><span>16:00</span><i>🌤️</i><b>19°</b></div>
    <div><span>17:00</span><i>⛅</i><b>17°</b></div>
    <div><span>18:00</span><i>⛅</i><b>15°</b></div>
  </div>
</div>
</html>

> Ines: the big number reads from across the room. Keep it, and keep the sun small enough not to crowd it.

Sketch 2 · The week as ranges

<html>
<style>
  body { margin: 0; font: 14px/1.4 system-ui, "Segoe UI", sans-serif; }
  .week { border-radius: 18px; padding: 6px 20px; background: light-dark(#f2f5fa, #1e2532); color: light-dark(#1d2633, #e6ecf5); }
  .day { display: grid; grid-template-columns: 4em 1.8em 2.6em 1fr 2.6em; align-items: center; gap: 12px; padding: 9px 0;
         border-top: 1px solid light-dark(#dde3ec, #2e3746); }
  .day:first-child { border-top: 0; }
  .lo { text-align: right; opacity: .6; }
  .bar { position: relative; height: 6px; border-radius: 3px; background: light-dark(#dfe5ee, #323c4d); }
  .bar i { position: absolute; top: 0; bottom: 0; border-radius: 3px; background: linear-gradient(90deg, #5aa9f0, #f6c344 65%, #f29340); }
  .hi { font-weight: 600; }
</style>
<div class="week">
  <div class="day"><b>Today</b><span>☀️</span><span class="lo">11°</span><div class="bar"><i style="left:23%;width:69%"></i></div><span class="hi">20°</span></div>
  <div class="day"><b>Fri</b><span>🌤️</span><span class="lo">12°</span><div class="bar"><i style="left:31%;width:69%"></i></div><span class="hi">21°</span></div>
  <div class="day"><b>Sat</b><span>🌧️</span><span class="lo">10°</span><div class="bar"><i style="left:15%;width:62%"></i></div><span class="hi">18°</span></div>
  <div class="day"><b>Sun</b><span>⛅</span><span class="lo">9°</span><div class="bar"><i style="left:8%;width:54%"></i></div><span class="hi">16°</span></div>
  <div class="day"><b>Mon</b><span>☀️</span><span class="lo">8°</span><div class="bar"><i style="left:0%;width:69%"></i></div><span class="hi">17°</span></div>
  <div class="day"><b>Tue</b><span>🌤️</span><span class="lo">9°</span><div class="bar"><i style="left:8%;width:77%"></i></div><span class="hi">19°</span></div>
  <div class="day"><b>Wed</b><span>☀️</span><span class="lo">11°</span><div class="bar"><i style="left:23%;width:69%"></i></div><span class="hi">20°</span></div>
</div>
</html>

Each bar runs from the day's low to its high, on one scale for the whole week from 8° to 21°. A cold day sits to the left, and a warm one to the right.

Sketch 3 · Celsius or Fahrenheit, needs scripts

<html>
<style>
  body { margin: 0; font: 15px/1.4 system-ui, "Segoe UI", sans-serif; }
  .row { display: flex; align-items: center; gap: 16px; padding: 14px 18px; border-radius: 16px; background: light-dark(#f2f5fa, #1e2532); }
  .temp { font-size: 34px; font-weight: 300; min-width: 2.4em; }
  .seg { margin-left: auto; display: inline-flex; padding: 3px; border-radius: 999px; background: light-dark(#dfe5ee, #323c4d); }
  .seg button { font: inherit; font-weight: 600; border: 0; padding: 6px 14px; border-radius: 999px; background: transparent; color: inherit; cursor: pointer; }
  .seg button[aria-pressed="true"] { background: light-dark(#ffffff, #4a5670); box-shadow: 0 1px 3px #0003; }
</style>
<div class="row">
  <span class="temp" id="t">19°</span><span>Valencia, now</span>
  <div class="seg"><button id="c" aria-pressed="true">°C</button><button id="f" aria-pressed="false">°F</button></div>
</div>
<script>
  const t = document.getElementById('t'), c = document.getElementById('c'), f = document.getElementById('f');
  const show = (fahr) => { t.textContent = (fahr ? 66 : 19) + '°'; c.setAttribute('aria-pressed', !fahr); f.setAttribute('aria-pressed', fahr); };
  c.onclick = () => show(false);
  f.onclick = () => show(true);
</script>
</html>

::: sandbox
**Scripts are off until you allow them.** Each frame is isolated, with no network and no access to this editor. With scripts off, the switch shows °C and does nothing. Switch on *Allow scripts* for the Live HTML plugin in the Manager, and it changes the number. Even then a frame cannot fetch anything.
:::

---

## How it's made

- **Each frame** is an `<html>` line, plain HTML and CSS, and a `</html>` line. One rule in the rules block hands those lines to the Live HTML plugin, which draws them in a frame of their own. Edit the HTML in Source and the frame redraws as you type.
- **Light and dark.** The frame knows the page's theme, so `light-dark(#f2f5fa, #1e2532)` in the CSS picks the first colour on a light page and the second on a dark one.
- **Movement** is plain CSS: the sun in the first sketch grows and shrinks with `@keyframes`.
- **The labels** `Sketch 1 · …`, the `project` line above the title and the dashed `::: sandbox` box are this page's own rules.

## Try it yourself

1. In the first sketch, change `#2f6fdf` in the card's gradient to `#7b4ae2`. The sky turns violet.
2. In the week, change Saturday's `18°` to `21°`. The frame redraws with the new high.
3. Switch on *Allow scripts* for the Live HTML plugin in the Manager. The °C and °F switch starts to work.
4. Change `project Weather app · forecast screen` to `project Weather app · round two`. The small capitals above the title follow.

Next: *Diagrams that move* animates pictures without any HTML; *Code snippets* shows code you read rather than run.