Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make svg image fits to parent container

Tags:

html

css

svg

I am using for the first time the svg <image> tag. I use this tag in order to apply a gray filter on the picture (thank you IE).

Here is my HTML :

<div class="container">
  <div class="item">
    <svg version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg">
        <image  xlink:href="https://dummyimage.com/400x400/000/00ffd5.png" />
    </svg>
  </div>
</div> 

And the scss :

.item {
  position: relative;
  width: 20%;
  height: 220px;

  background-color: blue;
}

svg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;

  image {
    width: 100%;
    height: 100%;
  }
}

I want to fit the image to the parent's container. Something like background: cover in css. Does anybody have an idea ?

You can check it out at : https://codepen.io/seabon/pen/QvBBrd

enter image description here

The blue background is for div.item

like image 274
Seabon Avatar asked May 16 '17 21:05

Seabon


People also ask

How do I make SVG fit to the parent container 100?

We use viewBox to make the SVG image scalable. viewBox = “0 0 100 100”: defines a coordinate system having x=0, y=0, width=100 units and height=100 units. Thus, the SVG containing a rectangle having width=50px and height=50px will fill up the height and width of the SVG image, and all the dimensions get scaled equally.

How do I make SVG fit inside a div?

Set the CSS attribute position:absolute on your <svg> element to cause it to sit inside and fill your div. (If necessary, also apply left:0; top:0; width:100%; height:100% .)

How do you scale svgs?

Just set the viewBox on your <svg> , and set one of height or width to auto . The browser will adjust it so that the overall aspect ratio matches the viewBox .

How do I fix SVG size?

❓ How can I resize a SVG image? First, you need to add a SVG image file: drag & drop your SVG image file or click inside the white area to choose a file. Then adjust resize settings, and click the "Resize" button. After the process completes, you can download your result file.


3 Answers

Just set an appropriate viewBox attribute on your SVG. Then set preserveAspectRatio to a value that uses the slice option, instead of the default meet. For example:

preserveAspectRatio="xMidYMid slice"

This is equivalent to CSS's background-size: cover.

BTW, setting width and height of the <image> via CSS will only work on Chrome right now. You'll need to assign those in the SVG if you want your example to work in other browsers.

.item {
  position: relative;
  width: 20%;
  height: 220px;

  background-color: blue;
}

svg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}
<div class="container">
  <div class="item">
    <svg viewBox="0 0 400 400" preserveAspectRatio="xMidYMid slice">
        <image xlink:href="https://dummyimage.com/400x400/000/00ffd5.png"
               width="400" height="400"/>
    </svg>
  </div>
</div>
like image 20
Paul LeBeau Avatar answered Oct 12 '22 04:10

Paul LeBeau


UPDATED

  • Add width="100%" and height="100%" attributes.
  • Add preserveAspectRatio 1 with value of none2 .

    none do not force uniform scaling. Scale the graphic content of the given element non-uniformly if necessary such that the element's bounding box exactly matches the viewport rectangle.
    Note: if <align> is none, then the optional <meetOrSlice> value is ignored.

jsFiddle - tested on Chrome, FF and IE11

.item { position: relative; width: 20%; height: 220px; background-color: blue; }
svg { position: absolute; top: 0; left: 0; width: 100%; height: 100%; }
<div class="item">
  <svg id="theSVG" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg">
      <image width="100%" height="100%" xlink:href="//dummyimage.com/400x400/000/00ffd5.png" preserveAspectRatio="none" />
    </svg>
</div>

Notes:

  1. The use of this preserveAspectRatio attribute was initially offered by another answer - I forgot the user, many thanks to him though- and it got deleted, in that answer the attribute was preserveAspectRatio="xMidYMid slice" but without using the width="100%" height="100%", except the ones in CSS which don't work except [partially, because if you resize the view larger the blue background will start to show up] in Chrome as stated in @Paul LeBeau answer. this jsFiddle Demo represents how that nice answer was.
  2. none does not force the scale to be uniformed, it may squish or stretch the image to make it fits, While it doesn't make any difference in the OP case, if Keeping aspect ratio is needed, then any of the other Min, Mid and Max for x or y could be used but it must be accompanied with slice only and not meet.
like image 121
Mi-Creativity Avatar answered Oct 12 '22 05:10

Mi-Creativity


If you don't have to use image tab there is my solution with CSS:

.item-image{
 width:100%;
 height:100%;
 background: url(https://dummyimage.com/400x400/000/00ffd5.png) center;
background-size: 100% 100% ;
}

- don't forget create div with the item-image class, between svg tags.

Codepen

There is a website where you can play with background-size. You can change the background of each element by assigning an id to div and add an attribute with JS.

like image 1
Piotr Bryla Avatar answered Oct 12 '22 03:10

Piotr Bryla