2

I am trying to make a horizontally scrollable website. I took screenshots of my clients demo website, and labeled them out as images 1-8. To make the site scrollable horizontally I put all the images in a div and set no-wrap property. The problem is: Each image is too big for my screen. I want each image to perfectly fit the size of my view port. My question is: How do I make each image fit the entire screen fully, regardless of screen size?

HTML:

<html>  
  <head>
    <link rel="stylesheet" type="text/css" href="main.css">
    <script src="jquery-2.1.1.js"></script>
    <script src="jquery.scrollpath.js"></script>
    <script src="createScroll.js"></script>
  </head>
  <body>
    <div id="scrollable">
      <img src="1.png"></img> 
      <img src="2.png"></img>
      <img src="4.png"></img>
      <img src="5.png"></img>
      <img src="6.png"></img>
      <img src="7.png"></img>
      <img src="8.png"></img>
    </div>
  </body>
</html>

CSS:

#scrollable {
  display: inline;
  white-space:nowrap;
  float: left;
}
chopper draw lion4
  • 12,401
  • 13
  • 53
  • 100

3 Answers3

3

Try this, use vh with max-height:

CSS:

#scrollable {
    width: 100%;
    white-space: nowrap;
    overflow-x: scroll;
}

img {
    display: inline-block;
    height: 100vh;
}

See JSFiddle: http://jsfiddle.net/48ck23L9/4/

Note that the second image is originally smaller than the viewport.

mikelt21
  • 2,728
  • 4
  • 23
  • 33
1

If you want something really responsive I'd go with DIV elements and background images. It's about images after all.

jsBin demo (resize to see the magic!)

<div id="scrollable">
  <div style="background-image: url(1.jpg);"></div>
  <div style="background-image: url(2.jpg);"></div>
  <div style="background-image: url(3.jpg);"></div>
</div>

The Simple CSS:

#scrollable{
  height:100%;
  overflow-x: auto;
  white-space: nowrap;
}

#scrollable > div{
  background: none 50% / cover;
  display:inline-block;
  width:100%;
  height:100%;
  margin-right:-4px;
}

Note: Usign this technique as you can clearly see the images will be cropped-to-fit but every image will do exactly what you asked for:

I want each image to perfectly fit the size of my view port. My question is: How do I make each image fit the entire screen fully, regardless of screen size?

Otherwise, if you want to preserve the whole image, than the answer is banally simple. make it 100% height the container area.

Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
0

You can use the vh and vw units.

img {
    height: 100vh;
    width: 100vw;
}
recursive
  • 83,943
  • 34
  • 151
  • 241
  • this stretches the image to the viewport size so that the image will skew. – mikelt21 Oct 30 '14 at 20:12
  • Well, yes, that's literally what the question is asking for though. The question is asking for all images to fit to the browser window, "regardless of screen size". That means sometimes images will have to be skewed. – recursive Oct 30 '14 at 22:29