In Javascript or jQuery, I want to calculate the width a specific SPAN element would be if its parent were infinitely large? (ie its “prefered” width)
What is the cleanest way to do this?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The best way I can think of is to actually clone and append the span to the document, in an infinitely large container, and get its width via jQuery.
HTML:
JS:
Now the fact that css does not allow you to have infinite width, and if you
spancontains inline text, you could use the css rulewhite-space: nowrap. This way there will be no line-breaks on your span and it will stretch horizontally towards infinity! It might be cleaner: