Skip to content

Instantly share code, notes, and snippets.

@pnavarrc
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pnavarrc/8f0e92c2cad9056a972e to your computer and use it in GitHub Desktop.
Save pnavarrc/8f0e92c2cad9056a972e to your computer and use it in GitHub Desktop.
Responsive SVG Image

Responsive SVG Image

This gist shows how to include an SVG image that adapts to the size of its container.

Whale

For this technique to work, the SVG element should have the attribute viewBox set with the image dimensions, and the attribute preserveAspectRatio="xMinYMin meet", and it shouldn't have the attributes width and height.

The container should have a width defined, as a percent or as a size in pixels.

<style>
    .img-container {
        width: 50%;
    }
</style>

The image can be wrapped in the container, preserving its aspect ratio and the container's width.

<div class="img-container">
    <img src="whale.svg" alt="whale">
</div>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Responsive SVG Images</title>
<style>
.img-container {
width: 50%;
}
</style>
</head>
<body>
<div class="img-container">
<img src="whale.svg" alt="whale">
</div>
</body>
</html>
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment