62

I'm trying to create a <div> with a series of photos which are horizontally scrollable only.

It should look something like this LINK;

However the above is only achieved by specifying a width for the <div> which contains the photos (so they don't 'word-wrap'). If I don't put a width - it looks like this;LINK

What can I do using CSS to prevent putting in a fixed width as the images may vary.

Thanks

Tieson T.
  • 20,774
  • 6
  • 77
  • 92
Simon R
  • 3,732
  • 4
  • 31
  • 39

4 Answers4

131

You can use display:inline-block with white-space:nowrap. Write like this:

.scrolls {
    overflow-x: scroll;
    overflow-y: hidden;
    height: 80px;
    white-space:nowrap;
}
.imageDiv img {
    box-shadow: 1px 1px 10px #999;
    margin: 2px;
    max-height: 50px;
    cursor: pointer;
    display:inline-block;
    *display:inline;/* For IE7*/
    *zoom:1;/* For IE7*/
    vertical-align:top;
 }

Check this http://jsfiddle.net/YbrX3/

Black
  • 18,150
  • 39
  • 158
  • 271
sandeep
  • 91,313
  • 23
  • 137
  • 155
  • @sandeep plz help me out i used ur css which is in jsfiddle and suppose some data r cuming dynamically by ajax callin which is storing in button which is in footer now how i can scroll that button not window page only footer – Atul Dhanuka Jul 18 '13 at 07:32
  • How to also support scrolling by grabbing an image and dragging left/right? – Edwin Evans Nov 10 '15 at 17:03
  • 1
    white-space:nowrap was the answer for me. – Mystus Jun 02 '17 at 11:17
14

Here's a solution with for images with variable width and height:

.container {
  display: flex;
  flex-wrap: no-wrap;
  overflow-x: auto;
  margin: 20px;
}
img {
  flex: 0 0 auto;
  width: auto;
  height: 100px;
  max-width: 100%;
  margin-right: 10px;
}
<div class="container">
 <img src="https://via.placeholder.com/100x100" />
 <img src="https://via.placeholder.com/50x50" />
 <img src="https://via.placeholder.com/5x50" />
 <img src="https://via.placeholder.com/100x50" />
 <img src="https://via.placeholder.com/50x100" />
 <img src="https://via.placeholder.com/20x50" />
 <img src="https://via.placeholder.com/50x30" />
 <img src="https://via.placeholder.com/50x150" />
 <img src="https://via.placeholder.com/250x50" />
 <img src="https://via.placeholder.com/150x350" />
 <img src="https://via.placeholder.com/50x350" />
 <img src="https://via.placeholder.com/100x100" />
 <img src="https://via.placeholder.com/50x50" />
 <img src="https://via.placeholder.com/5x50" />
 <img src="https://via.placeholder.com/100x50" />
 <img src="https://via.placeholder.com/50x100" />
 <img src="https://via.placeholder.com/20x50" />
 <img src="https://via.placeholder.com/50x30" />
 <img src="https://via.placeholder.com/50x150" />
 <img src="https://via.placeholder.com/250x50" />
 <img src="https://via.placeholder.com/150x350" />
 <img src="https://via.placeholder.com/50x350" />
</div>
Barnee
  • 3,212
  • 8
  • 41
  • 53
0

Use this code to generate horizontal scrolling blocks contents. I got this from here http://www.htmlexplorer.com/2014/02/horizontal-scrolling-webpage-content.html

<html>
<title>HTMLExplorer Demo: Horizontal Scrolling Content</title>
<head>
<style type="text/css">
#outer_wrapper {  
    overflow: scroll;  
    width:100%;
}
#outer_wrapper #inner_wrapper {
    width:6000px; /* If you have more elements, increase the width accordingly */
}
#outer_wrapper #inner_wrapper div.box { /* Define the properties of inner block */
    width: 250px;
    height:300px;
    float: left;
    margin: 0 4px 0 0;
    border:1px grey solid;
}
</style>
</head>
<body>

<div id="outer_wrapper">
    <div id="inner_wrapper">
        <div class="box">
            <!-- Add desired content here -->
            HTMLExplorer.com - Explores HTML, CSS, Jquery, XML, PHP, JSON, Javascript 
        </div>
        <div class="box">
             <!-- Add desired content here -->
            HTMLExplorer.com - Explores HTML, CSS, Jquery, XML, PHP, JSON, Javascript 
        </div>
        <div class="box">
            <!-- Add desired content here -->
            HTMLExplorer.com - Explores HTML, CSS, Jquery, XML, PHP, JSON, Javascript 
        </div>
        <div class="box">
            <!-- Add desired content here -->
            HTMLExplorer.com - Explores HTML, CSS, Jquery, XML, PHP, JSON, Javascript 
        </div>
        <div class="box">
             <!-- Add desired content here -->
            HTMLExplorer.com - Explores HTML, CSS, Jquery, XML, PHP, JSON, Javascript 
        </div>
        <div class="box">
            <!-- Add desired content here -->
            HTMLExplorer.com - Explores HTML, CSS, Jquery, XML, PHP, JSON, Javascript 
        </div>
        <!-- more boxes here -->
    </div>
</div>
</body>
</html>
FrameWorker
  • 9
  • 1
  • 4
0

try using table structure, it's more back compatible. Check this outHorizontal Scrolling using Tables

phoenisx
  • 1,507
  • 17
  • 28