HTML, overflow:scroll, and float

只谈情不闲聊 提交于 2021-02-07 11:59:39

问题


I have a div that encapsulates many unordered lists (ul). I have each ul set to "float:left". And I also have the parent div that contains them set to "overflow-x:scroll". What's happening is the ul's are wrapping when they hit the edge of the page and not staying side by side to take advantage of the scrolling property of the parent div (the scroll bars are there). Why? How can I fix this?

Thanks for any help.


回答1:


you need to insert those uls in another div, to which you'll give width=[width of ul]*[number of uls]
http://jsfiddle.net/seler/gAGKh/ or count total width of uls http://jsfiddle.net/seler/gAGKh/1/




回答2:


You can set your list items to display: inline-block, then use white-space: nowrap. Works in most modern browsers.

http://jsfiddle.net/gAGKh/22/




回答3:


Because you floated the ULs, they don't exist in the document flow anymore so they won't expand the parent div (hence the wrapping.)

Try setting an explicit width on the parent div that allows for all of them to exist side by side.

ALSO, if you aren't clearing the ULs in the parent div then you'll more than likely run into issues there too, vertical ones. Make sure you clear your floats :)




回答4:


You need to:

  1. Make the <li> also float.
  2. Set fixed width to each <ul>.
  3. Set fixed width to the containing <div>, enough to hold all the lists.

For example:

ul { width: 250px; }
li { margin-left: 5px; }
ul, li { float: left;  }
div { overflow-x: scroll; width: 750px; }

Test case.



来源:https://stackoverflow.com/questions/5926076/html-overflowscroll-and-float

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!