据我所知,没有基于 CSS 的解决方法。但是,您可以使用 Jquery 来解决它。
我做了一个小测试让你看看:
http://sotkra.com/stackoverflow/cssdilemma/cssdilemma.html
第一个示例有 6 个左右的 li,超出了容器 div 的宽度,这意味着您不需要滚动条。
第二个例子有 8-9 里,它确实超过了容器 div 的宽度,这意味着你需要一个滚动条。
Basically, you use Jquery to count the number of li's inside your div using size(). If they exceed X number, in my example's case 6 (the limit before scroll is needed), then a class is added to the ul to extend its width (.longer) so that there's no line break and the horizontal scrollbar appears.
It also adds another class (.taller) that increases the height to accomodate the scrollbar itself.
Cheers
G.Campos