CSS border-shape for custom borders and shadows

Post on X
Copy URL
Share

The CSS border-shape property lets you give an element’s border a custom shape. You can draw circles and polygons, or combine straight lines and curves in the same shape.

Adding a border and shadow around a cutout photo used to require workarounds such as layering several elements. With border-shape, a single element can clip the photo, draw its border, and cast a shadow. Change the shape and the border and shadow follow it.

Start with a simple hexagon, then apply the same idea to photos, speech bubbles, and navigation links.

Draw a hexagon with border-shape

To draw a hexagon, use border to set the line width and color, and border-shape to define the shape.

The polygon() function takes a pair of coordinates for each vertex. The first pair, 50% 0%, marks the midpoint of the box’s top edge. The remaining vertices follow clockwise.

<div class="box"></div>
.box {
  width: 120px;
  aspect-ratio: 1;
  border: 5px solid #e879a0;
  /* Connect the six vertices clockwise, starting at the top. */
  border-shape: polygon(
    50% 0%, 100% 25%, 100% 75%,
    50% 100%, 0% 75%, 0% 25%
  );
}

Numbers 1 through 6 match the coordinate pairs in polygon() to the vertices of the hexagon, clockwise from the top.

With one shape, the browser draws a line along its outline. This is called stroke mode. Use circle() for a circle, ellipse() for an ellipse, or shape() for a mix of straight lines and curves.

Demo 1. Add a border and shadow to a hexagonal photo

The demo uses four techniques to crop the same photo into a hexagon. Toggle Border and Shadow and watch the edges of each photo. Which techniques give you a hexagonal border? Which give you a hexagonal shadow?

Four hexagonal photos. The border-shape example shows both a border and a shadow. The clip-path and mask-image examples show neither. Applying drop-shadow() to the parent adds a shadow around the hexagon.

Try the demos in Chrome or Edge 147 or later.

In the clip-path and mask-image examples, the rectangular border and shadow fall outside the hexagon and disappear. The clip-path + drop-shadow() example applies drop-shadow() to the clipped element’s parent using the filter property. The parent then casts a hexagonal shadow.

For the border-shape example, give the photo’s wrapper a hexagonal border. Set overflow: hidden to clip any part of the photo that extends beyond it. On that same element, use border to style the border and box-shadow to add the shadow.

.photo.shaped {
  /* Size and image styles omitted */
  border-shape: polygon(
    24% 7%, 75% 7%, 96% 51%, 77% 89%, 31% 94%, 4% 60%
  ) border-box;
  overflow: hidden;
  border: 5px solid #ece8ff;
  box-shadow: 8px 8px #b7aff5;
}

Demo 2. Extend a speech bubble’s shadow around its tail

An earlier speech bubble design uses the ::before and ::after pseudo-elements to draw a triangular tail and layer it over a rectangular body. You can see the original example in HTML・CSSで実装するかわいいフキダシのアイデア.

Look for border + ::before + ::after in the demo to see that design alongside a bubble drawn with border-shape. Both bubbles have the same box-shadow on the body.

/* Both speech bubbles use the same shadow. */
.fukidashi-01-06,
.shaped {
  box-shadow: 4px 6px 0 #b7aff5;
}

The original bubble casts a rectangular shadow with a flat bottom edge. Toggle box-shadow and look beneath the tail. In the border-shape example, the shadow follows the triangle all the way to its tip.

On the left, the border-shape bubble casts a shadow beneath its triangular tail. On the right, the shadow of the bubble built with pseudo-elements has a flat bottom edge.

In the original, the body and tail each have their own border. With border-shape, the shape() function traces the body and tail as one continuous outline. A single border declaration sets the color and width for both.

In the next demo, select Left, Center, or Right to move the tail. The CSS custom property --tip-x sets its horizontal position.

The triangular tail and its shadow move together to the left, center, or right of the speech bubble.

To draw the tail, start at the right end of its base, draw a line to the tip, then return to the left end of the base. Add half the tail’s width to --tip-x to get the right end, and subtract the same amount to get the left end.

The coordinates are relative to the border-box, which includes the border. Try changing --tip-x to 75% in the code below. The tip moves three-quarters of the way across the box from its left edge.

<p class="bubble">Copy link</p>
.bubble {
  border: 2px solid #61519a;
  --tip-x: 50%;
  --tip-half-width: 9px;
  --body-bottom: calc(100% - 1px);
  /* Other styles omitted */

  border-shape: shape(
    from 1px 1px,
    hline to calc(100% - 1px),
    vline to var(--body-bottom),
    /* Trace the tail from its right base corner to the tip, then to its left base corner. */
    hline to calc(var(--tip-x) + var(--tip-half-width)),
    line to var(--tip-x) calc(var(--body-bottom) + 11px),
    line to calc(var(--tip-x) - var(--tip-half-width)) var(--body-bottom),
    hline to 1px,
    close
  ) border-box;
}

Change the shape of the bubble

The same technique works for ovals and even clouds. The border and shadow follow every curve of the bubble.

Switch between Oval, Box, and Cloud in the demo to see how the shadow changes with each shape.

An oval, a rounded rectangle, and a cloud, each with a shadow that follows the outline of the speech bubble.

Demo 3. Draw a focus ring along slanted edges

In this navigation bar, the links meet at an angle.

Press Tab until the Type link has focus. The outline property draws the focus ring that tells you which link has keyboard focus. The ring follows the shape defined by border-shape, including the slanted edges.

Press Enter or Space to switch to the section for the focused link.

The Palette section is still visible while Type has keyboard focus. The focus ring follows the slanted edges of the Type link.

