SVG 背景图像缩放问题
SVG background image scaling issue
我在 before 伪元素上使用 SVG(白色三角形)作为背景图像,如图所示。如果我将高度设置为与 SVG 大小相同,则它没有半像素线。但是因为我需要它来响应,所以我将背景大小设置为 "cover"(如果我使用 "contain" 则结果相同)。
这是我的 CSS 实现带有白色三角形的红色条纹的代码:
#careers{margin:7em 0 0}
#careers a{width:100%;height:auto;overflow:hidden;display:block;-webkit-box-shadow:7px 10px 11px 0 rgba(0,0,0,.16);-moz-box-shadow:7px 10px 11px 0 rgba(0,0,0,.16);box-shadow:7px 10px 11px 0 rgba(0,0,0,.16)}
#careers a img{width:100%;height:auto;-webkit-transition:all .2s linear;-moz-transition:all .2s linear;-ms-transition:all .2s linear;-o-transition:all .2s linear;transition:all .2s linear;display:block;z-index:10}
#careers a:hover img{-webkit-transform:scale(1.05);-moz-transform:scale(1.05);-o-transform:scale(1.05);transform:scale(1.05)}
#careers .col-md-5{z-index:15}
#careers .careers-holder{padding:0 15% 0 15px;color:#fff;z-index:10}
#careers .careers-holder .btn{width:auto;-webkit-box-shadow:none;-moz-box-shadow:none;box-shadow:none;display:inline-block}
#careers::before{content:'';position:absolute;width:calc(100% - 4.1666665%);height:385px;left:0;right:0;top:50%;-webkit-transform:translateY(-50%);-moz-transform:translateY(-50%);-o-transform:translateY(-50%);transform:translateY(-50%);background:#d02139 url(http://ranfurlie.website.2018.360southclients.net:8080/img/icon-triangle-white.svg) top right no-repeat;background-size:cover;z-index:1}
<section id="careers">
<div class="container-fluid">
<div class="row vertical-align">
<div class="col-md-1"></div>
<div class="col-md-5">
<a href="<?php echo $careers_link; ?>"><img src="<?php echo $careers_image[0]; ?>" width="705" height="450" alt="Careers" /></a>
</div>
<div class="col-md-5 careers-holder">
<h3>Careers</h3>
<h2>Interested in working for us?</h2>
<p>We are rapidly growing and always need talent to continue our pace. If you’re up for a great challenges and the rewarding career, check out our current positions available.</p>
<a href="<?php echo $careers_link; ?>" class="btn white">Read more</a>
</div>
<div class="col-md-1"></div>
</div>
</div>
</section>
这是该部分的 HTML:
知道为什么会有半个红色像素吗? SVG 背景图片设置为右上角。
可在此处找到 SVG 本身:
http://ranfurlie.website.2018.360southclients.net:8080/img/icon-triangle-white.svg
编辑:我在 SVG 代码中添加了设置宽度和高度,这似乎解决了 Firefox 的像素问题,但它仍在 Chrome 上显示。我没有 mac 来测试其他浏览器。
不要在 SVG 元素中添加 width
和 height
,或者在 CSS 中添加 background-size:cover
。 SVG 定位和调整大小的方式通过属性 preserveAspectRatio
:
<svg xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 347 385" preserveAspectRatio="xMaxYMax slice">
它将确保 viewBox 覆盖整个元素背景并定位,以便右上角重合。
(不评估在 svg 元素上设置的其余属性。)
我在 before 伪元素上使用 SVG(白色三角形)作为背景图像,如图所示。如果我将高度设置为与 SVG 大小相同,则它没有半像素线。但是因为我需要它来响应,所以我将背景大小设置为 "cover"(如果我使用 "contain" 则结果相同)。
这是我的 CSS 实现带有白色三角形的红色条纹的代码:
#careers{margin:7em 0 0}
#careers a{width:100%;height:auto;overflow:hidden;display:block;-webkit-box-shadow:7px 10px 11px 0 rgba(0,0,0,.16);-moz-box-shadow:7px 10px 11px 0 rgba(0,0,0,.16);box-shadow:7px 10px 11px 0 rgba(0,0,0,.16)}
#careers a img{width:100%;height:auto;-webkit-transition:all .2s linear;-moz-transition:all .2s linear;-ms-transition:all .2s linear;-o-transition:all .2s linear;transition:all .2s linear;display:block;z-index:10}
#careers a:hover img{-webkit-transform:scale(1.05);-moz-transform:scale(1.05);-o-transform:scale(1.05);transform:scale(1.05)}
#careers .col-md-5{z-index:15}
#careers .careers-holder{padding:0 15% 0 15px;color:#fff;z-index:10}
#careers .careers-holder .btn{width:auto;-webkit-box-shadow:none;-moz-box-shadow:none;box-shadow:none;display:inline-block}
#careers::before{content:'';position:absolute;width:calc(100% - 4.1666665%);height:385px;left:0;right:0;top:50%;-webkit-transform:translateY(-50%);-moz-transform:translateY(-50%);-o-transform:translateY(-50%);transform:translateY(-50%);background:#d02139 url(http://ranfurlie.website.2018.360southclients.net:8080/img/icon-triangle-white.svg) top right no-repeat;background-size:cover;z-index:1}
<section id="careers">
<div class="container-fluid">
<div class="row vertical-align">
<div class="col-md-1"></div>
<div class="col-md-5">
<a href="<?php echo $careers_link; ?>"><img src="<?php echo $careers_image[0]; ?>" width="705" height="450" alt="Careers" /></a>
</div>
<div class="col-md-5 careers-holder">
<h3>Careers</h3>
<h2>Interested in working for us?</h2>
<p>We are rapidly growing and always need talent to continue our pace. If you’re up for a great challenges and the rewarding career, check out our current positions available.</p>
<a href="<?php echo $careers_link; ?>" class="btn white">Read more</a>
</div>
<div class="col-md-1"></div>
</div>
</div>
</section>
这是该部分的 HTML:
知道为什么会有半个红色像素吗? SVG 背景图片设置为右上角。
可在此处找到 SVG 本身:
http://ranfurlie.website.2018.360southclients.net:8080/img/icon-triangle-white.svg
编辑:我在 SVG 代码中添加了设置宽度和高度,这似乎解决了 Firefox 的像素问题,但它仍在 Chrome 上显示。我没有 mac 来测试其他浏览器。
不要在 SVG 元素中添加 width
和 height
,或者在 CSS 中添加 background-size:cover
。 SVG 定位和调整大小的方式通过属性 preserveAspectRatio
:
<svg xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 347 385" preserveAspectRatio="xMaxYMax slice">
它将确保 viewBox 覆盖整个元素背景并定位,以便右上角重合。
(不评估在 svg 元素上设置的其余属性。)