CSS mask-border Property

The CSS mask-border property is a shorthand that uses an image or gradient to create a mask around the border of an element.

It divides a source image into nine regions, then uses those regions to mask the corners, edges, and optionally the center of an element. Unlike border-image, which paints a decorative border, mask-border controls the visibility of the element's rendered content.

mask-border Syntax

The mask-border shorthand combines a mask source with optional slicing, width, outset, repetition, and masking mode values.

Basic Syntax

selector {
  mask-border: source slice / width / outset repeat mode;
}

The example above illustrates the component order. The actual declaration uses an image or gradient and valid values for the remaining components.

Source and Slice

.element {
  mask-border: url("border-mask.png") 30;
}

Source, Slice, and Width

.element {
  mask-border: url("border-mask.png") 30 / 20px;
}

Source, Slice, and Repetition

.element {
  mask-border: url("border-mask.png") 30 round;
}

Complete Shorthand

.element {
  mask-border: url("border-mask.png") 30 fill / 20px / 5px round alpha;
}

Gradient Source

.element {
  mask-border: radial-gradient(circle, transparent 55%, black 60%) 30% fill / 20px;
}

Remove the Mask Border

.element {
  mask-border: none;
}

CSS-Wide Keywords

.element {
  mask-border: initial;
}
.element {
  mask-border: inherit;
}
.element {
  mask-border: unset;
}
.element {
  mask-border: revert;
}
.element {
  mask-border: revert-layer;
}

The mask-border shorthand has no single initial value. Each constituent property uses its own initial value.

Property Values

The shorthand accepts values from six constituent properties.

Component Accepted Values Description
mask-border-source none, url(), gradients, and other supported CSS images Specifies the source image used for the mask border.
mask-border-slice One to four nonnegative numbers or percentages, optionally followed by fill Defines how the source image is divided into regions.
mask-border-width One to four nonnegative lengths, percentages, numbers, or auto Sets the dimensions of the mask border regions.
mask-border-outset One to four nonnegative lengths or numbers Controls how far the mask border extends beyond the border box.
mask-border-repeat stretch, repeat, round, space Controls how the edge regions fill the available space.
mask-border-mode alpha, luminance Determines how the source image produces masking values.

Initial Values

Property Initial Value
mask-border-source none
mask-border-slice 0
mask-border-width auto
mask-border-outset 0
mask-border-repeat stretch
mask-border-mode alpha

Property Characteristics

Characteristic Value
Property type Shorthand
Inherited No
Applies to All elements, with SVG-specific applicability rules
Computed value Determined by the constituent longhand properties
Animation type Determined by the constituent longhand properties
Creates a stacking context Yes, when a mask border is applied
Browser availability Limited availability

How It Works

The mask-border property uses a nine-slice image technique similar to border-image. The source image is divided into four corners, four edges, and a center region.

The corner regions are placed at the corners of the element. The edge regions are stretched or repeated along the corresponding sides. The center region is used only when fill is specified.

Nine-Slice Layout

+--------------+--------------+--------------+
|   Top Left   |      Top     |   Top Right  |
+--------------+--------------+--------------+
|     Left     |    Center    |     Right    |
+--------------+--------------+--------------+
|  Bottom Left |    Bottom    | Bottom Right |
+--------------+--------------+--------------+

Basic Example

.masked-panel {
  mask-border: url("border-mask.png") 30 fill / 20px stretch;
}

The source image is sliced 30 units inward from each edge. The resulting regions are applied to a 20-pixel-wide mask border, with the edge regions stretched to fit.

Why fill Matters

Without fill, the center slice is discarded. The element's center may become transparent if no other masking layer reveals it.

Important Consideration

A mask border changes the visible portions of an element. It does not create additional layout space or automatically increase the element's CSS border width.

Mask Border Source

The mask-border-source component identifies the image used to create the mask border.

PNG Image

.element {
  mask-border: url("border-mask.png") 30 fill / 20px;
}

Transparent portions of the image hide corresponding parts of the element when alpha masking is used.

SVG Image

.element {
  mask-border: url("border-mask.svg") 25 fill / 18px;
}

An SVG image can provide scalable source artwork for the mask border.

CSS Gradient

