SVG has some predefined shape elements that can be used by developers:
The following chapters will explain each element, starting with the rect element.
The <rect> element is used to create a rectangle and variations of a rectangle shape:
Here is the SVG code:
<svg width="400" height="110">
<rect width="300" height="100" style="fill:rgb(0,0,255);stroke-width:3;stroke:rgb(0,0,0)" />
</svg>
Try it Yourself »
Code explanation:
Let's look at another example that contains some new attributes:
Here is the SVG code:
<svg width="400" height="180">
<rect x="50" y="20" width="150" height="150"
style="fill:blue;stroke:pink;stroke-width:5;fill-opacity:0.1;stroke-opacity:0.9" />
</svg>
Try it Yourself »
Code explanation:
Define the opacity for the whole element:
Here is the SVG code:
<svg width="400" height="180">
<rect x="50" y="20" width="150" height="150"
style="fill:blue;stroke:pink;stroke-width:5;opacity:0.5" />
</svg>
Try it Yourself »
Code explanation:
Last example, create a rectangle with rounded corners:
Here is the SVG code:
<svg width="400" height="180">
<rect x="50" y="20" rx="20" ry="20" width="150" height="150"
style="fill:red;stroke:black;stroke-width:5;opacity:0.5" />
</svg>
Try it Yourself »
Code explanation: