Using SVG as background image

后端 未结 5 837
忘掉有多难
忘掉有多难 2020-12-12 20:55

I can\'t seem to get this to work as desired. My page changes height based on what content is loaded and if it requires a scroll, the svg doesn\'t seem to be stretching...

相关标签:
5条回答
  • 2020-12-12 21:12

    With my solution you're able to get something similar:

    Here is bulletproff solution:

    Your html: <input class='calendarIcon'/>

    Your SVG: i used fa-calendar-alt

    (any IDE may open svg image as shown below)

    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><path d="M148 288h-40c-6.6 0-12-5.4-12-12v-40c0-6.6 5.4-12 12-12h40c6.6 0 12 5.4 12 12v40c0 6.6-5.4 12-12 12zm108-12v-40c0-6.6-5.4-12-12-12h-40c-6.6 0-12 5.4-12 12v40c0 6.6 5.4 12 12 12h40c6.6 0 12-5.4 12-12zm96 0v-40c0-6.6-5.4-12-12-12h-40c-6.6 0-12 5.4-12 12v40c0 6.6 5.4 12 12 12h40c6.6 0 12-5.4 12-12zm-96 96v-40c0-6.6-5.4-12-12-12h-40c-6.6 0-12 5.4-12 12v40c0 6.6 5.4 12 12 12h40c6.6 0 12-5.4 12-12zm-96 0v-40c0-6.6-5.4-12-12-12h-40c-6.6 0-12 5.4-12 12v40c0 6.6 5.4 12 12 12h40c6.6 0 12-5.4 12-12zm192 0v-40c0-6.6-5.4-12-12-12h-40c-6.6 0-12 5.4-12 12v40c0 6.6 5.4 12 12 12h40c6.6 0 12-5.4 12-12zm96-260v352c0 26.5-21.5 48-48 48H48c-26.5 0-48-21.5-48-48V112c0-26.5 21.5-48 48-48h48V12c0-6.6 5.4-12 12-12h40c6.6 0 12 5.4 12 12v52h128V12c0-6.6 5.4-12 12-12h40c6.6 0 12 5.4 12 12v52h48c26.5 0 48 21.5 48 48zm-48 346V160H48v298c0 3.3 2.7 6 6 6h340c3.3 0 6-2.7 6-6z"/></svg>

    To use it at css background-image you gotta encode the svg to address valid string. I used this tool

    As far as you got all stuff you need, you're coming to css

    .calendarIcon{
          //your url will be something like this:
          background-image: url("data:image/svg+xml,***<here place encoded svg>***");
          background-repeat: no-repeat;
        }
    

    Note: these styling wont have any effect on encoded svg image

    .{
          fill: #f00; //neither this
          background-color: #f00; //nor this
    }
    

    because all changes over the image must be applied directly to its svg code

    <svg xmlns="" path="" fill="#f00"/></svg>

    To achive the location righthand i copied some Bootstrap spacing and my final css get the next look:

    .calendarIcon{
          background-image: url("data:image/svg+xml,%3Csvg...svg%3E");
          background-repeat: no-repeat;
          padding-right: calc(1.5em + 0.75rem);
          background-position: center right calc(0.375em + 0.1875rem);
          background-size: calc(0.75em + 0.375rem) calc(0.75em + 0.375rem);
        }
    
    0 讨论(0)
  • 2020-12-12 21:13

    You can try removing the width and height attributes on the svg root element, adding preserveAspectRatio="none" viewBox="0 0 1024 800" instead. It makes a difference in Opera at least, assuming you wanted the svg to stretch to fill the entire region defined by the CSS styles.

    0 讨论(0)
  • 2020-12-12 21:13

    Set Background svg with content/cart at center

    .login-container {
      justify-content: center;
      align-items: center;
      display: flex;
      height: 100vh; 
      background-image: url(/assets/images/login-bg.svg);
      background-position: center;
      background-repeat: no-repeat;
      background-size: cover;
    }
    
    0 讨论(0)
  • 2020-12-12 21:28

    You have set a fixed width and height in your svg tag. This is probably the root of your problem. Try not removing those and set the width and height (if needed) using CSS instead.

    0 讨论(0)
  • 2020-12-12 21:36

    Try placing it on your body

    body {
        height: 100%;
        background-image: url(../img/bg.svg);
        background-size:100% 100%;
        -o-background-size: 100% 100%;
        -webkit-background-size: 100% 100%;
        background-size:cover;
    }
    
    0 讨论(0)
提交回复
热议问题