- Services
- Case Studies
- Technologies
- NextJs development
- Flutter development
- NodeJs development
- ReactJs development
- About
- Contact
- Tools
- Blogs
- FAQ
CSS Mask-Size Control Mask Image Dimensions
Learn about the use cases and available options like contain, cover, lengths, and percentages.
Introduction to CSS mask-size
The CSS mask-size
property is a useful tool for web developers, allowing you to specify the size of mask images. This feature helps ensure that mask images fit perfectly within your design elements. Introduced in December 2023, mask-size
works great with the latest devices and browser versions, but you should check compatibility for older ones.
The mask-size
property can be fully or partially constrained to preserve the image’s intrinsic ratio, making it a valuable asset for creating visually appealing and responsive designs.
In this article, we’ll explore the syntax and values of the mask-size
property, including keywords like contain
and cover
, and provide practical examples. We’ll also cover browser compatibility and offer related resources for further reading.
By the end of this article, you’ll understand how to use the mask-size
property effectively in your web development projects.
Specification and Compatibility
The mask-size
property is defined in the CSS Masking Module Level 1 specification. This specification outlines the behavior and usage of the mask-size
property, ensuring consistency across different browsers and platforms.
Specification:
- CSS Masking Module Level 1: This module introduces the
mask-size
property, detailing its functionality and how it interacts with other CSS properties.
Compatibility:
As of December 2023, the mask-size
property is supported by the latest versions of popular web browsers. However, it’s important to note that older browsers and devices may not support this feature.
- Chrome: Supported from version 4.
- Firefox: Supported from version 53.
- Safari: Supported from version 15.4.
- Opera: Supported from version 15.
- Edge: Supported from version 79.
- Internet Explorer: Not supported.
To check the full compatibility details, you can refer to the browser compatibility tables provided in this article.
Syntax and Values
The mask-size
property in CSS allows you to specify the size of mask images applied to an element. This property accepts various values, including keywords, lengths, percentages, and global values.
Syntax:
Values:
-
Keyword Values:
cover
: The mask image is scaled to cover the entire element, maintaining its aspect ratio. The image may be clipped if the element’s dimensions differ.contain
: The mask image is scaled to fit within the element, maintaining its aspect ratio. The image is centered and may have empty space (letterboxing).
-
Length and Percentage Values:
<length>
: Specifies the width or height using a fixed length (e.g., px, em).<percentage>
: Specifies the width or height as a percentage of the mask positioning area.
-
Auto Value:
auto
: Scales the mask image to its intrinsic dimensions, maintaining its intrinsic proportion.
Usage Examples:
-
One-Value Syntax:
mask-size: 50%;
: Sets the width to 50% of the mask positioning area and the height to auto.mask-size: 3em;
: Sets the width to 3em and the height to auto.
-
Two-Value Syntax:
mask-size: 50% auto;
: Sets the width to 50% of the mask positioning area and the height to auto.mask-size: 3em 25%;
: Sets the width to 3em and the height to 25% of the mask positioning area.
-
Multiple Values:
mask-size: auto, auto;
: Sets multiple mask images to auto size.mask-size: 50%, 25%, 25%;
: Sets the first mask image to 50% width, and the second and third mask images to 25% width each.
-
Global Values:
inherit
: Inherits the value from the parent element.initial
: Sets the value to its initial default.revert
: Resets the value to the user agent’s default.revert-layer
: Resets the value to the user agent’s default for the current cascade layer.unset
: Resets the value to its natural value, which means it acts asinherit
if the property is inherited, otherwise asinitial
.
Usage of Keywords: contain and cover
The mask-size
property in CSS offers two powerful keywords, contain
and cover
, which provide convenient ways to control the size and positioning of mask images. These keywords are essential for ensuring that mask images fit well within their designated areas while maintaining their aspect ratios.
Keyword: contain
The contain
keyword scales the mask image to fit within the mask positioning area while maintaining its aspect ratio. This means the image will fit completely within the element without being stretched or distorted. If the dimensions of the image and the element differ, the image will be letterboxed, which means there may be empty space around the image.
Usage Example:
Keyword: cover
The cover
keyword scales the mask image to cover the entire mask positioning area while maintaining its aspect ratio. This means the image will fill the element completely, and if the dimensions of the image and the element differ, the image will be clipped on the sides or top/bottom.
Usage Example:
Practical Example:
Let’s compare the usage of contain
and cover
with a practical example:
HTML:
CSS:
Output:
- The
mask-contain
element will display the mask image scaled to fit within the 200px by 200px area, with possible letterboxing. - The
mask-cover
element will display the mask image scaled to cover the entire 200px by 200px area, with possible clipping.
Browser Support:
- The
contain
andcover
keywords are supported across the latest versions of popular web browsers, including Chrome, Firefox, Safari, Opera, and Edge.
Working with Width and Height Values
The mask-size
property in CSS allows you to specify the size of mask images using width and height values. This provides precise control over the dimensions of the mask image, ensuring it fits perfectly within your design elements. By using width and height values, you can scale the mask image to specific lengths or percentages, maintaining its intrinsic proportions or adjusting it as needed.
Syntax:
Values:
- Width and Height Values:
<width>
: Specifies the width of the mask image. This can be a fixed length (e.g., px, em), a percentage, or the keywordauto
.<height>
: Specifies the height of the mask image. This can be a fixed length (e.g., px, em), a percentage, or the keywordauto
.
Usage Examples:
-
One-Value Syntax:
mask-size: 50%;
: Sets the width to 50% of the mask positioning area and the height toauto
.mask-size: 3em;
: Sets the width to 3em and the height toauto
.
-
Two-Value Syntax:
mask-size: 50% auto;
: Sets the width to 50% of the mask positioning area and the height toauto
.mask-size: 3em 25%;
: Sets the width to 3em and the height to 25% of the mask positioning area.
Handling Intrinsic Dimensions and Proportions:
The interpretation of width and height values depends on the intrinsic dimensions and proportions of the mask image. Here’s how the mask-size
property handles different scenarios:
-
Both Components Specified (Not
auto
):- If both width and height are specified with fixed values (e.g.,
mask-size: 100px 50px;
), the mask image will render at the specified size.
- If both width and height are specified with fixed values (e.g.,
-
One Component
auto
:- If one dimension is set to
auto
(e.g.,mask-size: auto 50%;
), the mask image will render using the specified dimension and compute the other dimension from the specified dimension and the intrinsic proportion. - If the image has no intrinsic proportion, the specified dimension will be used for that dimension, and the other dimension will use the image’s corresponding intrinsic dimension if available, or the corresponding dimension of the mask positioning area if not.
- If one dimension is set to
-
Both Components
auto
:- If both dimensions are set to
auto
(e.g.,mask-size: auto auto;
), the mask image will render at its intrinsic dimensions if available. - If the image has no intrinsic dimensions and no intrinsic proportion, it will render at the size of the mask positioning area.
- If the image has no dimensions but has a proportion, it will render as if
contain
had been specified. - If the image has one intrinsic dimension and a proportion, it will render at the size determined by that one dimension and the proportion.
- If the image has one intrinsic dimension but no proportion, it will render using the intrinsic dimension and the corresponding dimension of the mask positioning area.
- If both dimensions are set to
Practical Example:
Let’s illustrate the usage of width and height values with a practical example:
HTML:
CSS:
Output:
- The
mask-fixed
element will display the mask image scaled to 100px width and 50px height. - The
mask-percentage
element will display the mask image scaled to 50% of the width and 75% of the height of the mask positioning area.
Browser Support:
- The width and height values for the
mask-size
property are supported across the latest versions of popular web browsers, including Chrome, Firefox, Safari, Opera, and Edge.
Handling Intrinsic Dimensions and Proportions
The mask-size
property in CSS is designed to handle the intrinsic dimensions and proportions of mask images effectively. This ensures that the images are displayed appropriately within their designated areas, maintaining their natural aspect ratios. Understanding how mask-size
interacts with intrinsic dimensions and proportions is crucial for achieving the desired visual outcomes in your web designs.
Intrinsic Dimensions: Intrinsic dimensions refer to the natural width and height of an image. For instance, a bitmap image has fixed intrinsic dimensions, while a vector image may or may not have defined intrinsic dimensions. Gradients are treated as images with no intrinsic dimensions or proportions.
Intrinsic Proportions: Intrinsic proportions refer to the aspect ratio of an image, which is the ratio of its width to its height. Maintaining the intrinsic proportion ensures that the image does not get stretched or distorted.
Behavior of mask-size
:
The rendered size of the mask image is computed based on the values provided in the mask-size
property and the intrinsic dimensions and proportions of the image. Here’s how the mask-size
property handles different scenarios:
-
Both Components Specified (Not
auto
):- If both width and height are specified with fixed values (e.g.,
mask-size: 100px 50px;
), the mask image will render at the specified size.
- If both width and height are specified with fixed values (e.g.,
-
One Component
auto
:- If one dimension is set to
auto
(e.g.,mask-size: auto 50%;
), the mask image will render using the specified dimension and compute the other dimension from the specified dimension and the intrinsic proportion. - If the image has no intrinsic proportion, the specified dimension will be used for that dimension, and the other dimension will use the image’s corresponding intrinsic dimension if available, or the corresponding dimension of the mask positioning area if not.
- If one dimension is set to
-
Both Components
auto
:- If both dimensions are set to
auto
(e.g.,mask-size: auto auto;
), the mask image will render at its intrinsic dimensions if available. - If the image has no intrinsic dimensions and no intrinsic proportion, it will render at the size of the mask positioning area.
- If the image has no dimensions but has a proportion, it will render as if
contain
had been specified. - If the image has one intrinsic dimension and a proportion, it will render at the size determined by that one dimension and the proportion.
- If the image has one intrinsic dimension but no proportion, it will render using the intrinsic dimension and the corresponding dimension of the mask positioning area.
- If both dimensions are set to
By understanding how mask-size
interacts with intrinsic dimensions and proportions, you can effectively control the appearance of mask images in your web designs, ensuring they fit seamlessly within their designated areas while maintaining their natural aspect ratios.
Practical Examples
Example 1: Setting Mask Size Using Keywords
HTML:
CSS:
Output:
mask-contain
: Scales the mask image to fit within the 200px by 200px area.mask-cover
: Scales the mask image to cover the entire 200px by 200px area.
Example 2: Setting Mask Size Using Length and Percentage Values
HTML:
CSS:
Output:
mask-fixed
: Displays the mask image at 100px width and 50px height.mask-percentage
: Displays the mask image scaled to 50% width and 75% height of the mask positioning area.
Example 3: Setting Mask Size Using auto
HTML:
CSS:
Output:
mask-auto
: Displays the mask image at its intrinsic dimensions.
Example 4: Setting Mask Size Using Multiple Values
HTML:
CSS:
Output:
mask-multiple
: Displays the first mask image at 50% width, and the second and third mask images at 25% width each.
Example 5: Combining mask-size
with Other CSS Properties
HTML:
CSS:
Output:
mask-combined
: Displays the mask image scaled to cover the entire 200px by 200px area, with no repetition, and centered within the element.
Browser Compatibility
The mask-size
property is a relatively new feature in CSS. While it is supported by the latest versions of popular web browsers, it’s important to be aware of its compatibility across different platforms and versions. Here’s a breakdown of the browser compatibility for the mask-size
property:
Chrome:
- Supported from version 4 onwards.
Firefox:
- Supported from version 53 onwards.
Safari:
- Supported from version 15.4 onwards.
Opera:
- Supported from version 15 onwards.
Edge:
- Supported from version 79 onwards.
Internet Explorer:
- Not supported.
Browser Compatibility Table:
Browser | Version | Supported |
---|---|---|
Chrome | 4+ | Yes |
Firefox | 53+ | Yes |
Safari | 15.4+ | Yes |
Opera | 15+ | Yes |
Edge | 79+ | Yes |
Internet Explorer | - | No |
Note:
- The
mask-size
property might not
Talk with CEO
We'll be right here with you every step of the way.
We'll be here, prepared to commence this promising collaboration.
Whether you're curious about features, warranties, or shopping policies, we provide comprehensive answers to assist you.