View Box

You can use the viewBox attribute on these elements: <svg>, <marker>, <pattern>, <symbol>, <view>

The viewBox attribute defines the position (x, y) and dimension (width, height) of an SVG viewport.

SVG attributes

viewBox

Defines the position and dimension of an SVG viewport. Specify 4 numbers representing min-x, min-y, width, and height.

  • min-x and min-y can be negative.
  • Negative or zero of width or height disables rendering of an element.
| LIVE PREVIEW

preserveAspectRatio

Indicates how an element with a viewBox (or an <image> element) with a given aspect ratio must fit into a viewport with a different aspect ratio.

Specify none (just squeeze/stretch the content to fit the viewport), or an "alignment" and optionally a meet or slice value (this will preserve the aspect ratio).

  • An "alignment" value is in the form of x(Min|Mid|Max)Y(Min|Mid|Max):
    • Min means align the top/left
    • Mid means align the center
    • Max means align the bottom/right
  • meet (default) means the entire viewBox must be contained in the viewport ("contain").
  • slice means the viewBox must cover the entire viewport ("cover").
| LIVE PREVIEW

References