css - 无法拉伸(stretch) svg 背景图像,纵横比将被保留

标签 css svg

我使用 svg 作为背景图像,我希望它被拉伸(stretch)到 100% 100%。

但是,Chrome 和 firefox 似乎都在尽最大努力保留 svg 的纵横比,而不是将其缩小到另一个宽度。

正常大小

div
{
    background: url(image.svg) no-repeat;
    background-size: 100% 100%;
    width: 14rem;
    height: 4rem;
}

Normal size

双倍宽度

div
{
    background: url(image.svg) no-repeat;
    background-size: 100% 100%;
    width: 28rem;
    height: 4rem;
}

Double as wide

双倍高度

div
{
    background: url(image.svg) no-repeat;
    background-size: 100% 100%;
    width: 14rem;
    height: 8rem;
}

Double height

我怎样才能让 svg 拉伸(stretch)?

svg 内容:

<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 18.1.0, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
<svg version="1.1" id="Lager_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
     viewBox="0 0 371.7 102.8" enable-background="new 0 0 371.7 102.8" xml:space="preserve">
<polygon fill="#EF9104" points="370.4,100.5 0,100.5 0,0 269.4,0 "/>
</svg>

最佳答案

你应该添加

<svg preserveAspectRatio="none">

到您的 SVG。

MDN Reference Link

<none>

Do not force uniform scaling. Scale the graphic content of the given element non-uniformly if necessary such that the element's bounding box exactly matches the viewport rectangle.

关于css - 无法拉伸(stretch) svg 背景图像,纵横比将被保留,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32454685/

相关文章:

svg - 如何在 SVG 中绘制分段三次样条

javascript - 将整数和字符串连接成单个字符串

javascript - 如何在 d3.js 中的 svg 圆圈内填充图像

python - MathJax SVG 未验证

html - Zurb Ink 按钮不符合面板中的 2 列

html - 侧边栏包装器未使用页面内容包装器扩展

javascript - D3 v4 xAxis 缩放将 x 返回为 NaN 使得 rescale(x) 无法完成

CSS3 渐变只是多次重复细线

javascript - 有没有什么技术可以通过 CSS 规则将任意文本传递给 JavaScript?

html - 在两个部分之间放置一个箭头框