tags:

views:

36

answers:

3

I want to have a horizontal scrollbar if necessary, by having the 'child' divs be added side-by-size instead of getting pushed to the next row when the parent isn't wide enough.

My code is below. If you set .mid to have width: 1000px you'll see what I'm going for, only I only want it to be as wide as the dynamically generated children.

<html>
<head>

    <style type="text/css">

        .parent {
            width: 400px;
            background-color: #666;
            overflow:scroll;  /* cater to the older browsers */
            overflow-y:hidden; /* Hide vertical*/
        }

        .mid {
            background-color: red;
        }

        .child {
            display:inline-block;
            background-color: #ccc;
            border: 1px solid black;
            width: 190px;
        }

    </style>

</head>
<body>
    <div class="parent">
        <div class="mid">
            <div class="child">
                I am a child.
            </div>
            <div class="child">
                I am a child.
            </div>
            <div class="child">
                I am a child.
            </div>  
        </div>
    </div>

</body>
</html>
A: 

People have been here before and found the best / easiest way to do this. Look here:

http://css-tricks.com/how-to-create-a-horizontally-scrolling-site/

Moin Zaman
A: 

You could try and use overflow:auto

Kieran
or have i misunderstood the problem
Kieran
@Kieran: Yes, you have. ;)
Yi Jiang
oh good job of clarifying what you ment then.
Kieran
+2  A: 

Heck, might as well answer this. You're going to need Javascript for this, but given that you already use that to dynamically generate the child elements, it shouldn't really be a problem.

Assuming you're using jQuery, add these two lines after you generate the element:

var child = $('child');
$('.mid').width(child.length * child.outerWidth(true));

Sorry about the miscommunication

Using this, however, will create another problem. When you use display: inline-block, a whitespace added behind each div, so jQuery cannot get the correct width. This wouldn't happen if you use float: left instead though.

Yi Jiang
I'm actually generating the children in a loop in Rails, not javascript. It sounds like you're saying float: left would solve the problem too, but that didn't work.
99miles
@99miles: No, I mean that the two *together* will solve your problem. I generally dislike using `display: inline-block` to get elements lined up beside each other.
Yi Jiang
no he is saying that add in some dynamic code to figrue out how many elements you have in the side scroller then set the width of mid..
Kieran
@Kieran. Kind of tired of saying this but *no*, what I'm saying, in the second part, is that when you use `display: inline-block`, you get an extra space behind each of the boxes, like this: http://jsfiddle.net/rEbWu/. So, when using jQuery for the calculation, you need to use `float: left`.
Yi Jiang