Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use preserveAspectRatio to achieve this affect in a limited way. But you are limited by the positioning options that preserveAspectRatio provides. So as long as your sprite has a maximum of 3x3 images or are positioned at the corners or sides, it would work.</p> <p>The are a couple of other ways I can think of to achieve the same effect in a more flexible way.</p> <ul> <li>Use the clip or clip-path style properties along with careful positioning of the image on the page</li> <li>Embed the image inside another <code>&lt;svg&gt;</code> element and use viewBox to select the part of the sprite you want.</li> </ul> <p>The following example demonstrates the three main techniques above.</p> <pre><code>&lt;?xml version="1.0" standalone="no"?&gt; &lt;!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"&gt; &lt;svg width="8cm" height="8cm" viewBox="0 0 400 400" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"&gt; &lt;desc&gt;Testing image elements&lt;/desc&gt; &lt;!-- Outline the drawing area in blue --&gt; &lt;rect fill="none" stroke="blue" x="1" y="1" width="398" height="398"/&gt; &lt;!-- Use preserveAspectRatio to show the top 64 pixels of the image (Stack Overflow logo) --&gt; &lt;image x="100px" y="100px" width="238px" height="64px" xlink:href="http://cdn.sstatic.net/stackoverflow/img/sprites.png" preserveAspectRatio="xMinYMin slice"/&gt; &lt;!-- Use a CSS clip rectangle to show a small facebook logo from the sprite. Logo is at 150,1000 with dimensions 19x19. Positioned at 100,200 in the SVG (-50+150, -800+1000). Could also use a clip-path for this. --&gt; &lt;image x="-50px" y="-800px" width="238px" height="1073px" xlink:href="http://cdn.sstatic.net/stackoverflow/img/sprites.png" clip="rect(200 100 219 119)" /&gt; &lt;!-- Use a svg viewBox to show the facebook logo from the sprite. By setting our viewBox to the bounds of the logo, the renderer will scale it to fit the specified width and height. Which in this case is 19x19 - the same size as the sprite. --&gt; &lt;svg x="100px" y="300px" width="19px" height="19px" viewBox="150 1000 19 19" version="1.1"&gt; &lt;image x="0px" y="0px" width="238px" height="1073px" xlink:href="http://cdn.sstatic.net/stackoverflow/img/sprites.png" /&gt; &lt;/svg&gt; &lt;/svg&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload