CSS mask-border-width Property

The CSS mask-border-width property sets the widths of the four regions used to render an element's mask border image.

It accepts one to four nonnegative lengths, percentages, numbers, or the keyword auto. The property determines how much space the sliced mask image occupies along each side of the mask border area, independently of the element's actual CSS border widths.

mask-border-width Syntax

The mask-border-width property accepts one to four values. Each value can be a length, percentage, nonnegative number, or auto.

Basic Syntax

selector {
  mask-border-width: auto;
}

Length Value

.element {
  mask-border-width: 20px;
}

Percentage Value

.element {
  mask-border-width: 10%;
}

Number Value

.element {
  mask-border-width: 2;
}

Automatic Width

.element {
  mask-border-width: auto;
}

Different Widths

.element {
  mask-border-width: 10px 20px 30px 40px;
}

Mixed Values

.element {
  mask-border-width: 10px 5% 2 auto;
}

CSS-Wide Keywords

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

The initial value is auto, which determines the width from the corresponding source image slice when possible.

Property Values

Value Description
<length> Specifies an explicit nonnegative width using a CSS length such as px, em, or rem.
<percentage> Specifies a width relative to the corresponding dimension of the mask border image area.
<number> Specifies a nonnegative multiplier of the corresponding CSS border-width.
auto Uses the corresponding source image slice's intrinsic dimension when available, otherwise the corresponding CSS border width.
initial Resets the property to auto.
inherit Inherits the computed value from the parent element.
unset Resets the property to auto because it is not inherited.
revert Reverts the property according to cascade origin rules.
revert-layer Reverts the property according to cascade layer rules.

Property Characteristics

Characteristic Value
Initial value auto
Inherited No
Applies to All elements, with SVG-specific applicability rules
Percentages Relative to the width or height of the mask border image area
Computed value As specified, with relative lengths converted into absolute lengths
Animation type Discrete
Accepted values One to four lengths, percentages, numbers, or auto
Negative values Not permitted
Shorthand property mask-border
Browser availability Limited availability

How It Works

A mask border is constructed from an image divided into nine regions. The four corners are positioned at the corners of the mask border area, while the edge regions fill the spaces between them.

The mask-border-width property determines the thickness of the regions where those sliced images are rendered.

Nine-Slice Image

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

Step 1: Select a Source

.masked-panel {
  mask-border-source: url("border-mask.png");
}

The browser loads the image that will provide the mask border regions.

Step 2: Divide the Image

.masked-panel {
  mask-border-source: url("border-mask.png");
  mask-border-slice: 30 fill;
}

The image is divided using offsets of 30. The fill keyword retains the center region.

Step 3: Set the Rendered Width

.masked-panel {
  mask-border-source: url("border-mask.png");
  mask-border-slice: 30 fill;
  mask-border-width: 20px;
}

The sliced corner and edge regions are scaled and positioned using 20-pixel mask border widths.

Step 4: Control Edge Repetition

.masked-panel {
  mask-border-source: url("border-mask.png");
  mask-border-slice: 30 fill;
  mask-border-width: 20px;
  mask-border-repeat: round;
}

The edge slices repeat along the sides, with their dimensions adjusted to fit whole repetitions.

Step 5: Apply the Mask

The browser uses the assembled mask border image to control the visibility of the element. The source image's alpha or luminance values determine the masking effect.

Important Consideration

The mask border width is part of the masking effect. It does not increase the element's CSS box dimensions or reserve additional layout space.

Length Values

Length values specify explicit widths for the rendered mask border regions.

Pixel Width

.element {
  mask-border-width: 20px;
}

All four mask border regions use a width of 20 pixels.

Relative Font Units

.element {
  mask-border-width: 1.5em;
}

The width is calculated relative to the element's font size.

Root-Relative Units

.element {
  mask-border-width: 1rem;
}

The width is calculated relative to the root element's font size.

Different Horizontal and Vertical Widths

.element {
  mask-border-width: 10px 25px;
}

The top and bottom regions are 10 pixels thick. The left and right regions are 25 pixels thick.

Different Widths on Every Side

.element {
  mask-border-width: 10px 20px 30px 40px;
}

The widths apply to the top, right, bottom, and left in clockwise order.

Zero Width

.element {
  mask-border-width: 0;
}

A zero width removes the thickness of the corresponding mask border region. Zero is valid, but it may prevent the expected edge masking effect from appearing.

