Attribute

cells

Make elements already on the page the cells of the doodle, or the letters and words of their text. The doodle styles them where they are and draws no grid of its own.

Syntax

  • <css-doodle cells="#list li"></css-doodle>
  • <css-doodle cells="h1::letters"></css-doodle>
  • <css-doodle cells="p::words"></css-doodle>
selector
A CSS selector, matched in the document, or in the shadow root that holds the doodle. The elements it selects become the cells, in document order.
::letters, ::words
At the end of the selector, splits the text of the selected elements into one cell per letter or word.

The doodle's rules style the cells, and :doodle styles their parent. When cells are added or removed, the doodle draws again with the same seed. Its rules weigh as much as an ID selector, so they override the page's class rules for the same property; mark a page rule !important to keep it. click:cell, click:update and pause() work on the cells; @content does not.

Examples

Every tag is clipped to a blob of its own: @R.t gives @shape a noise curve that closes on itself, a different one in each cell. With the range reversed, the same curve has dents where it had bumps, and the tags wobble between the two. The clip would cut a shadow off each tag, so :doodle puts it on their parent, #tags. Click a tag for new shapes:

<div id="tags">
  <span>css</span> <span>doodle</span> <span>grid</span>
  <span>shape</span> <span>svg</span> <span>pattern</span>
  <span>scene</span> <span>plot</span> <span>tile</span>
</div>
<css-doodle cells="#tags span" click:update>
  :doodle {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 2px;
    filter: drop-shadow(0 2px 1px #0003);
  }
  padding: 10px 14px;
  background: @p(#ebbf4d, #e6437d, #2a9d8f);
  clip-path: @shape(r: @R.t(.8, 1));
  animation: wobble 2s ease-in-out infinite alternate;
  @keyframes wobble {
    to { clip-path: @shape(r: @R.t(1, .8)); }
  }
</css-doodle>
css doodle grid shape svg pattern scene plot tile
:doodle { display: flex; flex-wrap: wrap; justify-content: center; gap: 2px; filter: drop-shadow(0 2px 1px #0003); } padding: 10px 14px; background: @p(#ebbf4d, #e6437d, #2a9d8f); clip-path: @shape(r: @R.t(.8, 1)); animation: wobble 2s ease-in-out infinite alternate; @keyframes wobble { to { clip-path: @shape(r: @R.t(1, .8)); } }

Letters and words

::letters or ::words at the end of the selector splits the text of the selected elements, one cell per letter or word. The markup inside is kept, and screen readers still read the text whole. Here :doodle styles the element being split.

Letters are inline, so give them display: inline-block before turning or moving them. Each letter below is cut from a different page:

<p id="note">meet me at the old mill at noon</p>
<css-doodle cells="#note::letters" seed="1795187752256">
  display: inline-block;
  margin: 2px 1px;
  padding: 0 .1em;
  font-size: @r(1.2rem, 1.6rem);
  text-transform: @p(none, uppercase);
  background: @p.palette(4);
  rotate: @r(±8deg);
  @random(.25) {
    color: #fff;
    background: #000;
  }
</css-doodle>

meet me at the old mill at noon

display: inline-block; margin: 2px 1px; padding: 0 .1em; font-size: @r(1.2rem, 1.6rem); text-transform: @p(none, uppercase); background: @p.palette(4); rotate: @r(±8deg); @random(.25) { color: #fff; background: #000; }

Functions in @keyframes give each cell keyframes of its own, so every letter drifts its own way and comes back:

<p id="drift">Every letter drifts its own way</p>
<css-doodle cells="#drift::letters">
  display: inline-block;
  font: 700 1.5rem sans-serif;
  animation: drift 3s ease-in-out infinite alternate;
  @keyframes drift {
    40% { translate: 0; rotate: 0; }
    100% {
      translate: @r(±.6em) @r(±.6em);
      rotate: @r(±.5turn);
    }
  }
</css-doodle>

Every letter drifts its own way

display: inline-block; font: 700 1.5rem sans-serif; animation: drift 3s ease-in-out infinite alternate; @keyframes drift { 40% { translate: 0; rotate: 0; } 100% { translate: @r(±.6em) @r(±.6em); rotate: @r(±.5turn); } }

Rows and columns

The page lays the cells out, and the doodle reads where they landed: @y is the row, and @x the place in that row, from 1. When a resize moves cells to other rows, the doodle draws again. @grid: 3 numbers them in rows of 3 instead.

CSS can't select the lines of a paragraph. Here the words come in line by line, each line from the left:

<p id="lines">
  Lines come in one at a time, each from the left,
  wherever the paragraph happens to wrap.
</p>
<css-doodle cells="#lines::words">
  display: inline-block;
  font: 1.125rem/1.4 sans-serif;
  animation: in 5s calc(@y * .4s + @x * .05s) infinite both;
  @keyframes in {
    0% { opacity: 0; translate: 0 .5em; }
    10%, 80% { opacity: 1; translate: 0; }
    90%, 100% { opacity: 0; }
  }
</css-doodle>

Lines come in one at a time, each from the left, wherever the paragraph happens to wrap.

display: inline-block; font: 1.125rem/1.4 sans-serif; animation: in 5s calc(@y * .4s + @x * .05s) infinite both; @keyframes in { 0% { opacity: 0; translate: 0 .5em; } 10%, 80% { opacity: 1; translate: 0; } 90%, 100% { opacity: 0; } }

Pointer

@udx and @udy measure from each cell's center, so every letter knows how far away the pointer is. Move it over the text:

<p id="rise">Move the pointer over these letters</p>
<css-doodle cells="#rise::letters">
  display: inline-block;
  font: 700 1.5rem sans-serif;
  --k: $(max(0, 1 - hypot(@udx, @udy) / 40));
  translate: 0 $(k * -12)px;
  color: color-mix(in oklab, #e6437d $(k * 100)%, #000);
</css-doodle>

Move the pointer over these letters

display: inline-block; font: 700 1.5rem sans-serif; --k: $(max(0, 1 - hypot(@udx, @udy) / 40)); translate: 0 $(k * -12)px; color: color-mix(in oklab, #e6437d $(k * 100)%, #000);

See also