SVG <rect>

SVG has some predefined shape elements that can be used and manipulated by developers.

SVG shapes

SVG has some predefined shape elements that can be used and manipulated by developers:

  • Rectangle <rect>
  • Circle <circle>
  • Ellipse <ellipse>
  • Line <line>
  • Polyline <polyline>
  • Polygon <polygon>
  • Path <path>

The following chapters will explain these elements, starting with the rectangle element.

The <rect> tag

The <rect> tag can be used to create rectangles and variants of rectangles.

To understand how it works, copy this code into a text editor, then save it as a "rect1.svg" file. Place this file in the web directory:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" 
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="100%" height="100%" version="1.1"
xmlns="http://www.w3.org/2000/svg">
<rect width="300" height="100"
style="fill:rgb(0,0,255);stroke-width:1;
stroke:rgb(0,0,0)"/>
</svg>

Code Explanation:

  • The width and height attributes of the rect element can define the height and width of the rectangle
  • The style attribute is used to define CSS properties
  • The CSS fill property defines the fill color of the rectangle (rgb values, color names, or hexadecimal values)
  • The CSS stroke-width property defines the width of the rectangle border
  • The CSS stroke property defines the color of the rectangle border

View this example

Let's look at another example that includes new properties:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" 
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="100%" height="100%" version="1.1"
xmlns="http://www.w3.org/2000/svg">
<rect x="20" y="20" width="250" height="250"
style="fill:blue;stroke:pink;stroke-width:5;
fill-opacity:0.1;stroke-opacity:0.9"/>
</svg>

Example explanation:

  • The x property defines the left position of the rectangle (for example, x="0" defines the distance from the left of the rectangle to the left of the browser window is 0px)
  • The y property defines the top position of the rectangle (for example, y="0" defines the distance from the top of the rectangle to the top of the browser window is 0px)
  • The CSS fill-opacity property defines the opacity of the fill color (the valid range is: 0 - 1)
  • The CSS stroke-opacity property defines the opacity of the stroke color (the valid range is: 0 - 1)

View Example

Define the opacity for the entire element:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" 
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="100%" height="100%" version="1.1"
xmlns="http://www.w3.org/2000/svg">
<rect x="20" y="20" width="250" height="250"
style="fill:blue;stroke:pink;stroke-width:5;
opacity:0.9"/>
</svg>

Code Explanation:

The opacity property of CSS defines the transparency value of the entire element (the valid range is: 0 - 1)

View Example

In the final example, create a rectangle with rounded corners:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" 
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="100%" height="100%" version="1.1"
xmlns="http://www.w3.org/2000/svg">
<rect x="20" y="20" rx="20" ry="20" width="250"
height="100" style="fill:red;stroke:black;
stroke-width:5;opacity:0.5"/>
</svg>

Code Explanation:

The rx and ry attributes can make the rectangle rounded.

View Example