CSS mask-size Property
The CSS mask-size property specifies the width and height of a mask image used to control an element's visibility.
It accepts lengths, percentages, and the keywords auto, cover, and contain. The property works with mask-image, mask-position, and mask-repeat to control how a mask fits within its positioning area.
mask-size Syntax
The mask-size property accepts one or two sizing values for each mask image layer. Multiple layers are separated by commas.
Basic Syntax
selector {
mask-size: auto;
}
Cover
.element {
mask-size: cover;
}
Contain
.element {
mask-size: contain;
}
Fixed Dimensions
.element {
mask-size: 100px 80px;
}
Percentage Dimensions
.element {
mask-size: 50% 75%;
}
Automatic Height
.element {
mask-size: 120px auto;
}
Multiple Mask Layers
.element {
mask-size: cover, 80px 80px;
}
CSS-Wide Keywords
.element {
mask-size: initial;
}
.element {
mask-size: inherit;
}
.element {
mask-size: unset;
}
.element {
mask-size: revert;
}
.element {
mask-size: revert-layer;
}
The initial value is auto, which is equivalent to auto auto.
Property Values
| Value | Description |
|---|---|
auto |
Uses the mask image's intrinsic dimensions or aspect ratio when available, with sizing determined by the positioning area when necessary. |
cover |
Scales the image to cover the entire positioning area while preserving its intrinsic aspect ratio when one exists. |
contain |
Scales the image to fit entirely within the positioning area while preserving its intrinsic aspect ratio when one exists. |
<length> |
Specifies a dimension using a non-negative length such as px, em, or rem. |
<percentage> |
Specifies a dimension as a non-negative percentage of the corresponding positioning area dimension. |
initial |
Resets the property to auto. |
inherit |
Inherits the computed value from the parent element. |
unset |
Resets the property to auto 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 | auto |
| Inherited | No |
| Applies to | All elements, with SVG-specific applicability rules |
| Computed value | As specified, with lengths made absolute |
| Animation type | Repeatable list of size values |
| Multiple values | Yes, comma-separated |
| Shorthand property | mask |
How It Works
The browser determines the mask image's rendered dimensions using mask-size. Those dimensions affect how the image is positioned and whether it repeats within the mask 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 45%, transparent 46%);
}
Step 3: Set the Mask Dimensions
.masked-panel {
mask-size: 100px 100px;
}
The mask image is rendered at 100px by 100px.
Step 4: Center the Mask
.masked-panel {
mask-position: center;
mask-repeat: no-repeat;
}
The mask is centered within the element's positioning area, and only one copy appears.
Step 5: Change the Mask Size
.masked-panel {
mask-size: 160px 160px;
}
The visible circular region becomes larger without changing the element's dimensions.
auto Value
The auto value allows the browser to determine the mask image's dimensions from its intrinsic size, aspect ratio, and positioning area.
Basic Example
.element {
mask-size: auto;
}
External Image
.element {
mask-image: url("images/mask-shape.png");
mask-size: auto;
mask-repeat: no-repeat;
}
An image with intrinsic width and height normally uses those dimensions.
CSS Gradient
.element {
mask-image: linear-gradient(to right, black, transparent);
mask-size: auto;
}
CSS gradients have no intrinsic dimensions or intrinsic aspect ratio. With both dimensions set to auto, the gradient fills the mask positioning area.
One Automatic Dimension
.element {
mask-image: url("images/mask-shape.png");
mask-size: 150px auto;
}
For an image with an intrinsic aspect ratio, the browser calculates the automatic height to preserve that ratio.
When to Use auto
Use auto when the mask should retain its natural dimensions or when the browser should determine one dimension from the image's aspect ratio.
cover Value
The cover value scales the mask image until it covers the entire positioning area.
Basic Example
.element {
mask-size: cover;
}
Complete Example
.element {
width: 300px;
height: 180px;
background-color: #2563eb;
mask-image: url("images/mask-shape.png");
mask-size: cover;
mask-position: center;
mask-repeat: no-repeat;
}
The mask image covers the positioning area. If the image's aspect ratio differs from the area's aspect ratio, some portions of the image may extend beyond its edges.
Using a Gradient
.element {
mask-image: radial-gradient(circle, black 40%, transparent 70%);
mask-size: cover;
}
A CSS gradient without intrinsic dimensions fills the positioning area when cover is used.
When to Use cover
Use cover when the mask should fill the entire positioning area without leaving uncovered space caused by the image's aspect ratio.
Important Consideration
The cover value does not necessarily reveal the entire element. The mask image's transparent regions still determine which portions remain visible.
contain Value
The contain value scales the mask image so that the entire image fits within the positioning area.
Basic Example
.element {
mask-size: contain;
}
Complete Example
.element {
width: 300px;
height: 180px;
background-color: #059669;
mask-image: url("images/mask-shape.png");
mask-size: contain;
mask-position: center;
mask-repeat: no-repeat;
}
The complete mask image fits inside the positioning area. Depending on its aspect ratio, unused space may remain horizontally or vertically.
Centered Mask
.element {
mask-image: url("images/mask-shape.png");
mask-size: contain;
mask-position: center;
mask-repeat: no-repeat;
}
When to Use contain
Use contain when the entire mask image should remain within the positioning area without being cropped.
Important Consideration
When mask-repeat is no-repeat, areas outside the contained mask image are transparent for that mask layer.
Length Values
Length values specify exact or relative dimensions for a mask image.
Fixed Width and Height
.element {
mask-size: 120px 80px;
}
The mask image is rendered at 120px wide and 80px high.
Relative Units
.element {
mask-size: 8rem 6rem;
}
Single Length
.element {
mask-size: 150px;
}
A single length specifies the width. The height is treated as auto.
Automatic Width
.element {
mask-size: auto 100px;
}
The height is 100px. The width is calculated automatically.
Preserving Aspect Ratio
.element {
mask-image: url("images/mask-shape.png");
mask-size: 200px auto;
}
For an image with an intrinsic aspect ratio, the browser calculates the height from the specified width.
Stretching an Image
.element {
mask-image: url("images/mask-shape.png");
mask-size: 200px 100px;
}
Specifying both dimensions can stretch an image if the dimensions do not match its intrinsic aspect ratio.
Important Consideration
Negative lengths are invalid. A value of 0 is allowed but produces a zero-sized dimension, preventing that mask image from being rendered.
Percentage Values
Percentage values size a mask image relative to the dimensions of its mask positioning area.
Half Width and Height
.element {
mask-size: 50% 50%;
}
Full Width and Height
.element {
mask-size: 100% 100%;
}
The mask image fills the positioning area's width and height. An image with an intrinsic aspect ratio may be stretched.
Full Width with Automatic Height
.element {
mask-size: 100% auto;
}
The width matches the positioning area. The height is calculated automatically, preserving the intrinsic aspect ratio when available.
Mixed Percentage and Length
.element {
mask-size: 75% 120px;
}
Percentage Calculation
If the positioning area is 300px wide and 200px high, mask-size: 50% 75% produces a mask image measuring 150px by 150px.
Width: 300px * 50% = 150px
Height: 200px * 75% = 150px
Important Consideration
Percentages are calculated from the mask positioning area, which is defined by mask-origin. Changing that reference box can change the mask's rendered dimensions.
One vs. Two Values
The mask-size property accepts one or two sizing components for each mask layer.
| Syntax | Meaning |
|---|---|
100px |
Width is 100px; height is auto. |
100px 80px |
Width is 100px; height is 80px. |
50% |
Width is 50%; height is auto. |
50% 75% |
Width is 50%; height is 75%. |
auto 100px |
Width is auto; height is 100px. |
100px auto |
Width is 100px; height is auto. |
cover |
Scale to cover the positioning area. |
contain |
Scale to fit within the positioning area. |
One-Value Example
.element {
mask-size: 120px;
}
Two-Value Example
.element {
mask-size: 120px 80px;
}
Important Consideration
The keywords cover and contain are standalone sizing values. They cannot be combined with a second width or height value.
Mask Position and Repetition
The mask-size property works with mask-position and mask-repeat to determine where mask images appear and how they fill the positioning area.
One Centered Mask
.element {
mask-image: radial-gradient(circle, black 45%, transparent 46%);
mask-size: 120px 120px;
mask-position: center;
mask-repeat: no-repeat;
}
Repeated Small Masks
.element {
mask-image: radial-gradient(circle, black 45%, transparent 46%);
mask-size: 40px 40px;
mask-repeat: repeat;
}
Smaller mask images produce more repeated tiles within the same area.
Horizontal Mask Pattern
.element {
mask-image: radial-gradient(circle, black 45%, transparent 46%);
mask-size: 50px 50px;
mask-position: center;
mask-repeat: repeat-x;
}
Changing the Positioning Area
.element {
padding: 20px;
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 is sized and positioned relative to the content box.
Important Consideration
The mask-clip property controls the clipping area separately. A mask can be sized relative to one reference box and clipped to another.
Multiple Mask Layers
The mask-size property supports comma-separated values for multiple mask image layers.
Two Mask Layers
.element {
mask-image:
radial-gradient(circle, black 45%, transparent 46%),
linear-gradient(to right, black, transparent);
mask-size: 100px 100px, cover;
}
The first mask image measures 100px by 100px. The second covers its positioning area.
Three Mask Layers
.element {
mask-image:
radial-gradient(circle, black 45%, transparent 46%),
linear-gradient(to right, black, transparent),
radial-gradient(circle, black 45%, transparent 46%);
mask-size: 60px 60px, 100% 100%, 120px 120px;
}
Repeating Size Values
.element {
mask-image:
radial-gradient(circle, black, transparent),
linear-gradient(black, transparent),
radial-gradient(circle, black, transparent);
mask-size: 80px 80px, cover;
}
When fewer size values are supplied than mask layers, the values repeat. The third layer uses 80px 80px.
Extra Size Values
.element {
mask-image:
radial-gradient(circle, black, transparent),
linear-gradient(black, transparent);
mask-size: 80px 80px, cover, contain;
}
The third size value has no corresponding mask layer and is not used for rendering.
Different Sizes and Positions
.element {
mask-image:
radial-gradient(circle, black 45%, transparent 46%),
radial-gradient(circle, black 45%, transparent 46%);
mask-size: 80px 80px, 120px 120px;
mask-position: left center, right center;
mask-repeat: no-repeat;
}
Each mask layer has its own size and position.
Using the mask Shorthand
The mask-size property is a longhand component of the mask shorthand.
Position and Size
.element {
mask: url("images/mask-shape.png") center / contain no-repeat;
}
The slash separates the mask position from the mask size.
Fixed Mask Size
.element {
mask: radial-gradient(circle, black 45%, transparent 46%) center / 100px 100px no-repeat;
}
Equivalent Longhand Declarations
.element {
mask-image: radial-gradient(circle, black 45%, transparent 46%);
mask-position: center;
mask-size: 100px 100px;
mask-repeat: no-repeat;
}
Multiple Mask 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 / 120px 120px no-repeat;
}
Override the Shorthand
.element {
mask: url("images/mask-shape.png") center / contain no-repeat;
mask-size: 150px 150px;
}
The later longhand declaration overrides the size supplied by the shorthand.
Shorthand Reset Behavior
.element {
mask-size: 150px 150px;
mask: url("images/mask-shape.png") no-repeat;
}
The later shorthand resets mask-size to its initial value of auto.
mask-size Example
This example compares six mask sizes using the same circular gradient. Every box has identical dimensions, positioning, and repetition settings.
HTML Example
<div class="mask-size-demo">
<div class="mask-size-item">
<div class="mask-size-box size-small"></div>
<p>60px 60px</p>
</div>
<div class="mask-size-item">
<div class="mask-size-box size-medium"></div>
<p>100px 100px</p>
</div>
<div class="mask-size-item">
<div class="mask-size-box size-large"></div>
<p>140px 140px</p>
</div>
<div class="mask-size-item">
<div class="mask-size-box size-half"></div>
<p>50% 50%</p>
</div>
<div class="mask-size-item">
<div class="mask-size-box size-full"></div>
<p>100% 100%</p>
</div>
<div class="mask-size-item">
<div class="mask-size-box size-auto"></div>
<p>auto</p>
</div>
</div>
CSS Example
.mask-size-demo {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 20px;
padding: 20px;
background-color: #f1f5f9;
}
.mask-size-item {
min-width: 0;
text-align: center;
}
.mask-size-box {
width: 100%;
height: 150px;
background-color: #2563eb;
mask-image: radial-gradient(circle, black 45%, transparent 46%);
mask-position: center;
mask-repeat: no-repeat;
}
.size-small {
mask-size: 60px 60px;
}
.size-medium {
mask-size: 100px 100px;
}
.size-large {
mask-size: 140px 140px;
}
.size-half {
mask-size: 50% 50%;
}
.size-full {
mask-size: 100% 100%;
}
.size-auto {
mask-size: auto;
}
.mask-size-item p {
margin: 10px 0 0;
font-weight: bold;
}
@media (max-width: 575px) {
.mask-size-demo {
grid-template-columns: repeat(2, minmax(0, 1fr));
}
}
60px 60px
100px 100px
140px 140px
50% 50%
100% 100%
auto
The fixed-size examples produce circular visible regions of different sizes. The percentage examples scale with the positioning area. Because the gradient has no intrinsic dimensions, auto fills the positioning area, producing the same result as 100% 100% in this example.
Create a Repeating Dot Pattern
.dot-mask {
width: 300px;
height: 150px;
background-color: #059669;
mask-image: radial-gradient(circle, black 40%, transparent 41%);
mask-size: 35px 35px;
mask-repeat: repeat;
}
Small mask tiles create a repeating pattern of visible dots.
Create a Large Spotlight
.spotlight-mask {
width: 300px;
height: 180px;
background-color: #2563eb;
mask-image: radial-gradient(circle, black 30%, transparent 75%);
mask-size: 220px 220px;
mask-position: center;
mask-repeat: no-repeat;
}
A larger radial gradient creates a broad circular reveal with soft edges.
Common Mistakes
Using Negative Dimensions
.element {
mask-size: -100px 80px;
}
Negative lengths and percentages are invalid.
Using an Invalid Keyword
.element {
mask-size: stretch;
}
The stretch keyword is not supported. Use explicit width and height values when stretching is required.
Combining cover with a Length
.element {
mask-size: cover 100px;
}
The cover and contain keywords must be used alone for each mask layer.
Confusing mask-size with Element Dimensions
The mask-size property changes the mask image's dimensions, not the element's width or height.
Forgetting mask-repeat
.element {
mask-image: radial-gradient(circle, black 45%, transparent 46%);
mask-size: 80px 80px;
}
The mask repeats by default. Use mask-repeat: no-repeat when a single mask image is intended.
Expecting contain to Fill the Entire Area
The contain value fits the complete image within the positioning area. Depending on aspect ratios, unused space may remain.
Expecting cover to Preserve Every Edge
The cover value fills the positioning area, which may place parts of the mask image outside its boundaries.
Using Space-Separated Mask Layers
.element {
mask-size: 80px 80px cover;
}
Separate size values for different mask layers with commas.
Overwriting the Property with mask
.element {
mask-size: 100px 100px;
mask: url("images/mask-shape.png");
}
The later shorthand resets mask-size to auto.
Important Considerations
Keep the following points in mind when using mask-size:
- The property controls the rendered dimensions of mask images.
- Its initial value is
auto. - The property is not inherited.
autouses intrinsic image dimensions or other automatic sizing rules.coverscales an image to cover its positioning area.containscales an image to fit entirely within its positioning area.- Lengths specify fixed or relative mask dimensions.
- Percentages are calculated relative to the mask positioning area.
- Negative lengths and percentages are invalid.
- One sizing value sets the width and leaves the height automatic.
- Two sizing values independently control width and height.
- Explicit width and height values may stretch images.
- Automatic dimensions can preserve an image's intrinsic aspect ratio.
- CSS gradients have no intrinsic dimensions or aspect ratio.
- The
mask-originproperty defines the positioning reference box. - The
mask-positionproperty controls mask placement. - The
mask-repeatproperty controls mask tiling. - The
mask-clipproperty controls the clipping area separately. - Multiple mask layers can use different sizes.
- Comma-separated values correspond to mask layers in order.
- Size values repeat when fewer values are supplied than mask layers.
- The property does not create a mask image by itself.
- Changing mask size does not change the element's layout dimensions.
- The property is a longhand component of the
maskshorthand. - The
maskshorthand resets omitted size values toauto. - Test complex mask sizing in the browsers your website supports.
Browser Support
The CSS mask-size property is Baseline 2023 - Newly available. It is supported in current versions of major browsers, including the commonly used auto, cover, contain, length, and percentage values.
Some older browser versions require the -webkit-mask-size prefix. Support for complex mask combinations and SVG-related behavior can vary in older implementations.
For current compatibility information, visit Can I Use? and search for mask-size.
When supporting older browsers, test the actual mask dimensions, particularly when using cover, contain, and multiple mask layers.
Specifications
The mask-size property is defined in CSS Masking Module Level 1.
CSS Masking Module Level 1: mask-size
The specification defines mask image sizing, intrinsic dimensions, and the behavior of cover, contain, lengths, and percentages.
For additional documentation and examples, consult MDN Web Docs.
