×

CSS Masking

CSS Masking

The CSS mask property is applied to hide a component with the use of the masking or clipping an image at any specific point. The masking depicts the use of a graphical image or an image like the alpha mask or luminance. It can be any graphical operation, i.e., partially or fully hide the component’s portions or object.

It can be possible to hide or show the image’s part along with the distinct opacity levels with the use of masking. The masking can be achieved using the CSS mask-image attribute, and we must give any image like the mask.

The following are some examples of CSS masking.

Example:1

In the following example, we will position the mask over an image component. There will be two particular images, and we will apply to mask over them, the concluding image created by putting a mask image entirely together.

<!DOCTYPE html> 
<html> 
<head> 
<style> 
div img{ 
width: 200px; 
height: 200px; 
} 
#masked{ 
width: 300px; 
height: 300px; 
-webkit-mask-box-image: url(circle1.png) 25; 
} 
</style> 
</head> 
<body> 
<center> 
<div id = "one"> 
<h2> Original Images </h2> 
<img src = "flower4.jpg"> 
<img src = "circle1.png"> 
</div> 
<h2> After masking </h2> 
<img src = "flower4.jpg" id = "masked"> 
</center> 
</body> 
</html> 

Output:

Example: 2

In the following example, here are other illustrations where we will use masking on the images. We will apply two pictures where one of the pictures is the mask picture that includes stripes. The illustration will show the positioning the mask on a picture component as mentioned below.

<!DOCTYPE html> 
<html> 
<head> 
<style> 
div img{ 
width: 200px; 
height: 200px; 
} 
#masked{ 
width: 300px; 
height: 300px; 
-webkit-mask-box-image: url(stripes.png); 
background-color: black; 
} 
</style> 
</head> 
<body> 
<center> 
<div id = "one"> 
<h2> Original Images </h2> 
<img src = "flower4.jpg"> 
<img src = "stripes.png"> 
</div> 
<h2> After masking </h2> 
<img src = "flower4.jpg" id = "masked"> 
</center> 
</body> 
</html> 

Output:

Example: 3

We may apply the gradients of CSS like the mask image’s source.

<!DOCTYPE html> 
<html> 
<head> 
<style> 
#masked
{ 
width: 300px; 
height: 300px; 
-webkit-mask-image: -webkit-gradient(linear, right top, right bottom, from(rgba(0,0,0,0)), to(rgba(0,0,0,0.9))); 
border: 9px ridge green; 
} 
</style> 
</head> 
<body> 
<center> 
<img src = "flower4.jpg" id = "masked"> 
</center> 
</body> 
</html> 

Output:

Example: 4

<!DOCTYPE html> 
<html> 
<head> 
<style> 
#masked
{ 
width: 300px; 
height: 300px; 
border-radius: 0px; 
-webkit-mask-image: radial-gradient(circle at 50% 50%, blue 40%, rgba(0,0,0,0.3) 70%); 
border: 9px ridge green; 
} 
</style> 
</head> 
<body> 
<center> 
<img src = "flower4.jpg" id = "masked"> 
</center> 
</body> 
</html> 

Output:

CSS Masking

Example: 5

<!DOCTYPE html> 
<html> 
<head> 
<style> 
#circle
{ 
width: 300px; 
height: 300px; 
-webkit-mask-image: radial-gradient(circle at 50% 50%, blue 40%, rgba(0,0,0,0.3) 70%); 
border: 9px ridge green; 
} 
#ellipse
{ 
width: 300px; 
height: 300px; 
-webkit-mask-image: radial-gradient(ellipse 80% 30% at 50% 50%, blue 40%, rgba(0,0,0,0.3) 55%); 
border: 9px ridge green; 
} 
</style> 
</head> 
<body> 
<center> 
<img src = "flower4.jpg" id = "circle"> 
<img src = "flower4.jpg" id = "ellipse"> 
</center> 
</body> 
</html> 

Output:

CSS Masking

Related Topics

CSS Vertical Align

Vertical Align The vertical align CSS property describes the cell box of the table and an inline alignment vertically. This property is a self-explanatory and an essential property in CSS. But,...

2 minutes read.

CSS Word Wrap

Word Wrap in CSS: The word-wrap property in CSS is referred to break any lengthy word and wrap towards the next or following line. It is employed for preventing overflow...

1 minute read.

CSS Masking

CSS Masking The CSS mask property is applied to hide a component with the use of the masking or clipping an image at any specific point. The masking depicts the use...

3 minutes read.

CSS Font-Size

A Cascading Style Sheet (CSS) is a simple design scripting language intended to simplify the process of making web pages more visually appealing with the kind of high-level formatting. Cascading...

3 minutes read.

CSS Selector

CSS Selector: There are some selectors used in CSS, which allows us to choose the content we wish to style. Various selectors are considered as the separation of the set...

5 minutes read.

What is CSS

CSS stands for Cascading Style Sheet. It gives an additional style to the HTML document. A cascading style sheet is a language that is designed to define the document formatting...

3 minutes read.

CSS Background-clip

CSS Background-clip: The background-clip property describes the background’s painting area. It limits an area where the image or color of the background appears by using any clipping box. Everything will...

2 minutes read.

CSS Overflow

CSS Overflow: The Overflow property in CSS is used to manage the content if it overflows from its container of the block level. We have seen that all the elements...

3 minutes read.

CSS Pagination

CSS Pagination The pagination in CSS is an advantageous approach for indexing of a website’s distinct pages over the homepage. When our site contains multiple pages, we must insert a few...

13 minutes read.

CSS 3D Transform

Introduction To understand 3D transformation property of transform you need to first understand the difference between 2D and 3D transform. 2D transform is a CSS effect which transforms the elements according to...

6 minutes read.

CSS Form

You can create attractive HTML form by using CSS. Style input Fields The Input Field width properties are used to determine the width of the input field. Let us see an example of CSS style...

2 minutes read.

CSS Line Height

CSS Line Height: This property in CSS can be used to describe the line box’s minimal height inside the element. It sets-up the distinction among two lines in our content. It...

3 minutes read.

CSS Order

CSS Order CSS order property in CSS describes the flex item’s order inside the flex or any grid container. It is applied to order any flex item. Note: If any component is...

3 minutes read.

CSS Display

CSS Display: The CSS Display property is one of an essential property among all other properties of CSS. The display property specifies how the HTML elements should be displayed on...

4 minutes read.

CSS Flex-Box

Introduction The flex-box property of CSS stands for Flexible Box Layout Module. It is specifically used to design a flexible responsive layout. Flex-box is a one dimensional layout model, i.e. it works...

6 minutes read.

CSS: nth-selector

CSS nth selector can be used to match an element according to its position regardless of its parent’s type. Here, n can be any number, formula, or a keyword. These selectors...

2 minutes read.

CSS Checkbox style

CSS Checkbox style: It is an element of HTML, which is used to get input from various users. It is difficult to style any checkbox. However, some elements make it...

6 minutes read.

CSS Reference

The CSS Reference lists all of the basic standard CSS properties, pseudo-classes, pseudo-elements, and data types, for all intents and purposes functional notations, and at-rules alphabetically.  The site also includes...

5 minutes read.

CSS Page-break-inside Attribute

Page-break-inside Attribute As its name implies, the page-break-before property is applied to include page break inside any element, at the time of printing any document. It adds the page break before...

3 minutes read.

CSS Border

CSS Border: The border property of CSS allows us to define the style, color, width, and radius of the element’s border. It is applied to various elements to set-up the...

4 minutes read.