.element {
  mask-border: linear-gradient(135deg, black, transparent) 25% fill / 20px;
}

CSS gradients are valid image sources, although the resulting effect depends on how the gradient is sliced and applied.

No Source

.element {
  mask-border: none;
}

The none value removes the mask border source.

Important Consideration

The source image should be designed with its intended slicing regions in mind. A poorly aligned source can produce distorted corners or unexpected transparency.

Mask Border Slicing

The mask-border-slice component determines where the source image is divided into nine regions.

One Slice Value

.element {
  mask-border: url("border-mask.png") 30;
}

The image is sliced 30 units inward from all four edges.

Two Slice Values

.element {
  mask-border: url("border-mask.png") 20 40;
}

The first value applies to the top and bottom slices. The second applies to the left and right slices.

Four Slice Values

.element {
  mask-border: url("border-mask.png") 10 20 30 40;
}

The values apply to the top, right, bottom, and left slices, respectively.

Percentage Slices

.element {
  mask-border: url("border-mask.png") 25%;
}

Percentage values are measured relative to the dimensions of the source image.

Preserve the Center Region

.element {
  mask-border: url("border-mask.png") 30 fill / 20px;
}

The fill keyword preserves the center slice so it can contribute to the resulting mask.

Important Consideration

Unitless slice numbers refer to coordinates in the source image. They are not CSS pixel lengths.

Mask Border Width

The mask-border-width component controls the width of the regions where the sliced mask image is applied.

Fixed Width

.element {
  mask-border: url("border-mask.png") 30 / 20px;
}

Different Widths

.element {
  mask-border: url("border-mask.png") 30 / 10px 20px 30px 40px;
}

The widths apply to the top, right, bottom, and left regions.

Number Values

.element {
  border: 10px solid transparent;
  mask-border: url("border-mask.png") 30 / 2;
}

A unitless number multiplies the corresponding CSS border width. In this example, 2 produces a mask border width of 20 pixels on each side.

Automatic Width

.element {
  mask-border: url("border-mask.png") 30 / auto;
}

The auto value determines the mask border width using the applicable image slice and border sizing rules.

Important Consideration

The mask border width controls masking geometry, not the element's layout dimensions. It does not replace the CSS border-width property.

Mask Border Outset

The mask-border-outset component specifies how far the mask border extends beyond the element's border box.

Fixed Outset

.element {
  mask-border: url("border-mask.png") 30 / 20px / 8px;
}

Different Outset Values

.element {
  mask-border: url("border-mask.png") 30 / 20px / 5px 10px 15px 20px;
}

The values apply to the top, right, bottom, and left sides.

Number Values

.element {
  border: 10px solid transparent;
  mask-border: url("border-mask.png") 30 / 20px / 2;
}

A unitless outset value multiplies the corresponding CSS border width. With a 10-pixel border, 2 represents a 20-pixel outset.

No Outset

.element {
  mask-border: url("border-mask.png") 30 / 20px / 0;
}

Important Consideration

Outset values do not increase the element's layout box. The visible result can still be affected by overflow clipping and other rendering constraints.

Mask Border Repetition

The mask-border-repeat component determines how the edge slices fill the available border mask regions.

Value Description
stretch Stretches each edge slice to fill its region.
repeat Repeats the edge slices, allowing partial repetitions.
round Repeats the slices and adjusts their dimensions to fit a whole number of repetitions.
space Distributes whole repetitions with spacing when possible.

Stretch

.element {
  mask-border: url("border-mask.png") 30 / 20px stretch;
}

Repeat

.element {
  mask-border: url("border-mask.png") 30 / 20px repeat;
}

Round

.element {
  mask-border: url("border-mask.png") 30 / 20px round;
}

Space

.element {
  mask-border: url("border-mask.png") 30 / 20px space;
}

Different Horizontal and Vertical Behavior

.element {
  mask-border: url("border-mask.png") 30 / 20px round stretch;
}

The first repetition value controls the top and bottom edges. The second controls the left and right edges.

Important Consideration

The initial repetition value is stretch. For patterned border masks, round often provides more consistent results than stretching the pattern.

Mask Border Mode

The mask-border-mode component determines whether the source image's alpha channel or luminance values control visibility.

Alpha Masking