Important Consideration

Length values are useful when the mask border should maintain a consistent rendered thickness regardless of the element's actual border width.

Percentage Values

Percentage values specify mask border widths relative to the dimensions of the mask border image area.

Basic Percentage Example

.element {
  mask-border-width: 10%;
}

Each side uses a percentage of the corresponding dimension of the mask border image area.

Horizontal and Vertical Dimensions

For the top and bottom regions, percentages are calculated from the height of the mask border image area. For the left and right regions, percentages are calculated from its width.

Example Calculation

Suppose the mask border image area is 300 pixels wide and 200 pixels high.

.element {
  mask-border-width: 10%;
}

The top and bottom widths are 20 pixels each, while the left and right widths are 30 pixels each.

Different Percentages

.element {
  mask-border-width: 5% 10%;
}

The top and bottom use 5% of the area height. The left and right use 10% of the area width.

Mixing Percentages and Lengths

.element {
  mask-border-width: 5% 20px;
}

The top and bottom regions use percentage-based widths, while the left and right use fixed 20-pixel widths.

Responsive Mask Borders

Percentage widths can change as the mask border image area changes size. This can be useful when the mask border should scale proportionally with an element.

Important Consideration

Percentage values are not based on the source image's dimensions. That is an important difference from percentage values used by mask-border-slice.

Number Values

Unitless numbers specify mask border widths as multiples of the corresponding CSS border-width.

Basic Number Example

.element {
  border: 5px solid transparent;
  mask-border-width: 2;
}

The mask border regions are twice the corresponding CSS border widths, producing a width of 10 pixels on each side.

Multiplier of One

.element {
  border-width: 8px;
  border-style: solid;
  mask-border-width: 1;
}

A multiplier of 1 uses the corresponding CSS border width.

Multiplier of Three

.element {
  border: 4px solid transparent;
  mask-border-width: 3;
}

The resulting mask border regions are 12 pixels thick.

Fractional Multiplier

.element {
  border: 10px solid transparent;
  mask-border-width: 0.5;
}

A fractional multiplier of 0.5 produces a 5-pixel mask border width.

Different Multipliers

.element {
  border-width: 5px 10px;
  border-style: solid;
  mask-border-width: 2 3;
}

The top and bottom mask border widths become 10 pixels. The left and right widths become 30 pixels.

Important Consideration

A number is not a pixel measurement. Its result depends on the corresponding computed CSS border width. If that width is zero, multiplying it produces zero.

The auto Value

The auto keyword allows the browser to determine the mask border width from the source image's sliced regions.

Basic Example

.element {
  mask-border-width: auto;
}

Complete Example

.element {
  mask-border-source: url("border-mask.png");
  mask-border-slice: 30 fill;
  mask-border-width: auto;
}

The browser uses the corresponding intrinsic dimensions of the source image slices when those dimensions are available.

Fallback Behavior

If the source image does not provide the required intrinsic dimension, the browser uses the corresponding CSS border width instead.

Using auto with Other Values

.element {
  mask-border-width: auto 20px;
}

The top and bottom widths are determined automatically, while the left and right widths are explicitly set to 20 pixels.

Four-Value Example

.element {
  mask-border-width: auto 15px 20px auto;
}

The top and left use automatic widths, while the right and bottom use fixed widths.

When to Use auto

The auto value is useful when the source image's sliced dimensions should influence the rendered mask border thickness.

Important Consideration

The auto value is the initial value. It is different from border-image-width, whose initial value is 1.

One to Four Values

The mask-border-width property follows the same clockwise side order used by properties such as margin, padding, and border-width.

One Value

.element {
  mask-border-width: 20px;
}

All four sides use a width of 20 pixels.

Two Values

.element {
  mask-border-width: 10px 20px;
}

The top and bottom use 10 pixels. The right and left use 20 pixels.

Three Values

.element {
  mask-border-width: 10px 20px 30px;
}

The top uses 10 pixels, the right and left use 20 pixels, and the bottom uses 30 pixels.

Four Values

.element {
  mask-border-width: 10px 20px 30px 40px;
}

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

Value Order Reference

Values Top Right Bottom Left
10px 10px 10px 10px 10px
10px 20px 10px 20px 10px 20px
10px 20px 30px 10px 20px 30px 20px
10px 20px 30px 40px 10px 20px 30px 40px

