background-size:100% 100%; doesn't work properly in Chrome
Asked Answered
P

4

33

I'm using an svg image as a background. I'm trying to use CSS3's background-size: 100% 100%; but it doesn't seem to work, even in browsers which should support it (like Chrome).

If you look at this site you'll see the #special-post article (to the right/below the food image) with a red banner-looking background. Notice that as you shrink the window down, the height of the background image drops to retain it's proportions, rather then stretching, as I would like.

EDIT: I checked this on FireFox and it works correctly... so this appears to be a webkit issue.

EDIT: I checked this on Safari and it works! So it looks like my problem is specific to Chrome.

(PS: I'm familiar with this alternative solution, using an img tag, but I'd rather not use it.)

Paintbox answered 17/2, 2012 at 19:22 Comment(0)
G
97

Here's a workaround:

Open your .svg file, find the <svg> tag at the beginning and add the following property inside it:

preserveAspectRatio="none"

Source: http://www.yootheme.com/support/question/6801?order=modified

Garbanzo answered 1/7, 2012 at 10:15 Comment(6)
Awesome! I would upvote 10 times if I could. Thanks!Potheen
Thank you for this tip, i couldn't wrap my head arround what I was doing wrong.Reservist
I cannot believe this worked. Things like this never work for me. Thank you.Sapele
Chrome still has this bug & this answer fixes it!Wilsonwilt
Warning: this attrbute seems to be caseSenistive.Cutting
I was starting to doubt the world before I saw this answer.Willyt
I
2
background-size: cover

Works across the board. Tested w/ Safari, Chrome, & FF.

Impunity answered 18/2, 2012 at 4:39 Comment(3)
It works, but it doesn't do what I'm trying to accomplish. cover scales UP the entire image proportionally to fill the space. I want the image to distort so that it fits the dimensions of the article.Paintbox
In more concrete terms, I want all four points of the red background image to stay visible, just stretched/compressed to match the height of the article.Paintbox
Consider using a PNG instead of an SVG. I know this is kind of lame advice, but it might just be the only practical way.Impunity
P
2

It turns out this is a known bug in Chrome that is specific to svg background images. I'm running v 17.0.963.56, in case anyone cares, and you can track the bug here.

Paintbox answered 18/2, 2012 at 18:28 Comment(1)
It's not specific to SVG images. I'm having the same issue with a JPEG.Accursed
P
1

This is still an issue in Chrome 39, but not in Firefox. I ran into it when scaling a background SVG like so:

background-image: url(bg.svg);
background-repeat: repeat-x;
background-size: 1200px auto;

Even with exact on-the-pixel horizontal dimensions in the SVG I got a small gap. It worked when I didn't scale it:

background-size: auto auto;

Not much of a solution not to be able to scale your background, but still.

Pneumectomy answered 4/12, 2014 at 20:30 Comment(1)
Worked like charm! Thanks.Ineffective

© 2022 - 2024 — McMap. All rights reserved.