.element {
  mask-border: url("border-mask.png") 30 fill / 20px alpha;
}

With alpha masking, opaque areas reveal the element and transparent areas hide it. The color of an opaque pixel does not determine its visibility.

Luminance Masking

.element {
  mask-border: url("border-mask.png") 30 fill / 20px luminance;
}

With luminance masking, lighter areas reveal more of the element and darker areas reveal less. Alpha transparency also contributes to the result.

Alpha vs. Luminance

Source Pixel Alpha Mode Luminance Mode
Opaque white Visible Visible
Opaque black Visible Hidden
Transparent Hidden Hidden
Partially transparent Partially visible Depends on luminance and transparency

Important Consideration

The initial mode is alpha. The standardized mask-border-mode property has very limited browser support, so luminance-based border masking should not be relied upon without compatibility testing.

Shorthand and Longhand Properties

The mask-border shorthand sets six related CSS properties.

Shorthand Example

.element {
  mask-border: url("border-mask.png") 30 fill / 20px / 5px round alpha;
}

Equivalent Longhand Declarations

.element {
  mask-border-source: url("border-mask.png");
  mask-border-slice: 30 fill;
  mask-border-width: 20px;
  mask-border-outset: 5px;
  mask-border-repeat: round;
  mask-border-mode: alpha;
}

Resetting Omitted Values

.element {
  mask-border-width: 30px;
  mask-border: url("border-mask.png") 20;
}

The later shorthand resets mask-border-width to auto because no width is supplied.

Override a Shorthand Value

.element {
  mask-border: url("border-mask.png") 30 fill / 20px round;
  mask-border-width: 25px;
}

The later longhand declaration overrides the width established by the shorthand.

Interaction with mask

.element {
  mask-border: url("border-mask.png") 30 fill / 20px;
  mask: linear-gradient(black, transparent);
}

The mask shorthand resets all mask-border-* properties, removing the previously declared mask border.

Correct Declaration Order

.element {
  mask: linear-gradient(black, transparent);
  mask-border: url("border-mask.png") 30 fill / 20px;
}

Declare mask-border after mask when both effects are required.

mask-border vs. border-image

The mask-border and border-image properties use similar nine-slice techniques, but their purposes are different.

Feature mask-border border-image
Purpose Controls visibility through a mask Paints a decorative border image
Image slicing Yes Yes
Alpha transparency Controls element visibility Controls transparency of the painted border image
Luminance mode Defined by the specification Not applicable
Center fill Can contribute to the element's mask Can paint the center region
Browser support Limited Widely supported

Mask Border

.element {
  mask-border: url("border-mask.png") 30 fill / 20px round;
}

Border Image

.element {
  border: 20px solid transparent;
  border-image: url("border-image.png") 30 round;
}

The first declaration masks the element. The second paints an image along its border.

Important Consideration

Use border-image when the goal is to display decorative artwork. Use mask-border when the goal is to control transparency along the border and, optionally, the center.

mask-border Example

This example uses a radial gradient to create a rectangular frame mask. The fill keyword is intentionally omitted so the center slice does not contribute to the mask.

Because browser support is limited, the example includes the older -webkit-mask-box-image property for compatible browsers and the standardized mask-border declaration.

HTML Example

<div class="mask-border-demo">
  <div class="mask-border-box">
    <span>Masked Border</span>
  </div>
</div>

CSS Example

.mask-border-demo {
  padding: 15px;
  background-color: #f1f5f9;
}
.mask-border-box {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 160px;
  padding: 25px;
  background-color: #2563eb;
  color: #fff;
  font-weight: bold;
  text-align: center;
  -webkit-mask-box-image: radial-gradient(circle, transparent 55%, black 60%) 30% / 20px stretch;
  mask-border: radial-gradient(circle, transparent 55%, black 60%) 30% / 20px stretch;
}
Browser Result
Masked Border

In a supporting browser, the mask retains the outer regions of the blue box while masking the center. The text may also disappear because the mask affects the rendered element, including its descendants.

Browsers without support for either declaration display the original blue box without the mask border.

Preserve the Center

.mask-border-box {
  -webkit-mask-box-image: radial-gradient(circle, transparent 55%, black 60%) 30% fill / 20px stretch;
  mask-border: radial-gradient(circle, transparent 55%, black 60%) 30% fill / 20px stretch;
}

