Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

SVG Rectangle

SVG

The element of SVG (Scalable Vector Graphics) is used to draw rectangles; it is one of the commonly used basic shapes in SVG. It lets you draw rectangles and control the position, size, rounded corners, and other styles by setting attributes.

Basic Syntax



  y="y-coordinate"        

  width="width-value"     

  height="height-value"   

  rx="rx-value"           

  ry="ry-value"           

  fill="fill-color"       

  stroke="stroke-color"   

  stroke-width="width-value" 

/>

Attribute Analysis:

The following code draws a rectangle with blue fill, black stroke, and a width of 2 pixels; the top-left corner is at (50, 50), the width is 100, and the height is 80.



  




Example 1

Below is the SVG code:

Example

 

  style="fill:rgb(0,0,255);stroke-width:1;stroke:rgb(0,0,0)"/>

Try it »

Click to view: View SVG File.

Code analysis:

Example 2

Let's look at another example that contains some new attributes:

Example

 

  style="fill:blue;stroke:pink;stroke-width:5;fill-opacity:0.1;

  stroke-opacity:0.9"/>

Try it »

Click to view: View the SVG file.

Code analysis:

Example 3

Define the opacity of the entire element:

Below is the SVG code:

Example

 

  style="fill:blue;stroke:pink;stroke-width:5;opacity:0.5"/>

Try it »

For Opera users:View the SVG file.


Example 4

The last example, creating a rectangle with rounded corners:

Below is the SVG code:

Example

 

  style="fill:red;stroke:black;stroke-width:5;opacity:0.5"/>

Try it »

For Opera users:View the SVG file.