gpt4 book ai didi

css - 如何在不遵守 Firefox 中的宽高比的情况下缩放 SVG 背景图像?

转载 作者:技术小花猫 更新时间:2023-10-29 10:25:06 28 4
gpt4 key购买 nike

我在一个具有可变高度和宽度的元素上设置了 CSS background-image。它是一个 SVG,预期的行为是在覆盖整个元素区域所需的任何方向上拉伸(stretch)。在 Chrome、Safari,甚至是糟糕的 Internet Explorer 9 中,缩放按我想要的方式工作:

the correctly stretched background image

但在 Firefox(桌面和移动)中,输出更像这样:

the incorrectly stretched background image preserves its aspect ratio and doesn't fill the space

经过深入研究,Firefox 似乎比其他浏览器对 SVG 渲染更为严格,因为它严格遵守最新版本的规范。 (Opera Mini 也无法渲染此背景图像(启用时),因为它不会缩小,并且考虑到 Presto 引擎以夏季雷雨的热忱处理规范的传统,它可能期待与 Firefox 相同的更新方法。)

您可以在 http://ti.gt/ 看到我正在处理的实时页面.我还在下面提供了我正在使用的代码:

HTML

<h1 class="blog-title-wrapper">
<a href="/" class="blog-title pop-out">{Title}</a>
</h1>

CSS

.blog-title {
background: none;
color: #fff;
font: normal normal 4em "Alternate Gothic", sans-serif;
margin: 0 0 0 .1em;
}

.blog-title-wrapper {
margin: -1px 0 0 0;
padding-bottom: 20%;
background: #d23e2c;
background: url("data:image/svg+xml;charset=utf-8;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHByZXNlcnZlQXNwZWN0UmF0aW89Im5vbmUiIHdpZHRoPSIxMDAlIiBoZWlnaHQ9IjEwMCUiIHZpZXdCb3g9IjAgMCAxMzMzIDc1MCIgdmVyc2lvbj0iMS4xIj48cGF0aCBkPSJtIDAsMCAxMzMzLDAgMCwzMjAuNTMgLTcyLjY3LDQ4LjQyIDUyLjQ1LDM3LjU3IC0xMjYuMzEsNTIuODQgNTAuMTMsMTM2LjE5IC0xOS45OCwzNy4yNiAtMzkuNDcsLTExLjcxIC0zOC43NSw0MC40NyAtNjMuMzIsLTEyLjA3IC0zMC40NCwtMTM1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sNDkuMjIgLTI1LjQ3LC0yMjcuMzQgeiIgZmlsbD0iI2E4MzEyMyIvPjxwYXRoIGQ9Im0gNjA1LjY1LDMyMi43NCAzLjYsMTA3LjE5IC0xMDAuNzYsMS41NyA2NC41OSwtMzMuMzggeiIgZmlsbD0iI2E4MzEyMyIvPjxwYXRoIGQ9Im0gNzE4LjUxLDMyNS4zIDMzLjMzLDc3LjAyIDc5LjQxLDI4LjI3IC0xMTkuOSwxOS40OSB6IiBmaWxsPSIjYTgzMTIzIi8+PC9zdmc+") no-repeat 100% 100%, none;
}

数据 URI 似乎不是症结所在,但这是解码后的 SVG 文件:

<svg xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="none" width="100%" height="100%" viewBox="0 0 1333 750" version="1.1">
<path d="m 0,0 1333,0 0,320.53 -72.67,48.42 52.45,37.57 -126.31,52.84 50.13,136.19 -19.98,37.26 -39.47,-11.71 -38.75,40.47 -63.32,-12.07 -30.44,-135.6 -7.94,3.28 24.74,140.95 -38.13,30.26 -44.35,-3.33 -35.83,23.39 -48.93,-13.22 -38.24,-141.48 -10.57,3.68 6.2,148.97 -33.88,20.46 -25.26,-13.85 -22.48,31.49 -27.36,-22.32 -32,24.34 L 680.55,718.6 664.7,564 l -17.08,0.91 -21.88,159.73 -29.17,25.36 -1.72,0 -27.84,-23.02 -23.3,13.88 -21.6,-20.71 -22.31,11.21 -24.13,-23.18 10.14,-155.4 -4.77,-2.2 -33.64,157.63 -29.97,14.21 -8.8,-1.17 -35.21,-20.38 -34.17,10.41 -8.63,-2.79 -32.93,-26.68 24.96,-151.54 -10.66,-2.09 -45.62,140.34 -32.49,15.33 -55.08,-40.36 -38.99,9.07 L 106.82,618.43 153.26,486.06 8.85,428.57 63.93,380.86 0,333.05 z" fill="#d23e2c"/>
<path d="M 302.76,34.58 464.98,158.93 423.07,367.37 277.27,284.22 181.79,332.66 81.57,143.08 z" fill="#a83123"/><path d="m 1078.92,40.11 196.9,77.84 -109.99,253.07 -103.89,-60.07 -15.37,1.9 -119.05,49.22 -25.47,-227.34 z" fill="#a83123"/>
<path d="m 605.65,322.74 3.6,107.19 -100.76,1.57 64.59,-33.38 z" fill="#a83123"/>
<path d="m 718.51,325.3 33.33,77.02 79.41,28.27 -119.9,19.49 z" fill="#a83123"/>
</svg>

我尝试了多种策略让 Firefox 接受我的要求,但到目前为止还没有成功。我看过this document that's about exactly this topic ,但我无法从中提取任何建议。

最佳答案

我需要设置background-size。这段代码成功了:

background-size: 100% 100%;

“更严格的渲染模型”的一部分是将尺寸信息延迟到页面,而不是 SVG 本身。

我以为我background 简写内定义它,但显然我是在指定background-position。我觉得这很愚蠢,但它确实是 CSS 中最粗糙和最杂乱的速记。

关于css - 如何在不遵守 Firefox 中的宽高比的情况下缩放 SVG 背景图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26049859/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com