Here’s the Type link from the horizontal layout. When you tab to it, the link matches the :focus-visible pseudo-class and the focus ring appears. The outline-offset property leaves a gap between the ring and the border so the ring is easier to see.

<nav>
  <a href="#type">Type</a>
</nav>
nav a {
  /* Other styles omitted */
  border: 1px solid #9389a4;
  border-shape: polygon(
    12px 0, 100% 0, calc(100% - 12px) 100%, 0 100%
  ) border-box;
}

nav a:focus-visible {
  outline: 2px solid #5141c3;
  outline-offset: 3px;
}

Demo 4. Use two shapes to vary the border width

You can also give border-shape two shapes, one for the outer edge of the border and one for the inner edge. The browser fills the area between them. This is called fill mode.

The farther apart the two edges are, the thicker the border. For example, a circle on the outside and a wide ellipse on the inside give you a border that’s thicker at the top and bottom than on the sides.

List the outer shape first and the inner shape second, separated by a space. Try replacing the border-shape value in the hexagon example with this one.

.box {
  /* Other styles omitted */
  border-shape: circle(50%) ellipse(40% 30%);
}

The camera viewfinder in the demo uses the same technique. Its outer frame is an octagon, and its window is a wide opening with curved sides. Drag the Pan slider to move the photo left or right. Uncheck Clip to see the parts of the photo that extend beyond the window.

Two camera viewfinders frame the same photo. In the border-shape version, one element draws the frame, clips the photo, and casts the shadow.

Both viewfinders look the same, but their markup differs. The border-shape version uses a single div around the photo for the frame, clipping, and shadow. The clip-path version uses three div elements. Keeping the frame and window on separate elements makes it easy to adjust them independently, for instance by moving just the window.

<!-- border-shape version -->
<div class="viewfinder shaped">
  <img src="../01/beach.webp" alt="Waves breaking on a beach at sunrise" />
</div>

<!-- clip-path version -->
<div class="viewfinder clipped">
  <div class="shell"></div>
  <div class="window">
    <img src="../01/beach.webp" alt="Waves breaking on a beach at sunrise" />
  </div>
</div>

The following CSS shows the styles with Clip checked. Both versions use --outer for the frame and --inner for the window.

.viewfinder-demo {
  /* Octagonal outer frame */
  --outer: polygon(
    17% 3%, 83% 3%, 98% 27%, 98% 73%,
    83% 97%, 17% 97%, 2% 73%, 2% 27%
  );
  /* Window outline */
  --inner: shape(
    from 26% 18%,
    hline to 74%,
    curve to 88% 38% with 80% 18% / 87% 30%,
    curve to 88% 62% with 90% 44% / 90% 56%,
    curve to 74% 82% with 87% 70% / 80% 82%,
    hline to 26%,
    curve to 12% 62% with 20% 82% / 13% 70%,
    curve to 12% 38% with 10% 56% / 10% 44%,
    curve to 26% 18% with 13% 30% / 20% 18%,
    close
  );
}

The code below offsets the shadow by 7px to the right and 7px down, just as the demo does on a wide screen.

.shaped {
  /* Positioning and other styles omitted */
  border-color: #b7aff5;
  border-shape: var(--outer) border-box var(--inner) border-box;
  overflow: hidden;
  box-shadow: 7px 7px 0 #5141c3;
}

In the clip-path version, .shell::before draws the outer frame, .shell casts the shadow, and .window clips the photo to the shape of the opening. Open Compare CSS in the demo to compare the two versions.

.shell {
  /* Positioning and other styles omitted */
  filter: drop-shadow(7px 7px 0 #5141c3);

  &::before {
    content: "";
    background: #b7aff5;
    clip-path: var(--outer);
  }
}

.window {
  clip-path: var(--inner);
}

Browser support

Chrome and Edge have supported border-shape since version 147, released in April 2026.

See Can I use… for browser support.

Conclusion

As of December 2026, border-shape has two limitations to keep in mind.

  • border-radius and corner-shape have no effect on the corners of a shape drawn with border-shape. To round the corners, draw them as curves with shape().
  • A shaped border renders as a solid line even when border-style is set to dashed or dotted.

With border-shape, the same outline guides both the border and the shadow. Move a speech bubble’s tail, for example, and its border and shadow move with it. That makes even a cloud-shaped bubble straightforward to style in CSS.

If you only want to change the corners of an element, corner-shape lets you bevel them or scoop them inward. For examples, see CSS corner-shape for customize corner styles.

References

Find ICS MEDIA articles more easily on Google

Add ICS MEDIA as a preferred source to see our articles more often in Top Stories and AI Search.

Add as a preferred source on Google
Share on social media
Your shares help us keep the site running.
Post on X
Copy URL
Share
IWAMA Hina

Front-end engineer. Studied graphic design as a student. Loves reading and cute things.

Articles by this staff

About this site

ICS MEDIA is operated by ICS and is an owned media platform. We publish practical UI design and front-end knowledge every week.

About ICS

ICS is a web production company specializing in interaction design, creating expressive on-screen media with modern web technologies.

We are hiring teammates who enjoy creating interaction content and growing skills together.

Always up-to-date

We keep even older articles maintained so the information stays useful over time.

This site supports AI

ICS is actively adopting AI in front-end development. We support the technologies below to make this site easier for AI to use. Give them a try.

  • WebMCP

    WebMCP lets AI agents use website features. On ICS MEDIA, agents can use it to find and read articles.

    ICS MEDIA participates in the origin trial, so WebMCP works in Chrome without enabling any flags.

  • llms.txt

    llms.txt is a Markdown index that summarizes a site and links to its content.

    It helps AI find and read relevant articles.