Source code

Revision control

Copy as Markdown

Other Tools

Test Info:

<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<title>preserveAspectRatio on an image element fits a referenced SVG that has only a viewBox</title>
<link rel="author" title="Sam Weinig" href="mailto:sam@webkit.org">
<link rel="match" href="image-preserveaspectratio-meet-svg-viewbox-only-ref.html">
<meta name="assert" content="A referenced SVG with only a viewBox has a natural aspect ratio but no natural size. The image element's preserveAspectRatio fits that ratio into the positioning rectangle: with xMinYMid meet a square viewBox in a 200x100 rectangle is a 100x100 square at the left edge.">
<style>
body { margin: 0; }
</style>
<!-- Red is painted where the image must be, so an image drawn anywhere else, or not cropped, leaves red showing. -->
<svg width="220" height="120">
<rect x="10" y="10" width="100" height="100" fill="red"/>
<image x="10" y="10" width="200" height="100" preserveAspectRatio="xMinYMid meet" href="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'><rect width='100' height='100' fill='rgb(0,128,0)'/></svg>"/>
</svg>