CSS shape-image-threshold: Wrapping Transparent PNGs
The CSS shape-image-threshold property defines the exact
alpha channel boundary used to wrap inline text around non-rectangular
images. When paired with shape-outside, it reads the
transparency data of a raster image—such as a transparent PNG—and sets
an opacity cutoff level. Any pixel with an opacity exceeding this
threshold is treated as a solid obstruction, while pixels falling below
it allow text to flow through, creating dynamic, contoured text wrapping
directly derived from the image's transparent areas.
How the Extraction Mechanism Works
Browsers process transparent PNGs for text wrapping using pixel-level alpha evaluation. Here is the step-by-step mechanism of how the wrapping boundary is formed:
Reading the Alpha Channel: A standard PNG image contains RGB color channels and an alpha channel. The alpha channel stores the opacity level for every individual pixel on a scale from
0(completely transparent) to1(completely opaque), or0to255in 8-bit representation.Evaluating the Threshold: The
shape-image-thresholdaccepts a value between0.0(or0%) and1.0(or100%). The browser inspects each pixel in the image specified byshape-outside: url(...).- If a pixel's alpha value is strictly greater than the threshold, the browser marks that pixel as part of the shape boundary.
- If the alpha value is less than or equal to the threshold, the browser marks that area as empty space available for text layout.
Tracing the Contour: Once the pixels are classified as either solid or transparent based on the threshold, the browser's layout engine traces the perimeter of the solid pixel clusters. This creates a mathematical path (a geometric exclusion area) that mirrors the visible silhouette of the graphic.
Reflowing Inline Content: During layout calculation, the browser fits inline text line-by-line around the traced path, applying standard constraints like line height and margin offsets specified by
shape-margin.
Threshold Behavior in Practice
The default value for shape-image-threshold is
0.0. Under this default setting, any pixel that is not 100%
transparent—even a faint shadow with an alpha of 0.02—will
be included inside the exclusion boundary. This often leads to text
standing too far away from the visual subject of a PNG that features
soft, anti-aliased edges or drop shadows.
By increasing the value (for example, to 0.5), pixels
that are more than 50% transparent are discarded from the boundary. This
enables the text to tuck tightly against the solid parts of the graphic,
ignoring fuzzy, semi-transparent edges and producing a cleaner wrap.
Setting the value to 1.0 requires pixels to be completely
opaque to exclude text; any semi-transparent pixel will be ignored.
Technical Prerequisites
For shape-image-threshold to extract boundaries from a
PNG, the following criteria must be met:
- Floating Requirement: The element containing the
image must be floated (
float: leftorfloat: right), as CSS Shapes only affect content flowing around floated elements. - Valid Source: The image must be supplied directly
to the
shape-outsideproperty using aurl()value, a CSS gradient, or an SVG data URI. - CORS Compliance: Because the browser must read the
raw pixel data of the PNG to evaluate the alpha mask, the image must
satisfy Cross-Origin Resource Sharing (CORS) rules. If an image is
hosted on an external domain without proper
Access-Control-Allow-Originheaders, the browser cannot read the pixel data and will fail silently, defaulting back to standard rectangular wrapping.