XML technologies / SVG / Creating simple SVG graphics / Inserting and formatting rectangles and circles

(Excerpt from “Das Einsteigerseminar SVG — Webgrafiken mit XML” by Helma Spona)

Now apply the basic SVG elements in a small example: a webpage banner with a vertical navigation bar. The target is this 800 x 600 pixel graphic.

Target SVG example graphic

Figure: Target SVG example graphic.

Note:
Later chapters optimize this graphic and add interesting effects.

Creating a rounded rectangle as a page banner

First create a rectangle 800 pixels long and 100 pixels high, from the origin (0/0) to its bottom-right corner (800/100). Round the corners with the attributes rx and ry. The next figure shows the following code’s result.

<?xml version="1.0" standalone="no"?>
...
<svg width="800" height="600">
  <rect id="seitenbanner" x="0" y="0" width="800" height="100" rx="60" ry="100"/>
</svg>

Rectangle created as a page banner

Figure: Rectangle created as a page banner.

Adding the vertical navigation bar

Create a second rectangle for vertical navigation, also with rounded corners to match the horizontal bar. Insert this line below the first <rect> tag:

<rect id="navigationsleiste" x="0" y="0" width="130" height="600" rx="100" ry="75"/>

The browser should show this result.

Second rectangle for vertical navigation

Figure: Second rectangle for vertical navigation.

Overlaying a circle at the rectangles’ origin

Define a circle centered at the origin to connect the rectangles. Use the <circle> or <ellipse> tag. Insert the following code below the second <rect> tag:

<circle id="kreis" cx="0" cy="0" r="200"/>

Circle at the origin of the two rectangles

Figure: Circle at the origin of the two rectangles.

Note:
For overlapping elements, definition order in the SVG file determines which appears on top. Their insertion order therefore matters.

Formatting elements

The entirely black navigation bar and banner are unattractive because no formatting is defined. SVG Viewer fills elements with black by default. Set individual fill and border colors by adding a style attribute to all three elements, producing this code:

<?xml version="1.0" standalone="no"?>
...
<svg width="800" height="600">
  <rect id="seitenbanner" x="0" y="0" width="800" height="100" rx="60" ry="100" style="fill:rgb(255,0,0);stroke:rgb(128,0,0);stroke-width:1; "/>
  <rect id="navigationsleiste" x="0" y="0" width="130" height="600" rx="100" ry="75" style="fill:rgb(255,0,0);stroke:rgb(128,0,0);stroke-width:1; "/>
  <circle id="kreis" cx="0" cy="0" r="200" style="fill:rgb(192,0,0);stroke:rgb(128,0,0);stroke-width:1; "/>
</svg>

The style attribute defines fill color (fill), border color (stroke) and border width (stroke-width).

Note:
More formatting options exist; the most important are covered in later chapters.

The browser should now show this result.

Formatting result

Figure: Formatting result.

Adding a horizontal navigation bar

Finally add the small gray bar below the banner. It must lie beneath both the circle and banner, so place it before the first <rect> tag.

<rect id="hnavigationsleiste" x="0" y="80" width="720" height="40" rx="10" ry="10" style="fill:rgb(192,192,192);stroke:rgb(0,0,0);stroke-width:1"/>

The graphic is now complete and matches the target.

   

 

 

 

back to top