The fill keyword includes the center slice in the mask. The resulting transparency still depends on the gradient values in that region.

Increase the Mask Border Width

.mask-border-box {
  -webkit-mask-box-image: radial-gradient(circle, transparent 55%, black 60%) 30% / 35px stretch;
  mask-border: radial-gradient(circle, transparent 55%, black 60%) 30% / 35px stretch;
}

The border mask regions become wider.

Remove the Mask Border

.mask-border-box {
  -webkit-mask-box-image: none;
  mask-border: none;
}

The element returns to its unmasked appearance, assuming no other masking effects apply.

Important Consideration

The prefixed property is a legacy implementation and is not identical to every feature of the standardized shorthand. Test the result in each browser you intend to support.

Common Mistakes

Expecting Universal Browser Support

The standardized mask-border shorthand is not supported consistently across major browsers. A fallback is necessary when the masking effect is essential.

Confusing mask-border with border-image

The mask-border property changes visibility. It does not paint a decorative border image.

Forgetting the Center Slice

Without fill, the center slice is discarded. This can hide the element's central content.

Using Negative Values

.element {
  mask-border: url("border-mask.png") -20 / -10px;
}

Negative slice and width values are invalid. Use nonnegative values.

Confusing Slice Values with Width Values

.element {
  mask-border: url("border-mask.png") 30 / 20px;
}

The value before the slash defines the image slices. The value after the slash defines the mask border width.

Using Black to Hide Content in Alpha Mode

Opaque black is fully visible in alpha masking. Use transparent regions to hide content or select luminance masking where supported.

Overwriting the Mask Border with mask

.element {
  mask-border: url("border-mask.png") 30 fill / 20px;
  mask: linear-gradient(black, transparent);
}

The later mask shorthand resets the mask border properties.

Expecting Mask Borders to Change Layout

Mask border width and outset do not automatically change the element's layout dimensions.

Ignoring Content Visibility

A mask border can hide text and other descendants. Do not assume it affects only the background or border decoration.

Important Considerations

Keep the following points in mind when using mask-border:

  • The property is a shorthand for six mask border properties.
  • It uses a nine-slice technique to apply a masking image.
  • The initial mask border source is none.
  • The property is not inherited.
  • The source can be a supported CSS image or gradient.
  • The source image is divided into four corners, four edges, and a center region.
  • Slice values determine where the source image is divided.
  • The fill keyword includes the center region.
  • Without fill, the center slice is discarded.
  • Width values determine the dimensions of the mask border regions.
  • Outset values control how far the mask extends beyond the border box.
  • Repetition values control how the edge slices fill their regions.
  • The initial repetition value is stretch.
  • The initial masking mode is alpha.
  • Alpha masking uses transparency to determine visibility.
  • Luminance masking uses brightness and transparency.
  • Mask borders can affect the rendered element and its descendants.
  • Mask borders do not automatically change layout dimensions.
  • Omitted shorthand components reset to their initial values.
  • The mask shorthand resets mask border properties.
  • Declare mask-border after mask when both are needed.
  • Browser support for the standardized property is limited.
  • Some browsers support the legacy -webkit-mask-box-image property.
  • The legacy prefixed implementation does not support every standardized feature.
  • Unsupported browsers may display the element without the intended mask.
  • Use a fallback when the effect is essential to the design.
  • Do not rely on masking alone to hide content from assistive technologies.

Browser Support

The CSS mask-border property has Limited availability and is not part of Baseline because support is incomplete across major browsers.

Some browsers provide related functionality through the legacy -webkit-mask-box-image property. However, the prefixed implementation does not necessarily support all standardized features, including mask-border-mode.

For current compatibility information, visit Can I Use? and search for mask-border.

For production layouts, provide a usable appearance when mask borders are unsupported.

Specifications

The mask-border shorthand is defined in CSS Masking Module Level 1.

CSS Masking Module Level 1: mask-border

The specification defines the mask border source, slicing, sizing, outset, repetition, masking mode, and shorthand behavior.

For additional documentation and examples, consult MDN Web Docs.

MDN Web Docs: mask-border