Mixing Value Types

.element {
  mask-border-width: 10px 5% 2 auto;
}

Each side interprets its value independently. The top uses a length, the right a percentage, the bottom a border-width multiplier, and the left an automatic width.

Important Consideration

The values describe physical sides of the mask border area. They do not change order according to the element's writing direction.

Width vs. Slice

The mask-border-width and mask-border-slice properties control different stages of mask border construction.

Property Purpose
mask-border-source Provides the image used for the mask border.
mask-border-slice Determines where the source image is divided into corners, edges, and a center.
mask-border-width Determines the widths of the regions where the sliced mask image is rendered.
mask-border-outset Determines how far the mask border area extends beyond the element's border box.

Source Image Slicing

.element {
  mask-border-slice: 30 fill;
}

For a raster source image, the slicing lines are positioned 30 image pixels inward from the source image's edges.

Rendered Mask Border Width

.element {
  mask-border-width: 20px;
}

The rendered mask border regions use 20-pixel widths, regardless of the source image's slicing offsets.

Using Both Properties

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

The source image is divided at offsets of 30, and the resulting regions are scaled to fit the specified 20-pixel mask border widths.

Different Width and Slice Values

.element {
  mask-border-source: url("border-mask.png");
  mask-border-slice: 40 fill;
  mask-border-width: 12px;
}

The source image slices are resized to fit narrower rendered regions.

Important Consideration

A slicing value of 30 does not automatically create a 30-pixel rendered mask border. The two properties are independent.

Using the mask-border Shorthand

The mask-border-width property is one of six longhand properties represented by the mask-border shorthand.

Shorthand Example

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

The 20px value after the first slash specifies mask-border-width.

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;
}

Different Widths in the Shorthand

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

The top and bottom use 10-pixel widths, while the left and right use 20-pixel widths.

Using auto in the Shorthand

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

Override the Shorthand

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

The later longhand declaration changes the rendered widths to 10 pixels.

Shorthand Reset Behavior

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

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

Interaction with mask

.element {
  mask-border-width: 20px;
  mask: linear-gradient(black, transparent);
}

The mask shorthand resets mask border properties, including mask-border-width.

Correct Declaration Order

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

Declare the mask border settings after the mask shorthand when both are needed.

mask-border-width Example

This example compares different rendered border image widths. Because standardized mask border support is limited, the Browser Result uses border-image-width to demonstrate the same sizing geometry.

The demonstration uses a CSS gradient as the border image source. Each example changes the rendered width while keeping the source and slicing settings consistent.

HTML Example

<div class="mask-width-demo">
  <div class="mask-width-box width-ten">10px Width</div>
  <div class="mask-width-box width-twenty">20px Width</div>
  <div class="mask-width-box width-thirty">30px Width</div>
  <div class="mask-width-box width-mixed">10px 25px</div>
</div>

CSS Example

