search

Home  >  Q&A  >  body text

javascript - How to get the exact width of an inline element that has no width set?

For example:

td has the width set, span has no width set.

1. Use $.width() to get the integer width, $('td').width() to get the 218px, span is 129px
2. Use clientWidth to get the width without margins and boder , span width is 0, td width is '218'
3. Use getComputedStyle to get final width , accurate to the decimal point , but the width of span obtained is auto, td is 217.5

question:

How to get the exact width of an element with no width set (accurate to the decimal point, displayed in px units)?

PHP中文网PHP中文网2774 days ago890

reply all(3)I'll reply

  • 天蓬老师

    天蓬老师2017-05-19 10:48:37

    Try using getClientBoundingRect()

    reply
    0
  • 迷茫

    迷茫2017-05-19 10:48:37

    getComputedStyle('原生dom')['width']

    reply
    0
  • 迷茫

    迷茫2017-05-19 10:48:37

    <p id="p" style="width:88.88px;"></p>
    parseFloat($("#p").attr("style").split('width:')[1])

    reply
    0
  • Cancelreply