The Wayback Machine - https://web.archive.org/web/20241010070318/https://www.geeksforgeeks.org/svg-clippath-element/
Open In App

SVG <clipPath> Element

Last Updated : 17 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The <clipPath> SVG element is used to define a clipping path that is to be used by the clip-path property. It works the same as clip-path in CSS. The clipPath element is used to put some restriction on a region such that anything drawn outside that region will neither be visible nor be drawn.

Syntax

<clipPath> ... </clipPath>

Attributes

  • clipPathUnits: This attribute defines the coordinate system for the <clipPath> element’s content.

Below given are a few examples of the function given above.

Example 1:

html
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta property="viewport" content=
        "width=device-width, initial-scale=1.0" />

    <title>SVG clipPath Element</title>

    <style>
        @keyframes animation1 {
            from {
                width: 4px;
                height: 4px;
            }

            to {
                width: 150px;
                height: 150px;
            }
        }

        rect {
            animation: animation1 10s ease-in-out;
        }
    </style>
</head>

<body>
    <div style="width: 300px; height: 300px;">

        <h1 style="color: green;">
            GeeksforGeeks
        </h1>
        <h2>
            SVG clipPath Element
        </h2>

        <svg width="500" height="500" 
            transform="translate(100, 0)">
            <clipPath id="gfg">
                <rect width="4" height="4"></rect>
            </clipPath>
            <path id="path" d="M10, 30 A20, 20, 0, 0,
                         1, 50, 30 A20, 20, 0, 0, 1,
                         90, 30 Q90, 60, 50, 90 Q10, 
                         60, 10, 30 Z" />
            <use fill="green" clip-path="url(#gfg)" 
                href="#path" />
        </svg>
    </div>
</body>

</html>

Output:

SVG-clipPath-Element

Example 2:

html
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta property="viewport" content=
        "width=device-width, initial-scale=1.0" />

    <title>SVG clipPath Element</title>

    <style>
        @keyframes star {
            from {
                r: 0
            }

            to {
                r: 500px
            }
        }

        #gfg circle {
            animation: star 25s ease;
        }
    </style>
</head>

<body>
    <div style="width:300px; height:300px;">
        <h1 style="color:green">
            GeeksforGeeks
        </h1>

        <h2>SVG clipPath Element</h2>

        <svg width="500" height="500" 
            transform="translate(60, -40)">
            
            <clipPath id="gfg">
                <circle />
            </clipPath>
            
            <path id="star" d="
                M 85.000 105.000
                L 105.000 119.641
                L 102.321 95.000
                L 125.000 85.000
                L 102.321 75.000
                L 105.000 50.359
                L 85.000 65.000
                L 65.000 50.359
                L 67.679 75.000
                L 45.000 85.000
                L 67.679 95.000
                L 65.000 119.641
                z" />
            <use clip-path="url(#gfg)" 
                href="#star" fill="red" />
        </svg>
    </div>
</body>

</html>

Output:

SVG-clipPath-Element-2

Browsers Supported: The following browsers are supported by this SVG element:

  • Chrome 1
  • Edge 12
  • Firefox 1.5
  • Opera 9
  • Safari 3


Previous Article
Next Article

Similar Reads