.mask-width-demo {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 20px;
  padding: 20px;
  background-color: #f1f5f9;
}
.mask-width-box {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 130px;
  padding: 15px;
  border: 30px solid transparent;
  background-color: #fff;
  color: #1e293b;
  font-weight: bold;
  text-align: center;
  border-image-source: linear-gradient(135deg, #2563eb, #059669);
  border-image-slice: 1;
  border-image-repeat: stretch;
}
.width-ten {
  border-image-width: 10px;
}
.width-twenty {
  border-image-width: 20px;
}
.width-thirty {
  border-image-width: 30px;
}
.width-mixed {
  border-image-width: 10px 25px;
}
Browser Result
10px Width
20px Width
30px Width
10px 25px

The examples show how the rendered border image thickness changes when different width values are applied. The fourth example uses different widths for the horizontal and vertical sides.

This is a border image sizing demonstration, not a rendered mask border. The border-image-width property paints decorative border artwork, while mask-border-width determines the dimensions of a masking effect.

Actual mask-border-width Declarations

.masked-ten {
  mask-border-source: url("border-mask.png");
  mask-border-slice: 30 fill;
  mask-border-width: 10px;
}
.masked-twenty {
  mask-border-source: url("border-mask.png");
  mask-border-slice: 30 fill;
  mask-border-width: 20px;
}
.masked-thirty {
  mask-border-source: url("border-mask.png");
  mask-border-slice: 30 fill;
  mask-border-width: 30px;
}
.masked-mixed {
  mask-border-source: url("border-mask.png");
  mask-border-slice: 30 fill;
  mask-border-width: 10px 25px;
}

These declarations use the standardized mask-border-width property. The image URL is illustrative and must point to an existing mask image.

Using a Border Width Multiplier

.masked-panel {
  border: 8px solid transparent;
  mask-border-source: url("border-mask.png");
  mask-border-slice: 30 fill;
  mask-border-width: 2;
}

The mask border width is twice the corresponding CSS border width, producing a width of 16 pixels on each side.

Using Percentage Widths

.masked-panel {
  mask-border-source: url("border-mask.png");
  mask-border-slice: 25% fill;
  mask-border-width: 10%;
}

The mask border widths are calculated relative to the dimensions of the mask border image area.

Restore Automatic Widths

.masked-panel {
  mask-border-width: auto;
}

The browser returns to the initial automatic sizing behavior.

Common Mistakes

Using Negative Values

.element {
  mask-border-width: -10px;
}

Negative values are invalid. Use zero or a positive length, percentage, or number.

Using More Than Four Values

.element {
  mask-border-width: 10px 20px 30px 40px 50px;
}

The property accepts a maximum of four values.

Confusing Numbers with Pixels

.element {
  mask-border-width: 20;
}

The unitless value 20 is a multiplier of the corresponding CSS border width, not a 20-pixel measurement. Use 20px for a fixed width.

Confusing Width with Slice

The mask-border-slice property divides the source image. The mask-border-width property controls the rendered widths of the resulting regions.

Using Percentages Relative to the Source Image

Percentage mask border widths are based on the mask border image area, not the source image dimensions.

Expecting the Property to Change Layout

The property controls masking geometry. It does not increase the element's CSS border widths or change the space occupied in normal document flow.

Forgetting the Source Image

A width declaration alone does not create a mask border. Supply an image using mask-border-source or the shorthand.

Assuming auto Means Zero

The auto value uses intrinsic slice dimensions when available, otherwise the corresponding CSS border width. It does not automatically mean zero.

Overwriting the Width with a Shorthand

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

The later shorthand resets the width to auto.

Assuming Universal Browser Support

The standardized mask-border-width property has limited browser support. Test the intended result before relying on it in production.

Important Considerations

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

  • The property controls the rendered widths of mask border image regions.
  • Its initial value is auto.
  • The property is not inherited.
  • It accepts one to four values.
  • Each value can be a length, percentage, number, or auto.
  • Negative values are not permitted.
  • Zero is a valid value.
  • Length values specify explicit rendered widths.
  • Percentage values are relative to the mask border image area.
  • Top and bottom percentages use the area's height.
  • Left and right percentages use the area's width.
  • Unitless numbers multiply the corresponding CSS border width.
  • Fractional number multipliers are permitted.
  • The auto value uses intrinsic source slice dimensions when available.
  • When intrinsic dimensions are unavailable, auto uses the corresponding CSS border width.
  • One value applies to all four sides.
  • Two values apply to the top and bottom, then the right and left.
  • Three values apply to the top, right and left, then bottom.
  • Four values follow the top, right, bottom, and left order.
  • Different value types can be combined in one declaration.
  • The property does not determine where the source image is sliced.
  • The mask-border-slice property controls source image slicing.
  • The mask-border-outset property controls how far the mask border area extends beyond the border box.
  • The property does not change the element's layout dimensions.
  • It is a longhand component of the mask-border shorthand.
  • The shorthand resets omitted width values to auto.
  • The mask shorthand also resets mask border properties.
  • The standardized property has limited browser support.
  • Legacy prefixed mask border implementations are not identical to the standardized feature.
  • Provide a suitable fallback when the masking effect is essential.

Browser Support

The CSS mask-border-width property has Limited availability and is not part of Baseline because it is not consistently supported across major browsers.

Some Chromium-based browsers support related functionality through the legacy -webkit-mask-box-image-width property. This does not guarantee support for the standardized mask-border-width longhand or the complete standardized mask border system.

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

When broad compatibility is required, consider supported mask-image techniques for transparency effects or border-image-width for decorative borders that do not require masking.

Specifications

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

CSS Masking Module Level 1: mask-border-width

The specification defines the accepted values, width calculations, and how the property determines the dimensions of rendered mask border regions.

For additional documentation and examples, consult MDN Web Docs.

MDN Web Docs: mask-border-width