Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Horizontal Scrolling, fit to Content Width

I have a page with the following structure:

div.wrapper > div.content > div.item + div.item

The wrapper has a width of 320px, whereas the two div.item come out to around 600px. I need those two to be displayed inline (right now they are display: inline-block;, and have the wrapper's contents scroll horizontally. When I set the div.content width to auto, it takes the width of the wrapper (320px). Setting the width to 200% obviously gets the horizontal scrolling to work, but how do I get div.content to take on the width of its contents to allow for horizontal scrolling?

Note: The wrapper is set to a fixed width and height and has overflow-y: hidden and overflow-x: scroll set, because I do not want vertical scrolling-- only horizontal.

JSFiddle with an example: http://jsfiddle.net/kh5k7/

As you can see, the red divs will vertically stack. Changing the .content width to 200% (or some value) will cause horizontal scrolling to occur properly. I want this done automatically though, because I have no clue how many elements are going to be in the .content div before hand.

like image 410
Andrew M Avatar asked Jul 13 '12 19:07

Andrew M


People also ask

Is horizontal scrolling accessible?

Users may ignore content accessible through horizontal scrolling or “swiping” as they don't expect content there. Our research found that even strong cues such as arrows frequently remain unnoticed. People expect to scroll vertically for additional content, but they don't expect to scroll sideways.

How do I set horizontal scrolling?

Horizontal scrolling can be achieved by clicking and dragging a horizontal scroll bar, swiping sideways on a desktop trackpad or trackpad mouse, pressing left and right arrow keys, or swiping sideways with one's finger on a touchscreen.

Is horizontal scrolling good?

Introducing horizontal scrolling especially for pages with lots of text will also be bad for user experience. If you still think that horizontal scrolling will be beneficial for your users, then do go for it. Remember, your whole focus is your user.

Does width include scrollbar?

outerWidth() do not include the scrollbar width in their value. Especially when trying to match the window width to the CSS media queries.


1 Answers

Use white-space:nowrap; on .content

.content{
   width: auto;
   white-space:nowrap; 
}

http://jsfiddle.net/kh5k7/1/

like image 152
Musa Avatar answered Oct 06 '22 01:10

Musa