HTML, overflow:scroll, and float
Asked Answered
S

4

11

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.

Shiest answered 8/5, 2011 at 6:55 Comment(0)
M
6

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/

Mellisa answered 8/5, 2011 at 7:57 Comment(3)
Creating an inner div helps alot. Instead of using JS to calculate the appropriate width, I found that setting the static width of the new inner div to a sufficiently large width to account for the most extreme case does no harm to the layout. Thanks seler.Shiest
@Shiest that may only work if you have constant ul width and constant ul number.Mellisa
Nasty javascript fix, the answer described by ddlshack is a cleaner solution.Agraphia
G
6

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/

Gustie answered 21/4, 2012 at 17:2 Comment(0)
A
5

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 :)

Adrian answered 8/5, 2011 at 7:9 Comment(2)
The explanation was part of my query, thank you. Also can you explain what you mean by "Make sure you clear your floats"?Shiest
@Shiest Make sure you use something like clearfix to expand your parent containers to the height of the content. Otherwise, you'll run into issues with vertical height similar to the issues you were having with horizontal width. Just a heads up.Adrian
S
0

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.

Selfesteem answered 8/5, 2011 at 7:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.