Why does 100% equal 150 pixels?

2019-06-11 04:13发布

The following code results in a table that 150 pixels tall. Why 150? Is this an SVG quirk or something more general?

https://jsfiddle.net/cc9tyw5v/31/

<table class="root" cellspacing="0">
  <tr>
    <td>
      <svg class="root-symbol" viewBox="0 0 5 6.5" preserveAspectRatio="none" style="height:100%;background-color:lightgreen;">
        <text x="0" y="6.5" font-size="9px">a</text>
      </svg>
    </td>
    <td><span style="font-size:2em;background-color:lightblue">I would expect it to be as tall as this span</span></td>
  </tr>
</table>

标签: html css svg
2条回答
贪生不怕死
2楼-- · 2019-06-11 04:25

According to this source

SVG isn't (just) an image. SVG is a document. The default size for HTML replaced elements will be used: 300px wide, 150px tall. This applies for <img>, <object> or <iframe>. The default 300x150 size also applies to inline <svg> elements within HTML documents, but that's a relatively recent consensus from the HTML5 specifications: other browsers will by default expand inline SVG to the full size of the viewport—equivalent to width: 100vw; height: 100vh; — which is the default size for SVG files that are opened directly in their own browser tab. Internet Explorer cuts the difference, using width of 100% and height of 150px for images and inline SVG.

查看更多
贼婆χ
3楼-- · 2019-06-11 04:47

The span you are attempting to fit the height of is not the parent of the SVG. It is a sibling, and therefore height:100% will not fit its height.

查看更多
登录 后发表回答