CSS mask-position Property
The CSS mask-position property specifies the horizontal and vertical position of a mask image within its positioning area.
It accepts position keywords, lengths, percentages, and combinations of these values. The property works with mask-image, mask-size, and mask-origin to control where a mask appears without changing the element's layout or dimensions.
mask-position Syntax
The mask-position property accepts one to four position components for each mask layer. Multiple mask layer positions are separated by commas.
Basic Syntax
selector {
mask-position: 0% 0%;
}
Single Keyword
.element {
mask-position: center;
}
Two Keywords
.element {
mask-position: right bottom;
}
Lengths
.element {
mask-position: 20px 30px;
}
Percentages
.element {
mask-position: 25% 75%;
}
Edge Offsets
.element {
mask-position: right 20px bottom 15px;
}
Multiple Mask Layers
.element {
mask-position: left top, right bottom;
}
CSS-Wide Keywords
.element {
mask-position: initial;
}
.element {
mask-position: inherit;
}
.element {
mask-position: unset;
}
.element {
mask-position: revert;
}
.element {
mask-position: revert-layer;
}
The initial value is 0% 0%, positioning the mask image at the top-left corner of its positioning area.
Property Values
| Value | Description |
|---|---|
left |
Aligns the mask image with the left edge of its positioning area. |
right |
Aligns the mask image with the right edge. |
top |
Aligns the mask image with the top edge. |
bottom |
Aligns the mask image with the bottom edge. |
center |
Centers the mask image along the specified axis. |
<length> |
Specifies a positional offset using units such as px, em, or rem. |
<percentage> |
Positions the mask image using a percentage of the difference between the positioning area and image dimensions. |
initial |
Resets the property to 0% 0%. |
inherit |
Inherits the computed value from the parent element. |
unset |
Resets the property to 0% 0% 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 | 0% 0% |
| Inherited | No |
| Applies to | All elements, with SVG-specific applicability rules |
| Computed value | As specified, with lengths made absolute |
| Animation type | Repeatable list of position values |
| Multiple values | Yes, comma-separated |
| Shorthand property | mask |
How It Works
A mask image is placed inside a positioning area determined by mask-origin. The mask-position property determines where the image is aligned within that area.
The position becomes especially noticeable when the mask image is smaller than its positioning area.
Step 1: Create an Element
<div class="masked-panel"></div>
Step 2: Apply a Mask Image
.masked-panel {
width: 300px;
height: 180px;
background-color: #2563eb;
mask-image: radial-gradient(circle, black 48%, transparent 49%);
mask-size: 100px 100px;
mask-repeat: no-repeat;
}
The radial gradient creates a circular visible region within a 100px by 100px mask image.
Step 3: Center the Mask
.masked-panel {
mask-position: center;
}
The mask image is centered horizontally and vertically.
Step 4: Move the Mask
.masked-panel {
mask-position: right bottom;
}
The mask image moves to the bottom-right corner of the positioning area.
Important Consideration
The position applies to the mask image, not the element itself. The element retains its original layout position and dimensions.
Keyword Positioning
Position keywords provide a convenient way to align a mask image with the edges or center of its positioning area.
Top Left
.element {
mask-position: left top;
}
Top Right
.element {
mask-position: right top;
}
Bottom Left
.element {
mask-position: left bottom;
}
Bottom Right
.element {
mask-position: right bottom;
}
Center
.element {
mask-position: center;
}
A single center keyword centers the image on both axes.
Single-Axis Keywords
.element {
mask-position: left;
}
.element {
mask-position: top;
}
A single horizontal keyword centers the image vertically. A single vertical keyword centers it horizontally.
When to Use Keywords
Keywords are useful when a mask needs to remain aligned with an edge or centered as the element changes size.
Length Positioning
Length values position a mask image using fixed or relative CSS units.
Pixel Offsets
.element {
mask-position: 20px 30px;
}
The image is positioned 20px from the left edge and 30px from the top edge of its positioning area.
Relative Units
.element {
mask-position: 2rem 1rem;
}
Negative Offsets
.element {
mask-position: -20px -10px;
}
Negative offsets move the mask image beyond the top-left edges of its positioning area.
Mixed Lengths and Keywords
.element {
mask-position: 30px center;
}
The mask is placed 30px from the left and centered vertically.
When to Use Lengths
Use lengths when a mask requires a precise offset, such as positioning a decorative cutout or aligning a mask with a specific part of an element.
Percentage Positioning
Percentage values position a mask image relative to the available space between the image and its positioning area.
Basic Example
.element {
mask-position: 25% 75%;
}
Common Percentages
| Value | Position |
|---|---|
0% 0% |
Top left |
50% 50% |
Center |
100% 0% |
Top right |
0% 100% |
Bottom left |
100% 100% |
Bottom right |
How Percentages Are Calculated
The browser subtracts the mask image's rendered dimension from the corresponding positioning area dimension, then applies the percentage to the difference.
Offset = (Positioning area size - Mask image size) * Percentage
Calculation Example
If the positioning area is 300px wide and the mask image is 100px wide, mask-position: 25% 0% produces a horizontal offset of 50px.
(300px - 100px) * 25% = 50px
When the Mask Fills the Area
If the mask image and positioning area have identical dimensions, percentage positioning has no effect along that axis because there is no remaining space.
When to Use Percentages
Percentages are useful for responsive mask placement because the resulting offset adjusts as the element or mask image changes size.
Edge Offsets
The three-value and four-value syntax allows a mask image to be positioned a specified distance from selected edges.
Right Edge Offset
.element {
mask-position: right 20px center;
}
The image is positioned 20px from the right edge and centered vertically.
Bottom Edge Offset
.element {
mask-position: center bottom 15px;
}
Four-Value Syntax
.element {
mask-position: right 20px bottom 15px;
}
The mask image is positioned 20px from the right edge and 15px from the bottom edge.
Top and Left Offsets
.element {
mask-position: left 25px top 10px;
}
Percentage Edge Offsets
.element {
mask-position: right 10% bottom 20%;
}
The offsets are calculated using the available space along their corresponding axes.
Important Consideration
Edge-offset syntax associates each length or percentage with an edge keyword. The keyword order can vary, but the horizontal and vertical edges must remain unambiguous.
mask-size and mask-origin
The visible effect of mask-position depends on the mask image's dimensions and the positioning area selected by mask-origin.
Using mask-size
.element {
mask-image: radial-gradient(circle, black 45%, transparent 46%);
mask-size: 100px 100px;
mask-position: center;
mask-repeat: no-repeat;
}
The mask image is centered within its positioning area.
Using mask-origin
.element {
padding: 30px;
mask-image: radial-gradient(circle, black 45%, transparent 46%);
mask-origin: content-box;
mask-size: 100px 100px;
mask-position: right bottom;
mask-repeat: no-repeat;
}
The mask is positioned at the bottom-right corner of the content box rather than the border box.
Using cover
.element {
mask-image: url("images/mask-shape.png");
mask-size: cover;
mask-position: center;
mask-repeat: no-repeat;
}
The mask image is scaled to cover the positioning area while preserving its proportions. Centering determines how any excess image area is distributed.
Using contain
.element {
mask-image: url("images/mask-shape.png");
mask-size: contain;
mask-position: center;
mask-repeat: no-repeat;
}
The image fits within the positioning area, and any remaining space is distributed around it.
Important Consideration
The mask-clip property separately determines where a mask layer can affect the element. Positioning a mask beyond the positioning area does not necessarily clip it to that area.
Multiple Mask Layers
Each mask image layer can have a different position. Comma-separated mask-position values correspond to the layers listed in mask-image.
Two Mask Layers
.element {
mask-image:
radial-gradient(circle, black 45%, transparent 46%),
radial-gradient(circle, black 45%, transparent 46%);
mask-size: 100px 100px;
mask-position: left center, right center;
mask-repeat: no-repeat;
}
One circular mask appears on the left and the other on the right.
Three Mask Layers
.element {
mask-image:
radial-gradient(circle, black 45%, transparent 46%),
radial-gradient(circle, black 45%, transparent 46%),
radial-gradient(circle, black 45%, transparent 46%);
mask-size: 80px 80px;
mask-position: left center, center, right center;
mask-repeat: no-repeat;
}
Repeating Position Values
.element {
mask-image:
linear-gradient(black, transparent),
radial-gradient(circle, black, transparent),
linear-gradient(to right, black, transparent);
mask-position: left top, right bottom;
}
When fewer positions are supplied than mask layers, the values repeat. The third layer uses left top.
Extra Position Values
.element {
mask-image:
linear-gradient(black, transparent),
radial-gradient(circle, black, transparent);
mask-position: left, center, right;
}
The third position has no corresponding mask layer and is not used for rendering.
Important Consideration
Mask layers are listed from topmost to bottommost. Their positions and sizes are assigned in the same order.
Using the mask Shorthand
The mask-position property is a longhand component of the mask shorthand.
Position in the Shorthand
.element {
mask: url("images/mask-shape.png") center no-repeat;
}
Position and Size
.element {
mask: url("images/mask-shape.png") right bottom / 100px 100px no-repeat;
}
The slash separates the mask position from its size.
Equivalent Longhand Declarations
.element {
mask-image: url("images/mask-shape.png");
mask-position: right bottom;
mask-size: 100px 100px;
mask-repeat: no-repeat;
}
Multiple Layers
.element {
mask:
radial-gradient(circle, black 45%, transparent 46%) left center / 80px 80px no-repeat,
radial-gradient(circle, black 45%, transparent 46%) right center / 80px 80px no-repeat;
}
Override the Shorthand
.element {
mask: url("images/mask-shape.png") center / contain no-repeat;
mask-position: right top;
}
The later longhand declaration changes the mask position to the top-right corner.
Shorthand Reset Behavior
.element {
mask-position: center;
mask: url("images/mask-shape.png") no-repeat;
}
The later shorthand resets mask-position to 0% 0% because no position is specified.
mask-position Example
This example demonstrates how the same circular mask image appears at six different positions. Every box has identical dimensions and mask settings except for mask-position.
HTML Example
<div class="mask-position-demo">
<div class="mask-position-item">
<div class="mask-position-box position-top-left"></div>
<p>left top</p>
</div>
<div class="mask-position-item">
<div class="mask-position-box position-top-right"></div>
<p>right top</p>
</div>
<div class="mask-position-item">
<div class="mask-position-box position-center"></div>
<p>center</p>
</div>
<div class="mask-position-item">
<div class="mask-position-box position-bottom-left"></div>
<p>left bottom</p>
</div>
<div class="mask-position-item">
<div class="mask-position-box position-bottom-right"></div>
<p>right bottom</p>
</div>
<div class="mask-position-item">
<div class="mask-position-box position-offset"></div>
<p>right 15px bottom 10px</p>
</div>
</div>
CSS Example
.mask-position-demo {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 20px;
padding: 20px;
background-color: #f1f5f9;
}
.mask-position-item {
min-width: 0;
text-align: center;
}
.mask-position-box {
width: 100%;
height: 120px;
background-color: #2563eb;
mask-image: radial-gradient(circle, black 48%, transparent 49%);
mask-size: 80px 80px;
mask-repeat: no-repeat;
}
.position-top-left {
mask-position: left top;
}
.position-top-right {
mask-position: right top;
}
.position-center {
mask-position: center;
}
.position-bottom-left {
mask-position: left bottom;
}
.position-bottom-right {
mask-position: right bottom;
}
.position-offset {
mask-position: right 15px bottom 10px;
}
.mask-position-item p {
margin: 10px 0 0;
font-weight: bold;
}
@media (max-width: 575px) {
.mask-position-demo {
grid-template-columns: repeat(2, minmax(0, 1fr));
}
}
left top
right top
center
left bottom
right bottom
right 15px bottom 10px
The examples demonstrate how keyword alignment and edge offsets control the placement of a mask image. The blue circular region changes position while each element retains the same dimensions.
Position a Spotlight
.spotlight {
width: 300px;
height: 180px;
background-color: #059669;
mask-image: radial-gradient(circle, black 30%, transparent 75%);
mask-size: 160px 160px;
mask-position: 70% 30%;
mask-repeat: no-repeat;
}
The circular mask is positioned toward the upper-right portion of the element.
Animate a Mask Position
.moving-mask {
width: 300px;
height: 150px;
background-color: #2563eb;
mask-image: radial-gradient(circle, black 40%, transparent 70%);
mask-size: 100px 100px;
mask-repeat: no-repeat;
animation: move-mask 3s ease-in-out infinite alternate;
}
@keyframes move-mask {
from {
mask-position: left center;
}
to {
mask-position: right center;
}
}
The mask moves horizontally across the element. For decorative animations, consider providing a reduced-motion alternative.
Common Mistakes
Forgetting mask-size
.element {
mask-image: linear-gradient(black, transparent);
mask-position: center;
}
A CSS gradient normally fills its mask positioning area when its size is auto. Centering may produce no visible movement. Specify a smaller mask-size when positioning should be noticeable.
Forgetting mask-repeat
.element {
mask-image: radial-gradient(circle, black, transparent);
mask-size: 80px 80px;
mask-position: center;
}
The default mask-repeat value is repeat. Add mask-repeat: no-repeat to display a single mask image.
Confusing mask-position with mask-origin
The mask-position property places the mask image within a positioning area. The mask-origin property defines that area.
Using an Invalid Keyword
.element {
mask-position: middle;
}
Use center rather than middle.
Using Conflicting Keywords
.element {
mask-position: left right;
}
Both keywords specify horizontal positions. A valid two-keyword position must not contain conflicting values for the same axis.
Misunderstanding Percentages
Percentage positioning is based on the difference between the positioning area and the mask image dimensions. It is not simply a percentage measured from the element's top-left corner.
Using Space-Separated Mask Layers
.element {
mask-position: left top right bottom;
}
This is not a valid way to assign positions to two mask layers. Separate layer positions with commas.
Expecting the Element to Move
The property moves the mask image, not the element. Use layout or positioning properties to move the element itself.
Overwriting the Value with the mask Shorthand
.element {
mask-position: center;
mask: url("images/mask-shape.png") no-repeat;
}
The later shorthand resets mask-position to its initial value.
Important Considerations
Keep the following points in mind when using mask-position:
- The property controls the horizontal and vertical placement of a mask image.
- Its initial value is
0% 0%. - The property is not inherited.
- Position keywords include
left,right,top,bottom, andcenter. - Lengths and percentages can specify positional offsets.
- Three-value and four-value syntax supports offsets from selected edges.
- Percentage offsets depend on the difference between the positioning area and mask image dimensions.
- The
mask-originproperty determines the positioning reference box. - The
mask-sizeproperty determines the mask image dimensions. - The
mask-repeatproperty controls whether the image repeats. - The
mask-clipproperty controls the mask clipping area separately. - Negative positional offsets are permitted.
- Multiple mask layers can have different positions.
- Comma-separated values correspond to mask image layers in order.
- Position values repeat when fewer values are supplied than mask layers.
- Extra position values without corresponding mask layers are not used for rendering.
- The property can be animated between compatible position values.
- Changing the mask position does not change the element's layout dimensions.
- The property is a longhand component of the
maskshorthand. - The
maskshorthand resets omitted positions to0% 0%. - SVG mask references can follow different positioning rules from ordinary image-based mask layers.
- Test complex mask positioning in the browsers your website supports.
Browser Support
The CSS mask-position property is Baseline 2023 - Newly available. It is supported in current versions of major browsers, including common keyword, length, and percentage positioning.
Some older browser versions require the -webkit-mask-position prefix. Support for more complex masking combinations and SVG-related behavior can vary in older implementations.
For current compatibility information, visit Can I Use? and search for mask-position.
When supporting older browsers, test the actual mask placement, particularly when using edge offsets, multiple mask layers, or animated positions.
Specifications
The mask-position property is defined in CSS Masking Module Level 1.
CSS Masking Module Level 1: mask-position
The specification defines mask image positioning and its relationship to mask positioning areas and image dimensions.
For additional documentation and examples, consult MDN Web Docs.
