【问题标题】:div img(svg) stretched to viewport height and widthdiv img(svg) 拉伸到视口高度和宽度
【发布时间】:2015-03-23 13:26:07
【问题描述】:

我正在尝试将图像 (.svg) 的宽度填充到我的 DIV 中,使其可以拉伸,但我的图像只有 100% 的宽度,并且随着高度它裁剪了底部。

我怎样才能让它拉伸高度到视口高度? 我已经尝试将图像变成背景图像,但这在设计中不起作用。

.flag {
	position: absolute;
	z-index: -1;
	background-size: contain;
}
#flag img{
	width: 100vh;
	height: 100vh;
}
<section class="cover" style="min-height: 100vh">
	<img src="img/background.svg" class="flag"></div> 
</section>

<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="1024px"
	 height="768px" viewBox="0 0 1024 768" enable-background="new 0 0 1024 768" xml:space="preserve">
<g id="Layer_1" display="none">
	<rect x="-0.5" y="0.5" display="inline" fill="#FFFFFF" stroke="#000000" stroke-miterlimit="10" width="57" height="768"/>
	<rect x="967.5" y="0.5" display="inline" fill="#FFFFFF" stroke="#000000" stroke-miterlimit="10" width="57" height="768"/>
	<rect x="-0.5" y="0.5" display="inline" fill="none" stroke="#000000" stroke-miterlimit="10" width="1025" height="57"/>
	<rect x="-0.5" y="711.5" display="inline" fill="none" stroke="#000000" stroke-miterlimit="10" width="1025" height="57"/>
</g>
<g id="Layer_2">
	<rect fill="#FFFFFF" width="1024" height="768"/>
	<rect y="103" width="1022" height="34"/>
	<rect y="171" width="1022" height="34"/>
	<rect y="239" width="1022" height="34"/>
	<rect y="631" width="1022" height="34"/>
	<rect y="563" width="1022" height="34"/>
	<rect y="495" width="1022" height="34"/>
	<rect x="55" y="58" fill="#E5452D" width="912" height="654"/>
	<rect x="55" y="385" fill="#0698CF" width="912" height="327"/>
</g>
</svg>

【问题讨论】:

  • 请同时发布svg 代码。

标签: html css svg viewport stretch


【解决方案1】:

在支持它的浏览器中,您可以使用an SVG fragment identifier 关闭图像的 viewBox。 Firefox 不支持 viewBox(来自 SVG 1.2 tiny 规范)摆脱 viewBox 将允许在不强制纵横比的情况下显示图像。

另一种选择是将 preserveAspectRatio 设置为 none(片段标识符也可以这样做),即 #svgView(preserveAspectRatio(none)) 取决于您想要的。

如果需要,您可以同时进行。我在下面显示了 preserveAspectRatio。

<section class="cover" style="min-height: 100vh">
    <img src="http://www.klh-dev.com/lehava/train/test.svg#svgView(preserveAspectRatio(none))" class="flag">
</section>

【讨论】:

  • 我很困惑,OP想根据视口拉伸背景。顺便说一句,片段标识符 +1。
【解决方案2】:

要根据视口拉伸图像,您必须:

  1. svgwidthheight 更改为100% 并添加preserveAspectRatio="none"
  2. 由于您使用的是img 标记,并且这仅在widthheight 是固定值时才有效,因此您必须使用JavaScript 来设置imgwidthheight使用 JavaScript。

这是一个例子。

注意:在您的情况下,您必须设置imgheight

Fiddle

var d = document.getElementsByTagName('div')[0];

function resize() {
  d.style.height = window.innerHeight + 'px';
}
resize();
window.onresize = resize;
body {
  margin: 0;
}
div {
  position: relative;
  overflow: hidden;
}
<div>
  <svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 1024 768" enable-background="new 0 0 1024 768" xml:space="preserve" preserveAspectRatio="none">
    <g id="Layer_1" display="none">
      <rect x="-0.5" y="0.5" display="inline" fill="#FFFFFF" stroke="#000000" stroke-miterlimit="10" width="57" height="768" />
      <rect x="967.5" y="0.5" display="inline" fill="#FFFFFF" stroke="#000000" stroke-miterlimit="10" width="57" height="768" />
      <rect x="-0.5" y="0.5" display="inline" fill="none" stroke="#000000" stroke-miterlimit="10" width="1025" height="57" />
      <rect x="-0.5" y="711.5" display="inline" fill="none" stroke="#000000" stroke-miterlimit="10" width="1025" height="57" />
    </g>
    <g id="Layer_2">
      <rect fill="#FFFFFF" width="1024" height="768" />
      <rect y="103" width="1022" height="34" />
      <rect y="171" width="1022" height="34" />
      <rect y="239" width="1022" height="34" />
      <rect y="631" width="1022" height="34" />
      <rect y="563" width="1022" height="34" />
      <rect y="495" width="1022" height="34" />
      <rect x="55" y="58" fill="#E5452D" width="912" height="654" />
      <rect x="55" y="385" fill="#0698CF" width="912" height="327" />
    </g>
  </svg>
</div>

【讨论】:

  • 我编辑了 .svg 文件,我需要将第一个文件添加到我的 .js 中,将第二个文件添加到我的 .css 中?我对js不是很了解。
  • 如果问的不是太多,你能在 jsfiddle 中给我看吗?我完全迷失了这里的代码 sn-p
  • 我只使用位置,没有浮动。编辑:是的,只有一个带有标志的元素
  • @Marcel - 给你 ---> Fiddle。我在对 base64 的回答中对svg 代码进行了编码,并将其用作imgsrc
  • 成功了!非常感谢!!
【解决方案3】:

将其用作您的 css 文件:

.flag {
    position: absolute;
    z-index: -1;
    background-size: contain;
}

img{
    width: 50%;
    height: 50%;
}

jsFiddle:http://jsfiddle.net/cwnkswpo/

我会尽快改进我的答案

【讨论】:

  • 你的图片后面有一个 标签。那是什么?
  • 愚蠢的错误。删除它,拉伸错误保持不变。
  • 我实际上不知道..它在我的电脑上工作。确保您上传并保存了正确的文件
  • 如果不对svg 文件本身进行一些更改,这将无法正常工作。
  • 尝试用 chrome 和 safari 打开它,但没有显示差异。虽然在 Mac 上工作...您建议对 svg 进行哪些编辑以使其正常工作?
猜你喜欢
  • 1970-01-01
  • 2014-10-14
  • 2018-04-09
  • 2014-01-22
  • 1970-01-01
  • 2015-09-03
  • 1970-01-01
  • 2023-03-10
  • 2013-05-06
相关资源
最近更新 更多