unDraw for Websites

How to Make unDraw Illustrations Responsive with CSS

Make unDraw illustrations responsive with CSS: fluid sizing, aspect ratio, art direction with picture and srcset, and checks that stop artwork breaking a mobile layout.

The same colorful illustration shown at desktop, tablet, and phone widths inside one layout

An illustration that looks perfect on your laptop can overflow a phone screen, squash into the wrong proportions, or push a page sideways until the text is unreadable. Responsive design prevents all three. The good news is that illustrations are easier to make responsive than photographs: an SVG scales without losing quality, and a handful of CSS rules cover most layouts. This guide explains responsive unDraw illustrations CSS in the order you will use it, from the two line baseline that every image needs, through aspect ratio control, to srcset and the picture element for the cases where one file is not enough.

Everything here assumes the illustration is already on the page. If it is not, start with our guide to adding an unDraw SVG to HTML, then come back for the sizing work.

The baseline: fluid width, automatic height

Nearly every responsive image problem disappears with one rule:

.illustration {
  max-width: 100%;
  height: auto;
  display: block;
}

Each line earns its place. max-width: 100% caps the illustration at the width of its container, so it can shrink on a narrow screen but never spill out of it. height: auto tells the browser to work the height out from the width, preserving the artwork's proportions instead of stretching it. display: block removes the small strip of space browsers reserve under inline images for text descenders, which otherwise shows up as a mysterious gap in cards and hero sections.

Apply the class to the img element, or to the inline svg element if you pasted the code into your page. For SVG, the same rule works because the browser treats the viewBox as the artwork's intrinsic proportions. What you should not do is set both a fixed pixel width and a fixed pixel height in CSS. The moment those two numbers disagree with the drawing's real ratio, the browser obeys you and distorts the scene.

Reserve the space: width, height, and aspect-ratio

Fluid sizing solves overflow, but it does not tell the browser how much room to hold while the file loads. Without that information, the page paints text first and then shoves it down when the illustration arrives, a layout shift that is annoying to read through and easy to prevent.

The simplest fix lives in the HTML. Include the width and height attributes with values that match the artwork's proportions:

<img src="images/planning.svg" alt="A team planning
     a project on a board" width="880" height="620"
     class="illustration">

Modern browsers read those two attributes and compute the aspect ratio before downloading anything, reserving a correctly shaped box. Your CSS still controls the displayed width; the attributes only describe the shape. When the markup is generated by a template and attributes are awkward, the CSS aspect-ratio property does the same job:

.illustration-frame {
  aspect-ratio: 880 / 620;
  width: 100%;
}

Use one approach or the other, and keep the numbers honest. A wrong ratio is worse than none at all, because the browser will faithfully reserve the wrong shape and then crop or stretch to fill it. You can read the true proportions from the SVG's viewBox, which lists the coordinate width and height the artist drew against.

Let the container decide the size

In most layouts the illustration should not have a size of its own at all. Its container should. A common pattern is a two column hero that stacks on small screens:

.hero {
  display: grid;
  gap: 2rem;
}

@media (min-width: 800px) {
  .hero {
    grid-template-columns: 1fr 1fr;
    align-items: center;
  }
}

With the baseline rule from earlier applied to the image, the artwork fills its grid cell at every width: half the row on a desktop, the full row on a phone, always in proportion. This container first thinking is what makes responsive unDraw illustrations CSS manageable. You design the spaces, and the artwork follows.

Two refinements are worth knowing. First, a maximum size stops a wide scene from becoming enormous on a very large monitor: add something like max-width: 560px on the illustration or its wrapper when the design calls for it. Second, for a small spot illustration inside a text column, a relative width such as width: min(320px, 100%) gives it a calm fixed presence on desktop while still shrinking on phones.

When one file is not enough: srcset and sizes

SVG scales freely, so a single file serves every screen. PNG does not. A PNG downloaded from unDraw is one fixed rendering, and sending a wide version to every phone wastes data, while sending a narrow version to every desktop looks soft. The srcset and sizes attributes let you offer the browser several PNG exports and a hint about the display size, and the browser picks the closest match for the visitor's screen and pixel density.

<img src="images/planning-880.png"
     srcset="images/planning-440.png 440w,
             images/planning-880.png 880w,
             images/planning-1320.png 1320w"
     sizes="(max-width: 700px) 100vw, 50vw"
     alt="A team planning a project on a board"
     width="880" height="620" class="illustration">

Read it in two parts. The srcset list names each file and its real width in pixels, marked with the w descriptor. The sizes list tells the browser how wide the illustration will be displayed: full viewport width on screens up to 700 pixels, half the viewport above that. The browser combines the slot size with the screen's pixel density and chooses a file that is big enough without being wasteful. Keep the fallback src pointing at a middle size for older browsers.

To produce the variants, export the illustration a few times at different widths from your editor, or resize copies of the downloaded PNG downward. Never resize upward; an enlarged copy has no extra detail, only extra weight. If managing variants feels heavy for a small site, one well chosen PNG plus the fluid baseline is a respectable compromise, and our guide to optimizing unDraw illustrations explains where the real savings are.

Art direction with the picture element

Sometimes the problem is not resolution but composition. A wide scene with the subject in the middle can shrink until the people are tiny on a phone. The picture element solves this art direction problem by offering genuinely different images for different conditions, for example a tighter, more vertical crop on narrow screens:

