Using SVG as background image

后端 未结 5 850
忘掉有多难
忘掉有多难 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:

    Your SVG: i used fa-calendar-alt

    (any IDE may open svg image as shown below)

    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,******");
          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

    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);
        }
    

提交回复
热议问题