Why does background-size:100% 100%; not work on this SVG?
Asked Answered
T

2

7

Look at the example:

.container {
   background-image:url("http://svgshare.com/i/3cM.svg");
   background-position:center center;
   background-repeat:no-repeat;
   width:400px;
   height:200px;
   background-color:#eef;
   border-bottom:1px solid #000;
   background-size:100% 100%;
}
<div id="container1" class="container"></div>

Related question: Stretch a background image (SVG) to 100% width and 100% height?

Trammel answered 27/10, 2017 at 16:3 Comment(3)
I believe you have to nest another elem within the container using 100%Pisgah
Hmm in my live example this does not work either. There must be a problem with certain SVG files?Trammel
jsfiddle.net/JknDr/123 - this is working for a me on Chrome. if height is the issue, yeah you may need to find an svg that scales more proportionally.Pisgah
V
22

Open your .svg file and set

preserveAspectRatio="none"

within the SVG tag.

Varve answered 27/10, 2017 at 16:9 Comment(2)
Found related: #9334595Trammel
this was driving me crazy. thank you for the solution!Spikes
D
0

As suggested in a comment, wrap the svg container inside another one and give the wrapper the dimensions you need. In this case, I set the wrapper to 100vw width and 100vh height. Change the wrapper dimensions and notice that the svg will follow.

html, body {
  padding:0;
  margin:0;
}

.wrapper {
  width:100vw;
  height:100vh;
}

.container {
   background-image:url("http://svgshare.com/i/3cM.svg");
   background-position:center center;
   background-repeat:no-repeat;
   width:100%;
   height:100%;
   background-color:#eef;
   border-bottom:1px solid #000;
   background-size:100% 100%;
}
<div class="wrapper">
  <div id="container1" class="container"></div>
</div>
Dentalium answered 27/10, 2017 at 16:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.