SVG For Responsive Web Design

During the last decade internet access using mobile devices such as smartphones and tablets has increased significantly. Responsive design is a method that causes a web page to adjust its display size depending upon the screen size of the visitors device. Since SVG actually stands for Scalable Vector Graphics, it would make sense that SVG would be ideal for that purpose.

One way to make an SVG drawing resize itself according to the users screen size is to the viewBox attribute in the <svg> container element. The syntax for the viewBox attribute is shown below:

viewBox = "<min-x> <min-y> <width> <height>"

Where the <min-x> and <min-y> values determine the upper left corner of the viewbox, and the <width> and <height> values determine the width and height of that viewBox. Now you might note immediately that these values allow you to specify an area of the parent <svg> element. Only that specified area of the parent <svg> element would be visible. However, that's not our purpose with this example.

We want our entire image to be visible, and to adjust its size to the users display size. To do that we need to also add the preserveAspectRatio attribute in the <svg> container element. The syntax for the preserveAspectRatio attribute is shown below:

preserveAspectRatio ="<align> [<meetOrSlice>]"

Where the <align> value sets the viewBox's alignment within the <svg> container element. The value <xMinYMin> aligns the minimum x and minimum y of the view box with the left-top edge of the viewport. The <meetOrSlice> value defines how the aspect ratio is to be preserved. Set to <meet> it preserves the aspect ratio and scales view box to fit within viewport.

The example above shows my simple hand-coded SVG drawing. By resizing your browser window you can observe the the responsive resizing affect. Shown below is the SVG code for this example.

<svg viewBox="0 0 800 1200" preserveAspectRatio="xMinYMin meet" style="border: 1px solid #cccccc;">
<path d="M392,689 Q510,403 430,51" stroke="black" stroke-width="2" fill="none" />
<line x1="430" y1="51" x2="674" y2="782" stroke="black" stroke-width="2" fill="none" />
<path d="M674,782 Q507,768 392,689" stroke="black" stroke-width="2" fill="none" />
<path d="M135,520 Q276,366 414,50" stroke="black" stroke-width="2" fill="none" />
<line x1="414" y1="50" x2="414" y2="620" stroke="black" stroke-width="2" fill="none" />
<line x1="414" y1="620" x2="133" y2="520" stroke="black" stroke-width="2" fill="none" />
<line x1="15" y1="567" x2="146" y2="558"stroke="black" stroke-width="2" fill="none" />
<path d="M146,558 Q450,610 675,785" stroke="black" stroke-width="2" fill="none" />
<path d="M675,785 Q350,762 10,568" stroke="black" stroke-width="2" fill="none" />
<path d="M 10,569 C 1,720 600,910 673,787" stroke="black" stroke-width="2" fill="none" />
<rect x="415" y="40" width="15" height="650" stroke="black" stroke-width="2" fill="black" />
<polygon points="132,514 428,616 428,630 135,528" stroke="black" stroke-width="2" fill="black" />
</svg>