Introduction
Before reading further, read the following article.
SVG Rectangle
The tag <rect> represents a rectangle in SVG and by this, you can draw various types of rectangles like various widths & heights or with various strokes or color or sharp & rounded corner rectangles, etc.
Parameters used
Let’s see an example to see all the parameters used in an SVG rectangle.
Where:
- <rect x="50" y="20" rx="20" ry="20" width="150" height="150"
- style="fill:red;stroke:black;stroke-width:5;opacity:0.5"
- class="style class" >
- </rect>
- <rect> declares the rectangle.
- x & y coordinates of the top-left corner of the rectangle and the default is (0,0).
- Width & height of the rectangle.
- rx & ry rounds the corners of the rectangle.
- Style specifies the inline style.
- Class specifies the external name to the tag.
Let’s see the first example of an SVG rectangle.
Example 1
Output
- <html>
- <body>
- <svg width="200" height="100">
- <rect width="200" height="100" style="fill:#00c800;stroke-width:5;stroke:#c80000">
- </svg>
- </body>
- </html>

- <html>
- <body>
- <h1> Different Rounded Corners</h1>
- <svg width="500" height="200">
- <rect x="20" y="20" rx="10" ry="10" width="150" height="80" style="fill:blue;stroke:red;stroke-width:5"/>
- <rect x="180" y="20" rx="20" ry="20" width="150" height="80" style="fill:grey;stroke:orange;stroke-width:5"/>
- <rect x="340" y="20" rx="30" ry="30" width="150" height="80" style="fill:cyan;stroke:green;stroke-width:5">
- </svg>
- </body>
- </html>

- <html>
- <body>
- <h1>Different corner rounding coordinates</h1>
- <svg width="500" height="300">
- <rect x="20" y="20" rx="5" ry="15" width="100" height="70" style="fill:grey;stroke:green;stroke-width:5"/>
- <rect x="180" y="20" rx="10" ry="30" width="100" height="70" style="fill:pink;stroke:orange;stroke-width:5"/>
- <rect x="340" y="20" rx="20" ry="50" width="100" height="70" style="fill:yellow;stroke:black;stroke-width:5">
- </svg>
- </body>
- </html>

- <html>
- <body>
- <h1>Overlapped Rounded Corners</h1>
- <svg width="300" height="200">
- <rect x="30" y="20" rx="10" ry="10" width="200" height="150" style="fill:blue;stroke:red;stroke-width:15"/>
- <rect x="30" y="20" rx="20" ry="20" width="200" height="150" style="fill:grey;stroke:orange;stroke-width:10"/>
- <rect x="30" y="20" rx="30" ry="30" width="200" height="150" style="fill:cyan;stroke:green;stroke-width:5">
- </svg>
- </body>
- </html>

Example 5
Rectangle with opacity.
Output
- <html>
- <body>
- <h1>Rectangle with opacity</h1>
- <svg width="300" height="200">
- <rect x="50" y="20" width="200" height="150" style="fill:green;stroke:blue;stroke-width:5;fill-opacity:0.4;stroke-opacity:0.7">
- </svg>
- </body>
- </html>

- <html>
- <body>
- <h1>Overlapped with opacity</h1>
- <svg width="400" height="300">
- <rect x="20" y="20" width="150" height="100" style="fill:orange;stroke:red;stroke-width:5;opacity:0.5"/>
- <rect x="90" y="60" width="200" height="100" style="fill:blue;stroke:black;stroke-width:5;opacity:0.5"/>
- <rect x="240" y="20" width="150" height="100" style="fill:green;stroke:cyan;stroke-width:5;opacity:0.5"/>
- </svg>
- </body>
- </html>

- <html>
- <body>
- <h1>Rectangle with dashed strokes</h1>
- <svg width="400" height="300">
- <rect x="20" y="20" width="150" height="80" style="fill:none;stroke:red;stroke-width:3;stroke-dasharray: 10 5"/>
- <rect x="180" y="20" width="150" height="80" style="fill:orange;stroke:blue;fill-opacity:0.6;stroke-width:3;stroke-dasharray: 10 5"/>
- </svg>
- </body>
- </html>

Example 8
Rectangles with the transformation (rotation).
- <html>
- <body>
- <svg viewBox = "0 0 200 200" version = "1.1">
- <rect x = "20" y = "40" width = "30" height = "20" fill = "lightgreen" stroke = "tomato" stroke-width = "1" transform = "rotate(-45 100 100)"/>
- </svg>
- </body>
- </html>
Output:

- <html>
- <body>
- <svg viewBox = "0 0 200 200" version = "1.1">
- <rect x = "25" y = "25" width = "30" height = "20" fill = "#0f0fff" stroke = "#0f0f00" stroke-width = "1" transform = "rotate(-150 30 30)"/>
- </svg>
- </body>
- </html>
Output:

Want to read more...
Thank you, keep learning and sharing.

Vipul MalhotraPosted Aug 19, 2015, 7:47 AM
nice article.Thanks for sharing
Vaikesh K PPosted Aug 19, 2015, 7:23 AM
Nice one sir :)
Rajeesh MenothPosted Aug 19, 2015, 7:23 AM
Nice buddy
Sandeep KumarPosted Aug 19, 2015, 7:03 AM
Nice article sir :)
Karthikeyan KPosted Aug 19, 2015, 6:31 AM
Good one sir...Thanks for share
Gopi ChandPosted Jul 17, 2015, 7:58 AM
Thanks to everyone :)
Nilesh JadavPosted Jul 17, 2015, 6:17 AM
Nice one sir
Santhakumar MunuswamyPosted Jul 13, 2015, 3:11 PM
Nice article! Thanks for sharing
Sibeesh VenuPosted Jul 13, 2015, 3:30 AM
Nice share \
Debasis SahaPosted Jul 13, 2015, 12:41 AM
Good One..
Pankaj Kumar ChoudharyPosted Jul 12, 2015, 10:37 PM
Nice Explain Sir........