Image outlines

A faint line just inside the edge of an image, so it doesn't blend into a page of the same color. Add layerStyle="imageOutline" to any image or avatar.

When the edges of an image are close in color to the page, the image blends into it. A white product shot on a white page, or a night sky on a dark one, loses its edge, and the eye can't tell where the image stops.

The imageOutline layer style draws a 1px line just inside the image: black at 10% in light mode, white at 10% in dark mode. It's too faint to read as a border, but it gives every image the same defined edge.

Storm at Sea on a Moonlit Night by Ivan Aivazovsky
No outline
Storm at Sea on a Moonlit Night by Ivan Aivazovsky
layerStyle="imageOutline"

Ivan Aivazovsky, Storm at Sea on a Moonlit Night

Usage

The Image component and Avatar.Image already have it: the image and avatar recipes apply the layer style by default. Turn it off on one element with outlineStyle="none".

<Image src="/sunflowers.jpg" alt="Sunflowers by Vincent van Gogh" rounded="l3" />
<Image src="/van-gogh-signature.png" alt="Vincent van Gogh's signature" outlineStyle="none" />

The layer style ships with @isbatak/panda-ds, so there is nothing to install. Add it to any other image.

<styled.img src="/water-lilies.jpg" alt="Water Lilies by Claude Monet" rounded="l3" layerStyle="imageOutline" />

Or from css(), next to your other styles.

import { css } from "styled-system/css"

export function Painting() {
  return (
    <img
      src="/the-magpie.jpg"
      alt="The Magpie by Claude Monet"
      className={css({ layerStyle: "imageOutline", rounded: "l3" })}
    />
  )
}

The outline follows the element's border-radius, so it works on rounded images and circles without extra styles.

How it works

The layer style sets four properties.

imageOutline: {
  value: {
    outlineWidth: "1px",
    outlineStyle: "solid",
    outlineOffset: "-1px",
    outlineColor: "color-mix(in srgb, {colors.black} calc(var(--image-outline-opacity, 0.1) * 100%), transparent)",
    _dark: {
      outlineColor: "color-mix(in srgb, {colors.white} calc(var(--image-outline-opacity, 0.1) * 100%), transparent)",
    },
  },
}
  • outline-offset: -1px pulls the outline inside the image, so it paints over the edge pixels instead of around them.
  • color-mix() mixes the black or white token with transparent, so the opacity can come from a CSS variable.
  • _dark swaps black for white. A dark line disappears on a dark page, and a light one would glow on a light page.

Why not a border

A border takes up space. On a sized image it either grows the box by 2px or shrinks the picture inside it, and toggling it shifts the layout around it. An outline is painted on top and takes up no space. An inset box-shadow does the same, but it's drawn under an <img>'s content, so the image covers it.

<styled.img src="/the-night-cafe.jpg" borderWidth="1px" /> {/* adds 2px to the box */}
<styled.img src="/the-night-cafe.jpg" layerStyle="imageOutline" /> {/* takes up no space */}

Strength

About 10% is a good default. Below 5%, the line disappears against pale backgrounds. Above 20%, it stops reading as an edge and starts to look like a frame. Drag the slider to compare.

Water Lilies by Claude Monet
Claude Monet, Water Lilies
0.10

A defined edge that doesn't read as a border.

Set --image-outline-opacity to change it. It takes a number from 0 to 1 and inherits like any custom property, so you can set it on one image or on a whole gallery.

<styled.div css={{ "--image-outline-opacity": "0.15" }}>
  <styled.img src="/the-starry-night.jpg" alt="The Starry Night by Vincent van Gogh" layerStyle="imageOutline" />
  <styled.img src="/impression-sunrise.jpg" alt="Impression, Sunrise by Claude Monet" layerStyle="imageOutline" />
</styled.div>

Light and dark modes can need different values. A dark page often needs a little more contrast. Set the variable per condition.

<styled.img
  src="/the-ninth-wave.jpg"
  alt="The Ninth Wave by Ivan Aivazovsky"
  layerStyle="imageOutline"
  css={{ "--image-outline-opacity": { base: "0.1", _dark: "0.14" } }}
/>

Avatars

Avatars benefit most. They're small and round, and they often have a white or pale background, so without an outline they look like a gap in the page. The avatar recipe applies the layer style to Avatar.Image by default, and the image already takes the avatar's radius, so the outline stays round. Switch between the two and watch the edges of the bright and dark photos.

VG
CM
IA
BM
RR
ED

To turn it off for one avatar, remove the outline or set its opacity to 0.

<Avatar.Root>
  <Avatar.Fallback name="Rembrandt van Rijn" />
  <Avatar.Image src="/rembrandt.jpg" alt="Rembrandt van Rijn" outlineStyle="none" />
</Avatar.Root>

In plain CSS

The same technique outside Panda, for reference.

.image-outline {
  outline: 1px solid rgb(0 0 0 / var(--image-outline-opacity, 0.1));
  outline-offset: -1px;
}

.dark .image-outline {
  outline-color: rgb(255 255 255 / var(--image-outline-opacity, 0.1));
}

FAQ

Credits

From Jakub Krehel's Details that make interfaces feel better and Gustavo Fior's Image outlines.

The paintings and portraits are in the public domain, from Wikimedia Commons: