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.

4 Answers 4

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/

Sign up to request clarification or add additional context in comments.

3 Comments

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.
@s2cuts that may only work if you have constant ul width and constant ul number.
Nasty javascript fix, the answer described by ddlshack is a cleaner solution.
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/

Comments

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

2 Comments

The explanation was part of my query, thank you. Also can you explain what you mean by "Make sure you clear your floats"?
@s2cuts 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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.