CSS mask-origin Property
The CSS mask-origin property defines the positioning area used to place a mask image on an element.
It determines which reference box is used when calculating the mask image's position and size. Common values include border-box, padding-box, and content-box. Additional values support SVG graphics, and multiple mask layers can use different positioning areas.
mask-origin Syntax
The mask-origin property accepts a geometry box keyword or a comma-separated list of geometry box values.
Basic Syntax
selector {
mask-origin: border-box;
}
Border Box
.element {
mask-origin: border-box;
}
Padding Box
.element {
mask-origin: padding-box;
}
Content Box
.element {
mask-origin: content-box;
}
SVG Reference Boxes
.element {
mask-origin: fill-box;
}
.element {
mask-origin: stroke-box;
}
.element {
mask-origin: view-box;
}
Multiple Mask Layers
.element {
mask-origin: border-box, content-box;
}
CSS-Wide Keywords
.element {
mask-origin: initial;
}
.element {
mask-origin: inherit;
}
.element {
mask-origin: unset;
}
.element {
mask-origin: revert;
}
.element {
mask-origin: revert-layer;
}
The initial value is border-box, which positions the mask image relative to the element's border box.
Property Values
| Value | Description |
|---|---|
border-box |
Positions the mask image relative to the element's border box, including the content, padding, and border areas. |
padding-box |
Positions the mask image relative to the padding box, excluding the border area. |
content-box |
Positions the mask image relative to the content box, excluding the padding and border areas. |
fill-box |
Uses the object's fill bounding box as the mask positioning area. |
stroke-box |
Uses the object's stroke bounding box as the mask positioning area. |
view-box |
Uses the nearest SVG viewport as the positioning reference box. |
initial |
Resets the property to border-box. |
inherit |
Inherits the computed value from the parent element. |
unset |
Resets the property to border-box because the property 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 | border-box |
| Inherited | No |
| Applies to | All elements, with SVG-specific applicability rules |
| Computed value | As specified |
| Animation type | Discrete |
| Multiple values | Yes, comma-separated |
| Shorthand property | mask |
How It Works
Every CSS mask image has a positioning area. The mask-origin property determines which part of the element's box model is used to calculate that area.
The positioning area affects how mask-position and mask-size are resolved. It does not directly determine which portions of the element are clipped.
The CSS Box Model
+-------------------------------------+
| Border Box |
| +-------------------------------+ |
| | Padding Box | |
| | +-------------------------+ | |
| | | Content Box | | |
| | +-------------------------+ | |
| +-------------------------------+ |
+-------------------------------------+
Step 1: Create an Element
<div class="masked-panel">
Masked Content
</div>
Step 2: Define Its Box Model
.masked-panel {
width: 200px;
height: 120px;
padding: 25px;
border: 10px solid #2563eb;
background-color: #059669;
}
The element has distinct content, padding, and border areas.
Step 3: Add a Mask Image
.masked-panel {
mask-image: radial-gradient(circle, black 45%, transparent 46%);
mask-repeat: no-repeat;
}
The radial gradient creates a circular mask that reveals the center of the element.
Step 4: Choose the Positioning Area
.masked-panel {
mask-origin: content-box;
}
The mask image is now positioned relative to the content box instead of the border box.
Step 5: Adjust the Mask Position
.masked-panel {
mask-position: center;
mask-size: 100% 100%;
}
The mask is centered and sized relative to the selected positioning area.
Important Consideration
Changing mask-origin does not change the element's dimensions, padding, border, or position in the document.
border-box Value
The border-box value positions the mask image relative to the entire border box. It is the initial value.
Basic Example
.element {
mask-origin: border-box;
}
Complete Example
.masked-panel {
width: 200px;
padding: 20px;
border: 10px solid #2563eb;
background-color: #059669;
mask-image: radial-gradient(circle, black 45%, transparent 46%);
mask-origin: border-box;
mask-position: center;
mask-size: 100% 100%;
mask-repeat: no-repeat;
}
The mask image is sized and positioned using the entire border box, including the border and padding areas.
Using a Linear Gradient
.masked-panel {
mask-image: linear-gradient(to right, black, transparent);
mask-origin: border-box;
}
The gradient is positioned relative to the border box.
When to Use border-box
Use border-box when the mask image should be positioned relative to the element's complete outer box, including its border.
Important Consideration
Although the mask image is positioned relative to the border box, the mask-clip property can independently restrict the area where that mask layer is applied.
padding-box Value
The padding-box value positions the mask image relative to the content and padding areas, excluding the border.
Basic Example
.element {
mask-origin: padding-box;
}
Complete Example
.masked-panel {
width: 200px;
padding: 25px;
border: 12px solid #2563eb;
background-color: #059669;
mask-image: radial-gradient(circle, black 45%, transparent 46%);
mask-origin: padding-box;
mask-position: center;
mask-size: 100% 100%;
mask-repeat: no-repeat;
}
The mask image is positioned and sized relative to the padding box.
Matching the Clipping Area
.masked-panel {
mask-origin: padding-box;
mask-clip: padding-box;
}
The mask is both positioned and clipped using the padding box.
Different Positioning and Clipping Areas
.masked-panel {
mask-origin: padding-box;
mask-clip: border-box;
}
The mask is positioned relative to the padding box but can affect the border box wherever the mask image extends into that region.
When to Use padding-box
Use padding-box when the mask image should align with the element's content and padding rather than its outer border.
content-box Value
The content-box value positions the mask image relative to the element's content area, excluding padding and borders.
Basic Example
.element {
mask-origin: content-box;
}
Complete Example
.masked-panel {
width: 200px;
padding: 25px;
border: 12px solid #2563eb;
background-color: #059669;
mask-image: radial-gradient(circle, black 45%, transparent 46%);
mask-origin: content-box;
mask-position: center;
mask-size: 100% 100%;
mask-repeat: no-repeat;
}
The mask image is sized and positioned relative to the content box.
Matching the Clipping Area
.masked-panel {
mask-origin: content-box;
mask-clip: content-box;
}
Both the mask positioning area and clipping area use the content box.
Using a Smaller Mask
.masked-panel {
mask-image: radial-gradient(circle, black 45%, transparent 46%);
mask-origin: content-box;
mask-position: center;
mask-size: 80% 80%;
mask-repeat: no-repeat;
}
The mask occupies 80% of the content box's width and height and is centered within that area.
When to Use content-box
Use content-box when a mask should be aligned with the content area independently of the element's padding and border.
Important Consideration
The content box determines the mask's positioning reference. It does not automatically clip the mask to the content box unless mask-clip is also set appropriately.
SVG Reference Boxes
The fill-box, stroke-box, and view-box values provide positioning areas based on SVG geometry.
fill-box
.svg-shape {
mask-origin: fill-box;
}
The mask positioning area is based on the object's fill bounding box.
stroke-box
.svg-shape {
mask-origin: stroke-box;
}
The mask positioning area is based on the object's stroke bounding box, which includes its stroke geometry.
view-box
.svg-shape {
mask-origin: view-box;
}
The mask positioning area uses the nearest SVG viewport as its reference box.
SVG Example
<svg viewBox="0 0 200 120" xmlns="http://www.w3.org/2000/svg">
<rect class="masked-shape" x="30" y="20" width="140" height="80" rx="15" fill="#2563eb" stroke="#059669" stroke-width="12"/>
</svg>
CSS Example
.masked-shape {
mask-image: linear-gradient(to right, black, transparent);
mask-origin: stroke-box;
mask-size: 100% 100%;
mask-repeat: no-repeat;
}
The mask image is positioned and sized using the rectangle's stroke bounding box.
Using SVG Values on HTML Elements
SVG reference-box values also have defined behavior for elements without SVG layout boxes. For ordinary HTML elements, fill-box corresponds to content-box, while stroke-box and view-box correspond to border-box.
Important Consideration
SVG positioning areas depend on the element's geometry, strokes, and viewport. Test these values when using complex SVG shapes or nested SVG elements.
mask-origin vs. mask-clip
The mask-origin and mask-clip properties are closely related but control different parts of the masking process.
| Property | Purpose |
|---|---|
mask-origin |
Defines the positioning area used to place and size the mask image. |
mask-clip |
Defines the area within which a mask layer can affect the element. |
Same Origin and Clip
.element {
mask-image: linear-gradient(black, black);
mask-origin: content-box;
mask-clip: content-box;
}
The mask is positioned and clipped relative to the content box.
Different Origin and Clip
.element {
mask-image: linear-gradient(black, black);
mask-origin: border-box;
mask-clip: content-box;
}
The mask is positioned relative to the border box but restricted to the content box.
Position Within the Content Box
.element {
mask-image: radial-gradient(circle, black 45%, transparent 46%);
mask-origin: content-box;
mask-clip: border-box;
mask-position: center;
mask-size: 100% 100%;
mask-repeat: no-repeat;
}
The mask image uses the content box for positioning while the clipping region remains the border box.
Important Consideration
Changing mask-origin does not automatically change mask-clip. Set both properties when you need precise control over the mask's placement and visible boundaries.
Mask Position and Size
The mask-origin property works with mask-position and mask-size to determine where a mask image appears and how large it becomes.
Positioning a Mask
.element {
mask-image: radial-gradient(circle, black 45%, transparent 46%);
mask-origin: padding-box;
mask-position: center;
mask-size: 100px 100px;
mask-repeat: no-repeat;
}
The 100px mask is centered within the padding box.
Percentage-Based Mask Size
.element {
mask-image: radial-gradient(circle, black 45%, transparent 46%);
mask-origin: content-box;
mask-size: 50% 50%;
mask-position: center;
mask-repeat: no-repeat;
}
The mask image occupies half the width and height of the content box.
Using cover
.element {
mask-image: url("images/mask-shape.png");
mask-origin: border-box;
mask-size: cover;
mask-position: center;
mask-repeat: no-repeat;
}
The mask image is scaled to cover the selected positioning area while preserving its intrinsic proportions.
Using contain
.element {
mask-image: url("images/mask-shape.png");
mask-origin: content-box;
mask-size: contain;
mask-position: center;
mask-repeat: no-repeat;
}
The mask image is scaled to fit within the content box while preserving its intrinsic proportions.
Important Consideration
When a mask image uses percentage-based sizing or positioning, changing mask-origin can change the resulting dimensions and placement even when the other mask properties remain unchanged.
Multiple Mask Layers
The mask-origin property supports comma-separated values for multiple mask image layers.
Two Mask Layers
.element {
mask-image:
linear-gradient(to right, black, transparent),
radial-gradient(circle, black 45%, transparent 46%);
mask-origin: border-box, content-box;
}
The first mask image is positioned relative to the border box. The second is positioned relative to the content box.
Three Mask Layers
.element {
mask-image:
linear-gradient(to right, black, transparent),
radial-gradient(circle, black 45%, transparent 46%),
linear-gradient(to bottom, black, transparent);
mask-origin: border-box, padding-box, content-box;
}
Repeating Values
.element {
mask-image:
linear-gradient(to right, black, transparent),
radial-gradient(circle, black 45%, transparent 46%),
linear-gradient(to bottom, black, transparent);
mask-origin: border-box, content-box;
}
When fewer positioning values are supplied than mask layers, the values repeat. The third layer uses border-box.
Extra Values
.element {
mask-image:
linear-gradient(to right, black, transparent),
radial-gradient(circle, black 45%, transparent 46%);
mask-origin: border-box, padding-box, content-box;
}
The extra content-box value has no corresponding mask layer and is not used for rendering.
Different Origins and Clipping Areas
.element {
mask-image:
linear-gradient(to right, black, transparent),
radial-gradient(circle, black 45%, transparent 46%);
mask-origin: border-box, content-box;
mask-clip: padding-box, content-box;
}
Each mask layer can have its own positioning and clipping areas.
Important Consideration
Comma-separated values correspond to mask image layers, not the sides of the element. The first value applies to the first mask image, the second to the second image, and so on.
Using the mask Shorthand
The mask-origin property is a longhand component of the mask shorthand.
Basic Shorthand
.element {
mask: linear-gradient(black, transparent) padding-box;
}
When one geometry box value is specified in the shorthand, it sets both mask-origin and mask-clip.
Different Origin and Clip Values
.element {
mask: linear-gradient(black, transparent) border-box content-box;
}
The first geometry box value sets mask-origin. The second sets mask-clip.
Equivalent Longhand Declarations
.element {
mask-image: linear-gradient(black, transparent);
mask-origin: border-box;
mask-clip: content-box;
}
Shorthand with Position and Size
.element {
mask: url("images/mask-shape.png") center / contain no-repeat padding-box;
}
The mask image is centered, scaled to fit, and positioned and clipped relative to the padding box.
Override the Shorthand
.element {
mask: linear-gradient(black, transparent);
mask-origin: content-box;
}
The later longhand declaration changes the mask positioning area to content-box.
Shorthand Reset Behavior
.element {
mask-origin: content-box;
mask: linear-gradient(black, transparent);
}
The later shorthand resets mask-origin to its initial value of border-box.
Correct Declaration Order
.element {
mask: linear-gradient(black, transparent);
mask-origin: content-box;
}
Place the longhand declaration after the shorthand when it should override the shorthand's default positioning area.
mask-origin Example
This example compares three positioning areas using the same circular mask image. Each box has identical dimensions, padding, and borders.
The mask image uses mask-size: 100% 100%, so its size changes according to the selected positioning area. All three examples use mask-clip: border-box to keep the clipping boundary consistent.
HTML Example
<div class="mask-origin-demo">
<div class="mask-origin-item">
<div class="mask-origin-box origin-border"></div>
<p>border-box</p>
</div>
<div class="mask-origin-item">
<div class="mask-origin-box origin-padding"></div>
<p>padding-box</p>
</div>
<div class="mask-origin-item">
<div class="mask-origin-box origin-content"></div>
<p>content-box</p>
</div>
</div>
CSS Example
.mask-origin-demo {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 20px;
padding: 20px;
background-color: #f1f5f9;
}
.mask-origin-item {
min-width: 0;
text-align: center;
}
.mask-origin-box {
box-sizing: border-box;
width: 100%;
height: 150px;
padding: 20px;
border: 10px solid #2563eb;
background-color: #059669;
mask-image: radial-gradient(ellipse, black 48%, transparent 49%);
mask-position: center;
mask-size: 100% 100%;
mask-repeat: no-repeat;
mask-clip: border-box;
}
.origin-border {
mask-origin: border-box;
}
.origin-padding {
mask-origin: padding-box;
}
.origin-content {
mask-origin: content-box;
}
.mask-origin-item p {
margin: 10px 0 0;
font-weight: bold;
}
@media (max-width: 575px) {
.mask-origin-demo {
grid-template-columns: 1fr;
}
}
border-box
padding-box
content-box
The border-box example produces the largest circular mask because its positioning area includes the border and padding. The padding-box example produces a smaller circle, while the content-box example produces the smallest circle.
The boxes retain the same layout dimensions. Only the mask image's positioning and sizing reference changes.
Position a Mask in the Top-Right Corner
.corner-mask {
width: 250px;
height: 150px;
padding: 20px;
background-color: #2563eb;
mask-image: radial-gradient(circle, black 45%, transparent 46%);
mask-origin: content-box;
mask-position: right top;
mask-size: 100px 100px;
mask-repeat: no-repeat;
}
The circular mask is positioned at the top-right corner of the content box.
Use a Gradient with the Padding Box
.gradient-mask {
width: 250px;
height: 150px;
padding: 25px;
border: 10px solid #2563eb;
background-color: #059669;
mask-image: linear-gradient(to right, black, transparent);
mask-origin: padding-box;
mask-size: 100% 100%;
mask-repeat: no-repeat;
}
The gradient is sized and positioned relative to the padding box.
Common Mistakes
Confusing mask-origin with mask-clip
The mask-origin property controls mask image positioning. The mask-clip property controls the clipping area of a mask layer.
Using an Invalid Length
.element {
mask-origin: 20px;
}
The property does not accept lengths. Use mask-position to specify positional offsets.
Using an Invalid Percentage
.element {
mask-origin: 50%;
}
Percentages are not valid mask-origin values. Use a supported geometry box keyword.
Using no-clip as an Origin
.element {
mask-origin: no-clip;
}
The no-clip keyword belongs to mask-clip, not mask-origin.
Expecting content-box to Remove Padding
The content-box value changes the positioning reference of the mask image. It does not remove padding or change the element's layout.
Forgetting to Define a Mask Image
.element {
mask-origin: content-box;
}
The property does not create a mask image. Use mask-image or the mask shorthand to supply one.
Using Space-Separated Values
.element {
mask-origin: border-box content-box;
}
Multiple mask layer values must be separated by commas.
Assuming mask-origin Controls Clipping
Changing the mask positioning area does not automatically change the mask clipping area. Set mask-clip separately when needed.
Overwriting the Value with the mask Shorthand
.element {
mask-origin: content-box;
mask: linear-gradient(black, transparent);
}
The later shorthand resets mask-origin to border-box.
Important Considerations
Keep the following points in mind when using mask-origin:
- The property defines the positioning area for a CSS mask image.
- Its initial value is
border-box. - The property is not inherited.
- The
border-boxvalue uses the entire border box. - The
padding-boxvalue excludes the border area from the positioning reference. - The
content-boxvalue uses the content area as the positioning reference. - The
fill-boxvalue uses the object's fill bounding box. - The
stroke-boxvalue uses the object's stroke bounding box. - The
view-boxvalue uses an SVG viewport reference box. - The property accepts geometry box keywords rather than lengths or percentages.
- The
mask-positionproperty controls the mask image's position within the selected area. - The
mask-sizeproperty controls the mask image's dimensions. - Percentage-based mask sizing is affected by the selected positioning area.
- The
mask-clipproperty independently controls the mask clipping area. - Positioning and clipping areas can use different reference boxes.
- Multiple mask layers can use different positioning areas.
- Comma-separated values correspond to mask layers.
- Values repeat when fewer origins are supplied than mask layers.
- The property does not create a mask image by itself.
- Mask positioning does not change the element's layout dimensions.
- The property is a longhand component of the
maskshorthand. - The
maskshorthand resets omitted origin values toborder-box. - SVG reference boxes depend on the geometry of the SVG element.
- Test complex mask positioning in the browsers your website supports.
Browser Support
The CSS mask-origin property is Baseline 2023 - Newly available. It is supported in current versions of major browsers, including the commonly used border-box, padding-box, and content-box values.
Some older browser versions require the -webkit-mask-origin prefix. SVG-related positioning values may have additional compatibility differences in older implementations.
For current compatibility information, visit Can I Use? and search for mask-origin.
When supporting older browsers, test the actual positioning and sizing of the mask image, especially when using SVG reference boxes or multiple mask layers.
Specifications
The mask-origin property is defined in CSS Masking Module Level 1.
CSS Masking Module Level 1: mask-origin
The specification defines mask positioning areas, geometry box values, and their relationship to mask image placement.
For additional documentation and examples, consult MDN Web Docs.