<picture>
  <source media="(max-width: 640px)"
          srcset="images/planning-mobile.png">
  <img src="images/planning-wide.png"
       alt="A team planning a project on a board"
       width="880" height="620" class="illustration">
</picture>

The browser uses the first source whose media condition matches, and falls back to the img otherwise. The alt text, width, and height live on the img element, so keep them there. Preparing a second composition means opening the SVG in an editor and reframing the scene, which is real work; reserve picture for hero illustrations and other prominent placements where the improvement is visible, not for every spot graphic on the site. And remember that whichever version loads, it still needs a text alternative decision, which our alt text guide walks through.

SVG specific behavior worth knowing

Inline SVG and SVG loaded through an img element follow the same fluid rules, with two quirks. First, an SVG with a viewBox but no width or height of its own can behave unexpectedly in some older layouts, expanding to fill its container in one dimension only. The baseline rule plus an aspect ratio from the viewBox numbers prevents surprises. Second, very detailed scenes stay legible when shrunk only up to a point. A dashboard style illustration with small text like labels becomes unreadable at card size no matter how sharp the vector is. Choose simpler scenes for small slots, a selection skill our guide to finding the right unDraw illustration covers in depth.

Checks that stop a layout breaking on mobile

  • Test at a narrow width early. Use your browser's device toolbar at a common phone width and scroll the whole page. Sideways scrolling almost always traces back to an image or a fixed width element.
  • Search your CSS for fixed heights on images. A set height with an automatic width is fine for icons; on illustrations it invites distortion whenever the container narrows.
  • Check the hero first. The largest illustration is the most likely to overflow, to load late, or to shift the layout. Confirm its reserved space matches its ratio.
  • Watch long pages with many illustrations. Each one needs its ratio reserved, or the page will keep rearranging itself as the reader scrolls.
  • Verify PNG variants actually load. In developer tools, the network panel shows which srcset file the browser picked at each viewport width.
  • Do not upscale to fix blur. If the chosen PNG looks soft on a high density screen, add a larger variant to the srcset instead. The causes are explained in our blurry PNG guide.

Centering, alignment, and cards

Three layout patterns cover most of the places illustrations actually appear, and each has a small responsive trap worth knowing about.

Centered in a text column. A block level image with automatic side margins centers itself:

.illustration-centered {
  display: block;
  margin-inline: auto;
  width: min(480px, 100%);
}

The min() function is doing the responsive work: the illustration takes its intended 480 pixels when there is room, and the full container width when there is not. Without the percentage cap, a fixed width image is exactly how pages acquire horizontal scrolling on phones.

Beside text that wraps or sits in a row. On wide screens a flex or grid row puts text and artwork side by side. Give the image wrapper a flexible share of the row and a minimum width of zero, because flex items refuse to shrink below their content size by default, and an illustration's content size can be wider than the phone screen you are trying to fit. That single min-width: 0 on the wrapper fixes one of the most confusing overflow bugs in responsive layouts.

Uniform cards. When illustrations sit in a grid of cards, their natural ratios differ, and the rows look ragged. The usual solution is a fixed shape frame with the image fitted inside it:

.card-art {
  aspect-ratio: 4 / 3;
  width: 100%;
  object-fit: contain;
  background: #f4f1fa;
}

object-fit: contain letterboxes the whole scene inside the frame, which suits illustrations because nothing is cropped away. cover fills the frame and crops the edges; use it only for scenes whose subject sits safely in the center. A soft background color on the frame makes the letterboxing look designed rather than accidental.

Very wide and very tall artwork

Most unDraw scenes are landscape, but the library includes tall compositions, and a tall illustration in a wide column can grow absurdly tall on desktop, pushing the text far down the page. Cap the height and let the width follow: max-height: 70vh with width: auto on the image itself works when the image is not also constrained by the fluid baseline. The reverse problem, a panoramic scene on a phone, usually solves itself through the art direction approach with picture described earlier, because shrinking a panorama makes its subject vanish. In both directions the principle is the same: constrain the dimension that causes the problem, leave the other automatic, and the ratio you reserved does the rest.

Test like a visitor, not like a developer

Responsive failures hide at the widths you did not test. A short routine catches most of them. Resize the browser slowly from wide to narrow and watch for the moment anything jumps, overlaps, or scrolls sideways; that moment is a breakpoint your layout has not accounted for. Then check three specific widths: a small phone, a tablet in portrait, and a wide desktop, because those are the shapes your visitors actually hold. On each, confirm the illustration is neither tiny nor cropped, the text beside it is readable, and the page does not move as images load. Finally, throttle the network in developer tools once and reload: reserved space should hold the layout still while the artwork arrives, which is the visible proof that your width, height, and aspect ratio work is doing its job.

Frequently asked questions

Do I need media queries for every illustration?

No. The fluid baseline handles most images with no queries at all. Media queries enter when the layout around the image changes, for example when a two column hero stacks, and those queries belong to the layout components, not to each picture.

Why does my SVG ignore its container in one browser?

Usually because the SVG has explicit width and height attributes of its own, from an editor export, which override your intentions. Remove them or set them to 100 percent in the file or the markup, keep the viewBox, and let CSS size it.

Should illustrations use the same breakpoints as the rest of the site?

Yes. Artwork that changes size at widths where nothing else does looks like a bug. Reuse the layout's existing breakpoints so the whole page rearranges in one coherent step.

Sources and further reading