将背景图像 (SVG) 拉伸到 100% 宽度和 100% 高度?
Stretch a background image (SVG) to 100% width and 100% height?
我想要实现的行为是 background-size:cover;
的宽度,但通过拉伸图像达到 background-size:contain;
的高度。我认为 background-size:100%;
应该这样做,但看看这个例子 - 它没有。
.container {
background-image:url("https://upload.wikimedia.org/wikipedia/commons/3/34/Red_star.svg");
background-position:center center;
background-repeat:no-repeat;
width:300px;
height:180px;
background-color:#eef;
border-bottom:1px solid #000;
}
#container1 {
background-size:contain;
}
#container2 {
background-size:cover;
}
#container3 {
background-size:100%;
}
<div id="container1" class="container"></div>
<div id="container2" class="container"></div>
<div id="container3" class="container"></div>
我怎样才能达到预期的拉伸结果?
这应该有效:
background-size: 100% 100%;
.container {
background-image:url("https://upload.wikimedia.org/wikipedia/commons/3/34/Red_star.svg");
background-position:center center;
background-repeat:no-repeat;
width:300px;
height:180px;
background-color:#eef;
border-bottom:1px solid #000;
}
#container1 {
background-size: 100% 100%;
}
<div id="container1" class="container"></div>
使用background-size: 100%;
实际上意味着background-size: 100% auto;
。同时使用宽度和高度值:background-size: 100% 100%;
Using a width value only, in which case the height defaults to auto.
https://developer.mozilla.org/en-US/docs/Web/CSS/background-size#Syntax
.container {
background-image:url("https://upload.wikimedia.org/wikipedia/commons/3/34/Red_star.svg");
background-position:center center;
background-repeat:no-repeat;
width:300px;
height:180px;
background-color:#eef;
border-bottom:1px solid #000;
}
#container1 {
background-size:contain;
}
#container2 {
background-size:cover;
}
#container3 {
background-size: 100% 100%;
}
<div id="container1" class="container"></div>
<div id="container2" class="container"></div>
<div id="container3" class="container"></div>
所选答案正确,但有些不完整:原因如下...
如果您希望背景 SVG 图像拉伸并填满整个容器,请务必注意 SVG 必须允许这种情况发生。在 SVG 中,确保在视口旁边添加了“preserveAspectRatio="none"”。
所以使用:
background-size: 100% 100%;
并在 SVG 中确保它看起来像这样:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 800 100" preserveAspectRatio="none">
我想要实现的行为是 background-size:cover;
的宽度,但通过拉伸图像达到 background-size:contain;
的高度。我认为 background-size:100%;
应该这样做,但看看这个例子 - 它没有。
.container {
background-image:url("https://upload.wikimedia.org/wikipedia/commons/3/34/Red_star.svg");
background-position:center center;
background-repeat:no-repeat;
width:300px;
height:180px;
background-color:#eef;
border-bottom:1px solid #000;
}
#container1 {
background-size:contain;
}
#container2 {
background-size:cover;
}
#container3 {
background-size:100%;
}
<div id="container1" class="container"></div>
<div id="container2" class="container"></div>
<div id="container3" class="container"></div>
我怎样才能达到预期的拉伸结果?
这应该有效:
background-size: 100% 100%;
.container {
background-image:url("https://upload.wikimedia.org/wikipedia/commons/3/34/Red_star.svg");
background-position:center center;
background-repeat:no-repeat;
width:300px;
height:180px;
background-color:#eef;
border-bottom:1px solid #000;
}
#container1 {
background-size: 100% 100%;
}
<div id="container1" class="container"></div>
使用background-size: 100%;
实际上意味着background-size: 100% auto;
。同时使用宽度和高度值:background-size: 100% 100%;
Using a width value only, in which case the height defaults to auto.
https://developer.mozilla.org/en-US/docs/Web/CSS/background-size#Syntax
.container {
background-image:url("https://upload.wikimedia.org/wikipedia/commons/3/34/Red_star.svg");
background-position:center center;
background-repeat:no-repeat;
width:300px;
height:180px;
background-color:#eef;
border-bottom:1px solid #000;
}
#container1 {
background-size:contain;
}
#container2 {
background-size:cover;
}
#container3 {
background-size: 100% 100%;
}
<div id="container1" class="container"></div>
<div id="container2" class="container"></div>
<div id="container3" class="container"></div>
所选答案正确,但有些不完整:原因如下...
如果您希望背景 SVG 图像拉伸并填满整个容器,请务必注意 SVG 必须允许这种情况发生。在 SVG 中,确保在视口旁边添加了“preserveAspectRatio="none"”。
所以使用:
background-size: 100% 100%;
并在 SVG 中确保它看起来像这样:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 800 100" preserveAspectRatio="none">