---
title: "Render CSS box shadows to PNG in C#"
url: https://recursiveai.org/onerender/cookbook/render-css-box-shadows-to-image/
description: "Render outer and inset CSS box shadows with blur and spread from local HTML to PNG in C#, then measure which pixels the shadows reach."
version: "1.4.0"
---

# Render CSS box shadows to PNG in C#

**OneRender** is a free, open-source HTML and CSS rendering library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneRender.Image) · [GitHub](https://github.com/recursive-ai-oss/onerender-csharp) · [Docs](https://recursiveai.org/onerender/docs/) · [Download example (.zip)](https://recursiveai.org/onerender/cookbook/render-css-box-shadows-to-image/render-css-box-shadows-to-image.zip)

An outer shadow darkens the area around a box. An inset shadow darkens the inside edges.

```shell
dotnet add package RecursiveAI.OneRender.Image
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using OneRender.Image;
var image = HtmlRender.ToImage("""
<style>
div { position: absolute; top: 20px;
  width: 40px; height: 40px }
.outer { left: 20px;
  box-shadow: 4px 2px 6px 3px black }
.inner { left: 100px;
  box-shadow: inset 4px 2px 6px 3px black }
</style>
<div class="outer"></div>
<div class="inner"></div>
""", new() { Width = 160, Height = 80 });
File.WriteAllBytes("shadows.png",
    image.ToPng());
Console.WriteLine("Saved shadows.png");
```

Writes a 160 × 80 PNG with the outer shadow on the left and the inset shadow on the right.

![Two white squares: the left has a soft shadow outside its edges, and the right has a soft shadow inside its edges.](https://recursiveai.org/assets/onerender/render-css-box-shadows-to-image/shadows.png)

```text
Saved shadows.png
```

Unzip [the example](https://recursiveai.org/onerender/cookbook/render-css-box-shadows-to-image/render-css-box-shadows-to-image.zip), then `dotnet run`.

## The full program

The example uses inline HTML and CSS. It scans the two halves separately on a white background and reports the first and last nonwhite pixel in each direction. These are inclusive image coordinates, not CSS box edges.

```csharp
using OneRender.Image;

var image = HtmlRender.ToImage("""
<style>
body { margin: 0 }
div { position: absolute; top: 20px;
  width: 40px; height: 40px;
  background: white }
.outer { left: 20px;
  box-shadow: 4px 2px 6px 3px black }
.inner { left: 100px;
  box-shadow: inset 4px 2px 6px 3px black }
</style>
<div class="outer"></div>
<div class="inner"></div>
""", new() { Width = 160, Height = 80 });
File.WriteAllBytes("shadows.png", image.ToPng());
Console.WriteLine("Saved shadows.png");

// On this white page, a red channel below 255 marks a shadow.
ReportBounds("Outer", 0, 80);
ReportBounds("Inset", 80, 160);

void ReportBounds(string label, int startX, int endX)
{
    int left = endX, top = image.Height, right = -1, bottom = -1;
    for (int y = 0; y < image.Height; y++)
    for (int x = startX; x < endX; x++)
    {
        int red = image.Pixels[(y * image.Width + x) * 4];
        if (red == 255) continue;
        left = Math.Min(left, x);
        top = Math.Min(top, y);
        right = Math.Max(right, x);
        bottom = Math.Max(bottom, y);
    }
    Console.WriteLine(right < 0 ? $"{label}: no shadow pixels" :
        $"{label}: x={left}..{right}, y={top}..{bottom}");
}
```

```text
Saved shadows.png
Outer: x=13..74, y=11..72
Inset: x=100..139, y=20..59
```

The outer shadow extends past its box’s x range of `20..59`; the inset shadow stays in its own box. If an outer shadow is cut off at the edge of the PNG, leave more room between the element and the canvas edge.

## Variations

Replace the outer shadow with a hard-edged version by setting the third length, blur, to zero:

```css
box-shadow: 4px 2px 0 3px black;
```

The outer scan now prints `Outer: x=21..66, y=19..64`.

## Related recipes

- [Render CSS gradients to PNG](https://recursiveai.org/onerender/cookbook/render-css-gradients-to-image/)
- [Render CSS masks and clip paths](https://recursiveai.org/onerender/cookbook/render-css-masks-and-clip-paths/)

## The short answer

Use `box-shadow: 4px 2px 6px 3px black` for an outer shadow and add `inset` for an inner shadow. Render the HTML with `HtmlRender.ToImage` and save the pixels with `ToPng()`. Leave space on the canvas for the outer blur; the inset blur stays inside the padding box.

## How do you make an inset box shadow in CSS?

Add `inset` to the [box-shadow declaration](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/box-shadow). The two boxes in the example use identical offsets, blur, spread and color so the keyword is the only change to the shadow itself. For the outer shadow, OneRender shifts and expands the shadow shape, blurs it, and cuts out the border box. For the inset shadow, the offset moves the unshadowed hole, the positive spread shrinks it, and the padding box clips the blur. The example has no borders or padding, so the inset bounds coincide with the 40 × 40 element.

## What is the difference between blur and spread in a CSS box shadow?

The third length is blur and the fourth is spread. In `4px 2px 6px 3px`, the first two numbers shift the shape 4 px right and 2 px down. The spread changes the shape before blurring: positive spread grows an outer shadow, while negative spread shrinks it. For an inset shadow, positive spread leaves a smaller clear area inside the box. Blur softens that edge; setting blur to zero keeps the spread but removes the soft falloff. In the [OneRender display list](https://recursiveai.org/onerender/docs/display-list/#box-shadows), the blur has a Gaussian standard deviation of half the specified blur value.

## Why is my CSS box shadow cut off in the PNG?

A box shadow doesn’t enlarge its element’s layout dimensions. The image canvas is fixed by `Width` and `Height`; an outer shadow cannot appear beyond it. A parent’s overflow clip can also cut a shadow before it reaches the canvas edge. If the measured shadow touches an edge you didn’t intend, give the element more room and run the same scan again. Increasing only the PNG’s right and bottom edges won’t help a shadow that’s clipped on the top or left; move the element inward on those sides as well.

The scan assumes black shadows on a white background, so a drop below 255 in the red channel marks a changed pixel. On a colored page or with other artwork nearby, compare against a render of the same layout with `box-shadow: none` rather than treating every dark pixel as part of the shadow. The measured bounds depend on the canvas and the color comparison, not just the four CSS lengths.
