search

Home  >  Q&A  >  body text

How to make an SVG stretch vertically to a table cell?

I have a simple table that contains SVG in the table cells:

<html>
<body>
<table style="width: 8em;">
<tr>
<td style="border: 1px solid black;">
<svg viewBox="0 0 1 1" style="width: 1.37em;" preserveAspectRatio="none">
<polyline points="0,0 1,0.5 0,1" style="fill: blue;"></polyline>
</svg>
</td>
<td style="border: 1px solid black;">Lorem ipsum dolor sit amet</td>
</tr>
</table>
</body>
</html>

As you can see, other cells in the row may be too long, thus causing the row height to change relative to the default value, thereby introducing a gap between the SVG and its upper and lower borders.

How can I make an SVG automatically stretch or shrink to fill its cells vertically without changing its horizontal dimensions?

P粉521697419P粉521697419443 days ago488

reply all(1)I'll reply

  • P粉952365143

    P粉9523651432023-09-08 10:12:23

    One solution is to make it a background image. You can put the SVG code into a separate file or percent-encode it for use in a data URI.

    <table style="width: 8em;">
    <tr>
    <td style="border: 1px solid black; width: 1.37em; background-image: url('data:image/svg+xml,%3Csvg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 1 1%22 preserveAspectRatio=%22none%22%3E%3Cpolyline points=%220,0 1,0.5 0,1%22 style=%22fill: blue;%22%3E%3C/polyline%3E%3C/svg%3E');"></td>
    <td style="border: 1px solid black;">Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet</td>
    </tr>
    </table>

    reply
    0
  • Cancelreply