Make sure to include display: inline-block;
in the styling of your div that wraps the spans.
Update:
If you have the spans enclosed within a div like this:
<div id="spanwrapper">
<span>span1</span>
<span>span2</span>
</div>
Your CSS should include (in this specific order as noted by gilly3):
#spanwrapper {
display: inline-block; // Allows the div to adjust its size according to content
}
#spanwrapper {
*display: inline; // Provides support for IE7 or older versions
}
This will enable you to determine the width using jQuery:
$("#spanwrapper").width();
Or with traditional JavaScript:
document.getElementById('spanwrapper').clientWidth;
Keep in mind that since the width is now determined by the browser, specifying a fixed width in the style may no longer be necessary.