Mastering SVG viewBox, width, height, and preserveAspectRatio
Fix clipped, stretched, or weirdly scaled SVGs once and for all by understanding how the SVG viewBox coordinate system interacts with CSS dimensions.
What the viewBox Attribute Actually Defines
The viewBox attribute takes four numbers: min-x, min-y, width, and height—for example, viewBox='0 0 200 100'. Think of viewBox as the internal coordinate canvas where your vector paths live, completely independent of how many physical pixels the SVG occupies on screen.
A path drawn from (0, 0) to (200, 100) fills the entire viewBox whether the SVG is displayed at 40px wide in a mobile navbar or 1200px wide on a desktop hero banner.
Why Missing a viewBox Causes Clipped SVGs
A classic frontend bug happens when an exported SVG has width='200' height='100' but no viewBox attribute. If you try to shrink it with CSS (width: 100px; height: 50px;), the browser does not scale the artwork down—it simply crops the viewport, cutting off the right and bottom halves of your logo.
Always ensure every production SVG includes an accurate viewBox. Once viewBox is present, you can safely remove fixed width and height attributes (or override them in CSS) and the graphic will scale proportionally.
Controlling Aspect Ratio with preserveAspectRatio
When the CSS box holding an SVG has a different aspect ratio than the viewBox, the browser defaults to preserveAspectRatio='xMidYMid meet'. This behaves like CSS object-fit: contain—scaling the graphic as large as possible while keeping the entire drawing centered and unclipped.
If you want the SVG to fill the entire container like object-fit: cover, set preserveAspectRatio='xMidYMid slice'. For decorative wave dividers that should intentionally stretch to match any viewport width, use preserveAspectRatio='none'.
Preventing Layout Shift (CLS) on Responsive SVGs
Removing width and height makes an SVG fluid, but remember to give its wrapper or <img> tag an explicit width or aspect-ratio in CSS. That way the browser reserves the exact layout space before the image loads, keeping Cumulative Layout Shift at zero.
Make Your SVGs Responsive Automatically
Use our SVG Optimizer to preserve viewBox while stripping rigid width and height attributes.