简体   繁体   English

为什么我需要背景尺寸200%而不是封面

[英]why do I need background-size 200% instead of cover

I'm trying to use an inline svg as a background-image (data url), but background-size:cover does not seems to work well, however with 200% its fits perfectly. 我正在尝试使用内嵌svg作为背景图像(数据url),但是background-size:cover似乎不能很好地工作,但是200%适合。

I'd like to understand the issue so that I can make sure I have a consistent render with different browsers. 我想了解这个问题,以便可以确保我在不同浏览器上的渲染效果一致。 Ideally I'd like to keep using background-size:cover if possible 理想情况下,我希望继续使用background-size:cover

 .card { display: inline-flex; /* required in my context */ width: 45vmax; background-color: lightblue; background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="none" viewBox="0 0 2 2"><g fill="red"><path d="M0,1v-1h1z" opacity=".4" /><path d="M1,0v1h-1z" opacity=".2"/></g></svg>'); background-size: cover; } ._200percent { background-size: 200%; } .video { padding-top: 56.25%; } body { margin: 0; } 
 <a class="card"> <div class="video"></div> </a> <a class="card _200percent"> <div class="video"></div> </a> 

As commented you can adjust the viewbox to cover the path and avoid having extra spaces and the use of 200% 如前所述,您可以调整视图框以覆盖路径,并避免有多余的空间和200%的使用率

 svg { border:1px solid; width:200px; } 
 <svg xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="none" viewBox="0 0 2 2"><g fill="red"><path d="M0,1v-1h1z" opacity=".4" /><path d="M1,0v1h-1z" opacity=".2"/></g></svg> <svg xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="none" viewBox="0 0 1 1"><g fill="red"><path d="M0,1v-1h1z" opacity=".4" /><path d="M1,0v1h-1z" opacity=".2"/></g></svg> 

You can also do this using CSS and gradient without the need of SVG 您也可以使用CSS和渐变进行此操作,而无需SVG

 .card { display: inline-flex; /* required in my context */ width: 45vmax; background: linear-gradient(to bottom right,rgba(255,0,0,0.4) 49.5%,rgba(255,0,0,0.2) 50%), lightblue; } .video { padding-top: 56.25%; } body { margin: 0; } 
 <a class="card"> <div class="video"></div> </a> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM