# Exploring legend-positioning with CSS and SVG foreignObject

**URL:** <https://talk.observablehq.com/t/exploring-legend-positioning-with-css-and-svg-foreignobject/10294>\
**Category:** Show and tell\
**Created:** [March 1, 2025, 12:59pm UTC](https://talk.observablehq.com/t/exploring-legend-positioning-with-css-and-svg-foreignobject/10294 "2025-03-01T12:59:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![stschiff](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/stschiff/32/9112_2.png) [@stschiff](https://talk.observablehq.com/u/stschiff)\
**Post date:** [March 1, 2025, 12:59pm UTC](https://talk.observablehq.com/t/exploring-legend-positioning-with-css-and-svg-foreignobject/10294/1 "2025-03-01T12:59:34Z")

</div>

Not sure whether this really fits into the “Show and Tell” category, but I did some exploration around how to position legends and include them into SVGs generated by Observable-Plot. I found the documentation around this lacking, so perhaps some others may find this helpful, too:

> **[Exploring Legend positioning with Observable-Plot and SVG/CSS modifications](https://observablehq.com/d/8b50ea5ec6948607)**
>
> So the background of this exploration are three issues that I currently have with the way legends are implemnted in Observable plot. Let's consider this example: The three issues here are: The legend doesn't have a label. As you can see in the code,...

---

<div class="post-metadata">

**Author:** ![espinielli](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/espinielli/32/142_2.png) [@espinielli](https://talk.observablehq.com/u/espinielli)\
**Post date:** [March 6, 2025, 8:32am UTC](https://talk.observablehq.com/t/exploring-legend-positioning-with-css-and-svg-foreignobject/10294/2 "2025-03-06T08:32:56Z")

</div>

I have done something similar using `serialize(chart)` from Mike.  
[Year to date CO2 Emissions variation per State (vs 2019) / Enrico Spinielli | Observable](https://observablehq.com/@espinielli/year-to-date-co2-emissions-variation-per-state)

---

<div class="post-metadata">

**Author:** ![stschiff](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/stschiff/32/9112_2.png) [@stschiff](https://talk.observablehq.com/u/stschiff)\
**Post date:** [March 6, 2025, 10:18am UTC](https://talk.observablehq.com/t/exploring-legend-positioning-with-css-and-svg-foreignobject/10294/3 "2025-03-06T10:18:16Z")

</div>

Huh, interesting. Thanks for sharing. I admit that I don’t fully get what `serialise` does. It seems to have come from a time when Charts were not readily downloadable as SVG at all, but that is now built right into the Observable Cells, as Mike comments on the top of [his notebook](https://observablehq.com/@mbostock/saving-svg), right?

---

<div class="post-metadata">

**Author:** ![mootari](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mootari/32/581_2.png) [@mootari](https://talk.observablehq.com/u/mootari)\
**Post date:** [March 6, 2025, 11:07am UTC](https://talk.observablehq.com/t/exploring-legend-positioning-with-css-and-svg-foreignobject/10294/4 "2025-03-06T11:07:35Z")

</div>

Instead of modifying the SVG externally I’d recommend to use a mark function:

```js
Plot.plot({
  marks: [
    Plot.dot(penguins, {
      x: "culmen_length_mm",
      y: "culmen_depth_mm",
      stroke: "species"
    }),
    // RenderFunction: https://github.com/observablehq/plot/blob/v0.6.17/src/mark.d.ts#L42-L60
    (index, scales, values, dims, context) => {
      const width = 100;
      const height = 80;
      const x = dims.width - dims.marginRight - width;
      const y = dims.height - dims.marginBottom - height;
      const label = htl.html`<div style="font:bold 12px/1.5 system-ui,sans-serif">Species`;
      return htl.svg`<foreignObject ${{width, height, x, y}}>${label}${Plot.legend(scales.scales)}`;
    }
  ],
})

```

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/observablehq/original/2X/8/8c759be020ede025b0f4756f5a41cd9acf490555.png)

---

<div class="post-metadata">

**Author:** ![stschiff](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/stschiff/32/9112_2.png) [@stschiff](https://talk.observablehq.com/u/stschiff)\
**Post date:** [March 10, 2025, 6:27am UTC](https://talk.observablehq.com/t/exploring-legend-positioning-with-css-and-svg-foreignobject/10294/5 "2025-03-10T06:27:11Z")

</div>

Brillant! Thanks for this. Yes, I was aware that this might be possible via the marks-interface, but I was a bit overwhelmed by the 5-argument function API. Super useful to have this example ready!

---

<div class="post-metadata">

**Author:** ![mootari](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mootari/32/581_2.png) [@mootari](https://talk.observablehq.com/u/mootari)\
**Post date:** [September 23, 2025, 6:22pm UTC](https://talk.observablehq.com/t/exploring-legend-positioning-with-css-and-svg-foreignobject/10294/6 "2025-09-23T18:22:05Z")

</div>

I’ve expanded my earlier example to cover both categorical and sequential scales:

> **[Positioning a Plot legend](https://observablehq.com/@mootari/placing-plot-legends)**
>
> Demonstrates how Plot legends can be included within the Plot as custom marks. Categorical An HTML legend that wraps and has no options for width or height. No label is rendered. Sequential An SVG legend with adjustable width and height. The label...
