I want my tiles to be in the same row, and the container to scroll horizontally, if the tiles go beyond the width of the container. Looking at the following demo, the tiles get added to the next row, so I have to scroll vertically to access them. How can I make horizontal scroll work, and keep all tiles in the same row?
.container {
width: 600px;
max-height: 140px;
border: 1px solid green;
overflow-x: scroll;
overflow-y: scroll;
white-space: nowrap;
}
.tile {
width: 200px;
height: 92px;
float: left;
margin: 10px 10px 50px 10px;
background: cornflowerblue;
}
<div class="container">
<div><img class="tile"></div>
<div><img class="tile"></div>
<div><img class="tile"></div>
</div>
Your
.container
class has a fixed with of480px
. If this is intentional then all you need to do is adddisplay: inline-block
to your.floatLeft
class like so:Otherwise, you can make your
.container
class have a flexible width. If you like the suggestion, you can change the width tomin-width: 480px
that way your width will expand with your content.However, if your screen width is too small to hold many tiles, then they will align vertically in a new row, which is normal expected behavior. Or better yet, you could do both. The choice is yours.
You need to set
overflow-x:scroll;
andoverflow-y: hidden;
on parent, andwhite-space:nowrap;
on inner div and alsodisplay: inline-block;
onfloatLeft
HTML:
CSS:
Add display: inline-block to your containing divs css:
or alternatively you can add it as a style attribute on each div:
Heres the working fiddle: https://jsfiddle.net/edencorbin/rq0L7x7v/