Explain Codes LogoExplain Codes Logo

Scale SVG to container without mask/crop

html
responsive-design
svg
performance
Alex KataevbyAlex Kataev·Mar 5, 2025
TLDR

Scaling an SVG? Two words: viewBox attribute. Set viewBox according to the SVG’s original dimensions. Apply some CSS magic with width: 100% and height: auto. The SVG becomes a stretching pro, filling its parent without skewing its shape:

<div style="width: 500px; height: 309px;"> <!-- Because 500x309px is just right 👌 --> <svg viewBox="0 0 100 100" width="100%" height="auto"> <!-- SVG content does its flexing here 💪 --> </svg> </div>

Where does the viewBox come in? It's the SVG's personal trainer, commanding the viewport dimensions and making sure the SVG stays in shape no matter its container size.

Preserving aesthetics with aspect ratios

Avoid SVG squishing or flattening with the preserveAspectRatio attribute. Slap on a value of xMidYMid meet, and you've got the SVG centered in its container like a diva on a stage:

<svg preserveAspectRatio="xMidYMid meet" viewBox="0 0 100 100" width="100%" height="auto"> <!-- Where the SVG diva performs 🎤 --> </svg>

This arrangement prevents any bit of your SVG from being snipped off, ensuring it's fully visible no matter the container size. Beautiful.

Stretching guidelines: When aspect ratios don't matter

Sometimes, you just want the SVG to cover the entire container, aspect ratios be damned. Thing is, SVG's got you covered there too. Make preserveAspectRatio none:

<svg preserveAspectRatio="none" viewBox="0 0 100 100" width="100%" height="100%"> <!-- SVG's stretching routine ✨ --> </svg>

With this little trick, your SVG fills the entire container, no cropping, no squishing.

Performance? There’s an SVG for that

SVGs might be slim, but they aren't always light. Complex SVGs can bog performance down, so consider streamlining your SVG or using a simplified version. Always test your SVG for responsiveness and performance across all devices.

Responsive SVG with JavaScript by your side

Sometimes, you need a helping hand. Or rather, a helping script. JavaScript can dynamically adjust the SVG’s dimensions, perfect for when CSS alone falls short:

function resizeSVG(svg, container) { let bbox = container.getBoundingClientRect(); svg.setAttribute('width', bbox.width); svg.setAttribute('height', bbox.height); } // Call resizeSVG with your SVG and container football team // (just kidding, only need those two elements 😉) resizeSVG(document.querySelector('svg'), document.querySelector('.container'));

This function adjusts the SVG's size to match the container exactly, even when the container's dimensions aren't known beforehand or dynamically change.

Multiple seasons, multiple sizes

Don't just consider spring; think winter, summer, and fall too. Your SVG needs to survive all container sizes. Play around with viewBox and preserveAspectRatio settings to make sure your SVG scales properly for any and every container size. At the risk of sounding like a broken record: test on multiple screen sizes and resolutions to ensure your SVG is universally adaptable.