简体   繁体   中英

Scale svg image to fit parent as css background

I want to have an svg image to be scaled as follows:

草图

The right arrow (painted in red) should always scale to the height of the blue box, which can have dynamic height.

I have tried implementing it using an svg image:

   <?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
   <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
      "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
    <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
      viewBox="0 0 50 100" preserveAspectRatio="xMidYMid meet">
      <polygon points="0,0 50,50 0,100" style="fill:white" />
    </svg>

I tried different versions of preserveAspectRatio and viewBox.

My css implementation is like

#before-main section header.section-header .header-arrow {
    position:absolute;
    right:0;
    height:100%;
    width:4.1%;
    background:url("../img/headlineArrow_bg.svg") no-repeat right center;
    background-size:100% 100%;
}

HTML looks like

<header class="section-header">
    <div class="header-arrow">
    </div>
</header>

Is there a way to implement this using the svg image as css background?

Edit: Width of The arrow should always stay the same.

要使红色箭头拉伸以匹配高度,请尝试preserveAspectRatio="none"

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