CSS AdvancedLesson #157

Masking - SVG

The SVG `<mask>` element can be used to

CSS SVG Mask Layers

The SVG <mask> element can be used to apply a mask to an image.

Here, we use the SVG <mask> element to create different mask layers for an image.


SVG Mask Layer - Circle

The SVG <circle> element is used to create a circle.

<svg width="600" height="400" xmlns="http://www.w3.org/2000/svg"> <mask id="svgmask1"> <circle r="150" cx="200" cy="200" fill="#ffffff" /> </mask> <image xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="img_5terre.jpg" mask="url(#svgmask1)"></image> Sorry, your browser does not support inline SVG. </svg>

Example

Example Code

html
Try it Yourself
Live Output Preview:

SVG Mask Layer - Ellipse

The SVG <ellipse> element is used to create an ellipse.

<svg width="600" height="400" xmlns="http://www.w3.org/2000/svg"> <mask id="svgmask5"> <ellipse cx="220" cy="150" rx="200" ry="100" fill="#ffffff" /> </mask> <image xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="img_5terre.jpg" mask="url(#svgmask5)"></image> Sorry, your browser does not support inline SVG. </svg>

Example

Example Code

html
Try it Yourself
Live Output Preview:

SVG Mask Layer - Triangle

The SVG <polygon> element is used to create a graphic that contains at least three sides.

<svg width="600" height="400" xmlns="http://www.w3.org/2000/svg"> <mask id="svgmask2">

</polygon> </mask> <image xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="img_5terre.jpg" mask="url(#svgmask2)"></image> Sorry, your browser does not support inline SVG. </svg>

Example

Example Code

html
Try it Yourself
Live Output Preview:

SVG Mask - Star

The SVG <polygon> element can also be used to create a star-shaped mask layer.

<svg width="600" height="400" xmlns="http://www.w3.org/2000/svg"> <mask id="svgmask3">

</polygon> </mask> <image xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="img_5terre.jpg" mask="url(#svgmask3)"></image> Sorry, your browser does not support inline SVG. </svg>

Example

Example Code

html
Try it Yourself
Live Output Preview:

SVG Mask - Multiple Circles

Here we define three circles with different x and y positions.

<svg width="600" height="400" xmlns="http://www.w3.org/2000/svg"> <mask id="svgmask4"> <circle fill="#ffffff" cx="75" cy="75" r="75"></circle> <circle fill="#ffffff" cx="80" cy="260" r="75"></circle> <circle fill="#ffffff" cx="270" cy="160" r="75"></circle> </mask> <image xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="img_5terre.jpg" mask="url(#svgmask4)"></image> Sorry, your browser does not support inline SVG. </svg>

Example

Example Code

html
Try it Yourself
Live Output Preview:

CSS All Masking Properties

The following table lists all the CSS masking properties:

PropertyDescription
mask-clipSpecifies which area is affected by a mask image
mask-compositeSpecifies a compositing operation used on the current mask layer with the mask layers below it
mask-imageSpecifies an image to be used as a mask layer for an element
mask-modeSpecifies whether the mask layer image is treated as a luminance mask or as an alpha mask
mask-originSpecifies the origin position (the mask position area) of a mask layer image
mask-positionSets the starting position of a mask layer image (relative to the mask position area)
mask-repeatSpecifies how the mask layer image is repeated
mask-sizeSpecifies the size of a mask layer image
mask-typeSpecifies whether an SVG <mask> element is treated as a luminance mask or as an alpha mask

Interactive Code Examples

CSS Example 1

html
Try it Yourself
Interactive runnable CSS example from EA Academy.
Live Output Preview:

CSS Example 2

html
Try it Yourself
Interactive runnable CSS example from EA Academy.
Live Output Preview:

CSS Example 3

html
Try it Yourself
Interactive runnable CSS example from EA Academy.
Live Output Preview:

CSS Example 4

html
Try it Yourself
Interactive runnable CSS example from EA Academy.
Live Output Preview:

CSS Example 5

html
Try it Yourself
Interactive runnable CSS example from EA Academy.
Live Output Preview: