Table of Contents
With Specifying Width and Height on SVG Element
You can create viewport in SVG element with specifying width and height using different types of unit identifiers as follows.
Using Custom Unit Identifiers
Following is an example which uses “mm” as unit specifiers for with and height properties of <svg> element, which creates a viewport of 60mm x 30mm size. The rectangle created within <svg> elment is smaller than the viewport, so it fits within a viewport.
<svg width=”60mm” height=”30mm”>
<rect width=”500″ height=”500″ fill=”pink”/>
<rect x=”10″ y=”10″ width=”200″ height=”90″
style=”fill: none; stroke: blue;stroke-width:2;” />
</svg>
<svg width=”60mm” height=”30mm”>
<rect width=”500″ height=”500″ fill=”pink”/>
<rect x=”10″ y=”10″ width=”200″ height=”120″
style=”fill: none; stroke: blue;stroke-width:2;” />
</svg>
<svg width=”7.2cm” height=”130pt”>
<rect width=”500″ height=”500″ fill=”pink”/>
<rect x=”10″ y=”10″ width=”250″ height=”150″
style=”fill: blue; stroke: red;” />
</svg>
Using Default Coordinates
Following is an example of specifying properties of SVG elements using default coordinates, which is in pixel(px).
<svg width=”280″ height=”180″>
<rect width=”500″ height=”500″ fill=”pink”/>
<circle r=”80″ cx=”130″ cy=”90″
style=”fill: blue; stroke: red; stroke-width:2;” />
</svg>
With Using a viewBox
Using a viewbox is the another way of specifying user coordinates for a viewport. To create a viewbox you will set the viewBox attribute on the <svg> element. The value of the viewBox attribute is a list of four numbers <min-x>, <min-y>, <width> and <height> separated by whitespace and/or a comma, which specify a rectangle in user space which should be mapped to the bounds of the viewport established by the given element. Each single unit specified in viewbox attribute assigns one-sixteenth of a centimeter.
Using viewbox attribute, you can also specify that a given set of graphics stretch to fit a particular container element. All elements that establish a new viewport have attribute viewBox. A negative value for <width> or <height> generates error and a value of zero disables rendering of the element.
Example 1:
<svg width=”8cm” height=”5cm” viewBox=”0 0 80 80″>
<rect width=”500″ height=”500″ fill=”pink”/>
<circle r=”8″ cx=”45″ cy=”20″ style=”stroke: black; fill: none;”/>
<rect x=”35″ y=”28″ width=”20″ height=”20″
style=”stroke: black; fill: none;” />
<rect x=”38″ y=”48″ width=”5″ height=”10″
style=”stroke: black; fill: none;” />
<rect x=”48″ y=”48″ width=”5″ height=”10″
style=”stroke: black; fill: none;” />
</svg>
<svg width=”8cm” height=”5cm” viewBox=”0 0 160 160″>
<rect width=”500″ height=”500″ fill=”pink”/>
<circle r=”8″ cx=”45″ cy=”20″ style=”stroke: black; fill: none;”/>
<rect x=”35″ y=”28″ width=”20″ height=”20″
style=”stroke: black; fill: none;” />
<rect x=”38″ y=”48″ width=”5″ height=”10″
style=”stroke: black; fill: none;” />
<rect x=”48″ y=”48″ width=”5″ height=”10″
style=”stroke: black; fill: none;” />
</svg>
<svg width=”8cm” height=”5cm” viewBox=”0 0 40 40″>
<rect width=”500″ height=”500″ fill=”pink”/>
<circle r=”8″ cx=”45″ cy=”20″ style=”stroke: black; fill: none;”/>
<rect x=”35″ y=”28″ width=”20″ height=”20″
style=”stroke: black; fill: none;” />
<rect x=”38″ y=”48″ width=”5″ height=”10″
style=”stroke: black; fill: none;” />
<rect x=”48″ y=”48″ width=”5″ height=”10″
style=”stroke: black; fill: none;” />
</svg>
Example 2:
Here the triangle created on SVG below fits within viewport created with viewbox.
<svg width=”300px” height=”200px” viewBox=”0 0 1500 1000″>
<rect x=”0″ y=”0″ width=”1500″ height=”1000″
fill=”#00aaff” stroke=”blue” stroke-width=”12″/>
<polygon points=”750,50 50,950 1450,950″ fill=”red”/>
</svg>
<svg width=”150px” height=”100px” viewBox=”0 0 1500 1000″>
<rect x=”0″ y=”0″ width=”1500″ height=”1000″
fill=”#00aaff” stroke=”blue” stroke-width=”12″/>
<polygon points=”750,50 50,950 1450,950″ fill=”red”/>
</svg>
Read Next:How To Specify Alignment for PreserveAspectRatio in SVG
Comments are closed.