Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

White Space Between SVG Background Repeats

Tags:

html

css

svg

I cannot seem to get rid of this white space between SVG element repeats (as CSS backgrounds) exported by Illustrator CS5.

I am using a simple CSS background-image and background-repeat: repeat-x but there is always this white space between each repeat. I am not using background-size at all.

Here is what I am seeing (tested on Safari/Chrome):

white space

If I open the SVG to check for any whitespace on the side, nothing is there (see window on the right) and it goes all the way to the side of the window:

no white space on separate window

I've tried saving the image in Illustrator by File -> Scripts -> SaveDocsAsSVG and Save as.. -> SVG.

HTML

<body>
    <div class="outer">
        <div class="inner">
        </div>
    </div>
</body>

CSS

.outer {
  background-color: #7fcefb;
  height: 700px;
  background-image: url('/wp-content/themes/grizzly/assets/img/trees.png');
  background-position: center 95%;
  background-repeat: repeat-x;
}

.inner {
  background-image: url('/wp-content/themes/grizzly/assets/svg/treeshill.svg');
  height: 700px;
  background-position: center bottom;
  background-repeat: repeat-x;
}
like image 549
roflmyeggo Avatar asked Feb 10 '15 01:02

roflmyeggo


People also ask

How do I stop background image repetition?

To make a background image not repeat in HTML, specify no-repeat in the background-repeat property or the background shorthand property. The background image is set to 'no-repeat' using the 'background-repeat' property. The above example uses the background-repeat property to set the image to no-repeat .

How do you repeat an SVG in HTML?

It seems that some browsers do have trouble with repeating backgrounds made from SVG images. One solution to this is to do the repeating inside the SVG instead. We can use a <pattern> element to define the repeating pattern and use that to fill a rectangle.

Can you set an SVG as a background image?

SVG images can be used as background-image in CSS as well, just like PNG, JPG, or GIF. All the same awesomeness of SVG comes along for the ride, like flexibility while retaining sharpness. Plus you can do anything a raster graphic can do, like repeat.

Does SVG have background color?

The SVG background is used to draw any kind of shape, set any color you want by the set property. The below examples illustrates the concept of SVG set background-color more specifically. The SVG allowed the CSS background sizing, position, and much more complex property.


2 Answers

If it's what I think it is, open up your SVG file and change the root <svg> element so that it includes the following attribute:

<svg ... preserveAspectRatio="none">

If it already has a preserveAspectRatio attribute, just change it to "none" and reload.

like image 172
Paul LeBeau Avatar answered Oct 14 '22 04:10

Paul LeBeau


preserveAspectRatio="none" didn't work for me because it makes the svg to take the full width of its parent container.

Instead a take the parameters: preserveAspectRatio="xMinYMax meet" which in return I was able to use with background-repeat: repeat-x;

For more info here is an exellent article: A Look At preserveAspectRatio in SVG

like image 42
luis19mx Avatar answered Oct 14 '22 02:10

luis19mx