简体   繁体   中英

How to get background image/ image to stretch to fill container via CSS?

I have a scale vector image and I want it to fill a container. There's a couple of these sort of questions on SO, although they are a tad old. Eg Stretch and scale CSS background

However, when I've attempted to implement such solutions the results have been disappointing.

For instance:

    body  {

    margin: 0; 
    padding: 0;
    text-align: center; 
    color: #000;
    background-color: #000
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 100%;

}

...

.twoColLiqLtHdr #container {
    width: 80%;
    margin: 0 auto; 
    border: 2px solid #000000;
    text-align: left; 
    background-color: #003;
    background-image: url('background_image.svg'); 
    /*background-position: 80% 80%; */

  background-repeat: no-repeat; 
    color: #000;
    font-family: "Times", cursive;


    background-attachment:fixed;
    min-height:100%;
    /*top: 0;
    left: 0; */



} 

I've tried various arrangements but I cannot make it stretch without leaving blank areas (repeating is a success of sorts, but looks horrible).

However, since I'm working with a svg am I approaching this in perhaps the wrong way, should I be using a svg xmlns tag in the html instead? (PS I'm aware of IE8's absolute inability to handle svgs)

Have you tried

background-size:cover;

Check some examples .

I had issues with webkit, background-size:cover and SVG background images. I solved it adding

-webkit-background-origin:border;

you probably need to add preserveAspectRatio="none" as an attribute of the outer <svg> element in the SVG image itself.

If you don't want to modify the image file then you could try

background-image: url('background_image.svg#svgView(preserveAspectRatio(none))');

Firefox 15 would support that and probably other UAs too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM