search

Home  >  Q&A  >  body text

javascript - How to get the height of svg element in Firefox.

The clientHeight and offsetHeight of the svg element are 0 and undefined in Firefox, but are normal in Chrome. How to get the height of svg?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<svg  id="svg" viewBox="0,0,50,50"  version="1.1" xmlns="http://www.w3.org/2000/svg">
    <rect x="0.5" y="0.5" rx="3" ry="3" width="49" height="49" fill="transparent" stroke="black" stroke-width="1"/>
    <text x="25" y="30" text-anchor="middle" font-size="14px" font-family="Yahei" fill="red">Abc</text>
</svg>
<script>
    let a=document.getElementById('svg').clientHeight;
    console.log(a);
</script>
</body>
</html>
淡淡烟草味淡淡烟草味2745 days ago499

reply all(2)I'll reply

  • 大家讲道理

    大家讲道理2017-05-19 10:32:24

    Can be solved with getBoundingClientRect().height.

    reply
    0
  • 巴扎黑

    巴扎黑2017-05-19 10:32:24

    I haven’t encountered it specifically, but I can give you an idea. The prompts of Firefox are still user-friendly. Go directly to the console to see how the svg height is defined. If Firefox displays 0 and undefined, please post an error on the console. If I can give you some ideas, please adopt them. Thank you

    reply
    0
  • Cancelreply