Transpose Clippath CSS Keyframe animations into anime.js animation
Anime.js is a small, lightweight JavaScript library with a simple and small powerful API. It works with the DOM element, CSS, and JavaScript object. we use a library for making our work simple and easy. A library is a JavaScript document that contains a lot of functions, and those functions achieve some helpful tasks for your web-page. CDN link:
3 min read
SVG <svg> Element
SVG stands for Scalable Vector Graphic. It can be used to make graphics and animations like in HTML canvas. SVG graphics are supported by the <svg> element in HTML. SVG graphics feature a container in which we can create a variety of shapes such as boxes, pathways, text, graphic images, and circles. Syntax: <svg width="x" height="y"> sh
2 min read
SVG <linearGradient> Element
SVG <linearGradient> Element allows the developer to define linear gradients to apply SVG elements. It allows a smooth transition from one color to another. This is the most reliable technique. Linear gradients can be defined as vertical, angular gradients or horizontal: Vertical gradients are created when x1 and x2 are equal and y1 and y2 di
3 min read
SVG pattern Element
SVG (Scalable Vector Graphic) pattern is an element which defines the pattern in graphics using HTML. To work with create SVG pattern you have to define a <pattern> inside in an SVG after that, you can define the shapes inside of that <pattern>. Now have to define shapes area by specifying its color, borders, etc. Syntax of Declaration:
3 min read
SVG Path Element
SVG stands for Scalable Vector Graphic. The SVG element path is used to define a path that starts from a position and ends to a particular position. SVG path can be used to create any basic shapes. Syntax: <path d="Shape of path using keyword like M, L, C etc." pathLength="Length of path" stroke="stroke color name" fill="color name"> </pat
2 min read
SVG Polyline element
The <polyline> element of SVG in HTML is used to create a shape by connecting lines through different points. It is different from <polygon> as it can be used to create open shapes. Syntax: <polyline points="Pair of points required to draw the shape" stroke="stroke color" fill="fill color for colored closed shapes"> Attributes: po
1 min read
SVG defs Element
The <defs> in SVG is used whenever we want a particular element to render only when required or when it is called. objects that are created inside <defs> element are not rendered directly they are needed to be called by <use> element to render them on the browser. Syntax: <defs></defs> Property values: It does not have
2 min read
SVG polygon Element
SVG stands for Scalable Vector Graphic. It can be used to make graphics and animations like in HTML canvas. The <polygon> element of SVG is used to make any type of polygon on the SVG and defines a closed shape consisting of a set of connected straight line segments. Syntax: <polygon points="Pair of points required to draw the shape" strok
2 min read
SVG <marker> Element
The <marker> element in SVG is used to define the graphics that is mostly used for the drawing purpose. It may be used in graphs, charts for making arrowheads and polymarkers on a given path. Syntax: <marker></marker refX="" viewbox="" refY="" markerWidth="" markerHeight="" orient=""> Property Values: This element contains the fol
2 min read
SVG Circle Element
The SVG <circle> element is used to draw circle. The center point and radius are given. Syntax: <circle cx="x-axis co-ordinate" cy="y-axis co-ordinate" r="length" > </circle> Attributes: cx: x-axis co-ordinate of the center.cy: y-axis co-ordinate of the centerr: Radius of the circle. Example: C/C++ Code &lt;!DOCTYPE html&g
1 min read
SVG text Element
The SVG <text> element is used to draw text. There are some attributes to customize the text. Syntax: <text x="x-coordinates" y="y-coordinates" dx="list of lengths" dy="list of lengths" rotate="list of numbers" textlength="length" lengthAdjust="spacing" > </text> Attribute: x: x axis coordinates of glyphs.y: y axis coordinates of
1 min read
SVG line Element
The SVG <line> element is used to draw line. The start point and endpoints are given to draw the line. Syntax: <line x1="x-axis co-ordinate" y1="y-axis co-ordinate" x2="x-axis co-ordinate" y2="y-axis co-ordinate" > </line> Attribute: x1: x-axis start point.y1: y-axis start point.x2: x-axis end point.y2: y-axis end point. Example:
1 min read
SVG filter Element
The SVG <filter> element is used to define filters. To identify uniquely filter use id. The filters are defined in def elements. Syntax: <filter filterUnits="units to define filter effect region" primitiveUnits="units to define primitive filter subregion" x="x-axis co-ordinate" y="y-axis co-ordinate" width="length" height="length" filterRe
2 min read
SVG Ellipse Element
The SVG <ellipse> element is used to create an ellipse. The difference between circle and ellipse is that an ellipse has an x and a y radius that differs from each other, while a circle has equal x and y radius: Syntax: <ellipse cx="x-axis co-ordinate" cy="y-axis co-ordinate" rx="length" ry="length" > </ellipse> Attributes: cx: x-
1 min read
SVG <title> Element
The <title> SVG element provides an accessible, short-text description of any SVG container element or graphics element. Text in an <title> element is not rendered as part of the graphic, but browsers usually display it as a tooltip. When you hover over the element, the title of that element will be shown. Syntax: <title> TITLE HE
1 min read
SVG <animateMotion> Element
SVG stands for Scalable Vector Graphic. It can be used to make graphics and animations like in HTML canvas. The SVG <animateMotion> element let define how an element moves along a motion path. Syntax: <animateMotion values="" dur="" repeatCount="" path="" /> Attributes: keyPoints: This attribute indicates in the range [0, 1], how far is
1 min read
SVG <animate> Element
SVG stands for Scalable Vector Graphic. It can be used to make graphics and animations like in HTML canvas. The <animate> SVG element is used to animate an attribute or property of an element over time. It's normally inserted inside the element which we want to animate. Syntax: <animate attributeName="" values="" dur="" repeatCount="" /
1 min read
SVG <g> Element
The SVG <g> element is a container used to group other SVG elements. Transformations applied to the <g> element are also performed on its child elements, and its attributes are inherited by its children. Syntax: <g attributes="" > <elements> </g> Attributes: core Attributes: These attributes are core attributes like id
2 min read
SVG <textPath> Element
SVG stands for Scalable Vector Graphic. It can be used to make graphics and animations like in HTML canvas. The <textPath> SVG element is used to render the text along with a certain path. To render text along with a certain path, enclose the text in an <textPath> element that has a href attribute with a reference to the <path> el
2 min read
SVG <tspan> Element
SVG stands for Scalable Vector Graphic. It can be used to make graphics and animations like in HTML canvas. The SVG <tspan> element defines a subtext within a <text> element or another <tspan> element. It allows for adjustment of the style and/or position of that subtext as per user needs. Syntax: <tspan attributes="" > Subt
2 min read
SVG <use> Element
SVG stands for Scalable Vector Graphic. It can be used to make graphics and animations like in HTML canvas. The SVG <use> element takes nodes from within the SVG document and duplicates them somewhere else. Syntax: <use href="" > Subtext </use> Attribute: x: x-axis coordinates the positioning of the image.y: y-axis coordinates the
1 min read
SVG <symbol> Element
SVG stands for Scalable Vector Graphic. It can be used to make graphics and animations like in HTML canvas. The SVG <symbol> element is used to define graphical template objects which can be instantiated by the <use> element. The use of symbol elements for graphics that are used multiple times in the same document adds structure and sem
1 min read
SVG <switch> Element
The <switch> SVG element is used to evaluate attributes on its direct child elements in order and then renders the first child where these attributes evaluate to true. Other remaining children will not be rendered. Syntax: <switch attribute="" > <child 1> <child 2> <child 3> <child n> </switch> Attributes:
1 min read
SVG <foreignObject> Element
SVG stands for Scalable Vector Graphic. It can be used to make graphics and animations like in HTML canvas. The <foreignObject> element of SVG includes elements from a different XML namespace. It is an extensibility point which allows user agents to offer graphical rendering features beyond those which are defined within this specification. S
1 min read
SVG <script> Element
SVG stands for Scalable Vector Graphic. It can be used to make graphics and animations like in HTML canvas. The SVG <script> element allows adding scripts to an SVG document. Syntax: <script> // Script Here </script> Attributes: crossorigin: This defines the COS settings.href: This defines the href URL to load.type: Type of langua
1 min read
SVG <set> Element
SVG stands for Scalable Vector Graphic. It can be used to make graphics and animations like in HTML canvas. The SVG <set> element provides an easy means of just setting the value of an attribute for a specified duration. It supports all attribute types, including those that cannot reasonably be interpolated, such as string and boolean values.
1 min read
SVG <feSpotLight> Element
SVG stands for Scalable Vector Graphic. It can be used to make graphics and animations like in HTML canvas. The <feSpotLight> SVG defines a light source which results in a spotlight effect. It defines a spotlight whose light is directed at a specific point and it only lights a limited area. Syntax: <feSpotLight x="" y="" z="" pointsAtX=""
2 min read
SVG <feTurbulence> Element
The <feTurbulence> SVG filter generates noise which is helpful in simulating several natural phenomena like clouds, fire, and smoke, and in generating complex texture like marble or granite. The noise can be used to distort images and text. Perlin Turbulence function is used to generate Perlin Noise. Syntax: <feTurbulence type = "" baseFre
2 min read
SVG <style> Element
SVG stands for Scalable Vector Graphic. It can be used to make graphics and animations like in HTML canvas. The SVG <style> element allows style sheets to be embedded directly within the SVG content. Syntax: <style> Styling Content </style> Attribute: type: Type of stylesheet to be used.media: This attribute defines which media th
1 min read
SVG <feSpecularLighting> Element
The <feSpecularLighting> SVG filter primitive lights a source graphic using the alpha channel as a bump map. Syntax: <feSpecularLighting in ="" surfaceScale ="" specularConstant="" specularExponent="" kernelUnitLength="" > Attributes: in: It identifies input for the given filter primitive.surfaceScale: It represents the height of the su
2 min read