Example script for generating scrollWidth calculation manually versus the builtin one was a bug with a specific browser that it wasn t calculating it correctly to ensure consistency on all browsers this one a quick hack fix Further work type of box sizing etc are specific to this case also was designed for div and input elements single line not line wrapping input elements have a different result uses half the padding vs standard div style box sizing border box style script Custom scrollWidth map similiar behaviour to element scrollWidth const scrollWidth el let fontSize 12 let test document getElementById Test if test test document createElement span document body appendChild test let inputEl false if typeof el value undefined test innerHTML el value inputEl true else test innerHTML el innerHTML test id Test test style white space nowrap test style color red test style box sizing border box if inputEl test style padding parseInt window getComputedStyle el padding px else test style padding parseInt window getComputedStyle el padding 0 5 px test style border 0 window getComputedStyle el border test style margin 0 window getComputedStyle el margin test style width 0px test style height 0px test style width auto test style height auto let sw test offsetWidth let sw test offsetWidth return Math max el clientWidth sw 2 Generate random test cases and check the scrollWidth function works const randint lo hi return Math floor lo Math random hi lo for let i 0 i 10 i let el null if true Math random 0 5 el document createElement div el innerHTML abABC 0123 repeat randint 1 12 else el document createElement input el value abABC 0123 repeat randint 1 4 document body appendChild el el style display inline block span el style box sizing border box el style width randint 10 200 px el style fontSize inherit el style fontFamily inherit el style white space nowrap el style border randint 0 3 px solid green el style margin randint 0 40 px el style padding randint 0 40 px console log i scrollWidth orgi el scrollWidth mine scrollWidth el if el scrollWidth scrollWidth el console log failed testing break script
style margin 0 window getComputedStyle el margin test style width 0px test style height 0px test style width auto test style height auto let sw test offsetWidth let sw test offsetWidth return Math max el clientWidth sw 2 Generate random test cases and check the scrollWidth function works const randint lo hi return Math floor lo Math random hi lo for let i 0 i 10 i let el null if true Math random 0 5 el document createElement div el innerHTML abABC 0123 repeat randint 1 12 else el document createElement input el value abABC 0123 repeat randint 1 4 document body appendChild el el style display inline block span el style box sizing border box el style width randint 10 200 px el style fontSize inherit el style fontFamily inherit el style white space nowrap el style border randint 0 3 px solid green el style margin randint 0 40 px el style padding randint 0 40 px console log i scrollWidth orgi el scrollWidth mine scrollWidth el if el scrollWidth scrollWidth el console log failed testing break script