๐Ÿ–ผ๏ธ Understanding SVG `viewport` vs `viewBox`

๐Ÿ–ผ๏ธ Understanding SVG `viewport` vs `viewBox`


โœ… Viewport

The viewport is defined by the width and height of the SVG element in HTML or CSS.

<svg width="200" height="100">...</svg>

It controls the display size of the SVG on the page.


โœ… viewBox

The viewBox attribute defines the internal coordinate system of the SVG.

<svg viewBox="0 0 100 50">...</svg>

It tells the browser:

โ€œFit this 100ร—50 unit space into the given viewport size.โ€


๐Ÿ” viewBox Syntax

viewBox = "min-x min-y width height"
  • min-x, min-y: where the view starts
  • width, height: how much of the SVG canvas is visible

๐Ÿ” Scaling Example

<svg width="400" height="200" viewBox="0 0 100 50">
  <!-- Content here -->
</svg>
  • Viewport: 400ร—200 px
  • ViewBox: 100ร—50 units
  • Scale: Each SVG unit = 4px

Understanding this distinction helps you create responsive, scalable SVGs.