svg - 为什么这个 SVG 图像的高度为 150px

标签 svg jsbin codepen

为什么这个 SVG 图像在这个 500 像素的容器内以 150 像素的高度显示?为什么是这个特定值?

我在 js bin 和 Codepen 中都发现了这种奇怪的行为,所以我认为这与我的代码有关,而不是与在线编辑器有关。

enter image description here

注意:一个 700px 的 div 容器会产生同样的结果。所以 parent 的高度并不重要。

<div style="padding: 30px; background-color: yellow; height: 500px; width: 500px; ">
<svg>

  <pattern id="basicPattern" x="10" y="10" width="40" height="40" patternUnits="userSpaceOnUse" >
      <rect x= "0" y="0" width="4" height="4"
            stroke = "red"
            stroke-width = "1"
            fill = "black"/>
  </pattern>
  <!-- <rect x="0" y="0" width="300" height="151" // why this deletes the bottom line? -->
  <!-- <rect x="0" y="0" width="300" height="150" // why this deletes 1 px from the bottom line?  -->

  <!-- but this height="149" is the bottom limmit for this picture.. 
       what prevent's it bor beeing extended further -  we have unthil 500 px as you can see on the div.-->

  <rect x="0" y="0" width="300" height="149"
  stroke= "red" 
  stroke-width="2"
  fill="url(#basicPattern)" />
</svg>

This is Jsbinthis is CodePen.

最佳答案

您没有设置 SVG widthheight ,因此默认大小为 300 px 宽 x 150 px 高(对于某些用户代理)。

这是您的 JSBin,SVG 宽度和高度都设置为 500px。现在矩形可以超过 150px 的高度:https://jsbin.com/yafenemawe/1/edit?html,output

关于svg - 为什么这个 SVG 图像的高度为 150px,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40156710/

相关文章:

html - Codepen 仪表板不显示 iframe 中的内容

javascript - Codepen modernizr.js - 404 错误

javascript - 为什么要使用 "listener rect"来设置 d3 中的缩放?

javascript - 如何在 Capybara 中对 SVG 元素执行点击事件

javascript - D3 可折叠树路径标签颠倒

javascript - 可安装的代码 Playground

templates - polymer 1.0 : Vertically stacked <paper-button> group with no white space (iron-flex-layout)

javascript - 使用 D3 在 map 上绘制点

javascript - JS Bin 警告 : Bad escapement of EOL. 如果需要使用选项 multistr

javascript - 更改鼠标悬停时的粒子效果