最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - how to set a list of images next to eachother - Stack Overflow

matteradmin5PV0评论

I have a list of images but i want all the images next to each other. and than with horizontal scroll.

I tried to set a max height for the scroll menu but that his hide the 2 images below. I also disabled vertical scroll but that doesn't work to.

If it is possible I want only use css. if I need javascript to fix it I use Jquery.

My html:

<!DOCTYPE html>
<html>
<head>
  <title>test</title>
  <link href="css/index.css" rel="stylesheet"/>
  <link href="css/global.css" rel="stylesheet"/>
  <meta charset="UTF-8">
</head>
<body>
    <div id="list">
        <img src="img/1.jpg" class="img">
        <img src="img/2.jpg" class="img">
        <img src="img/3.jpg" class="img">
        <img src="img/4.jpg" class="img">
        <img src="img/5.jpg" class="img">
        <img src="img/6.jpg" class="img">
        <img src="img/7.jpg" class="img">
        <img src="img/8.jpg" class="img">
    </div>
    <div id="output">

    </div>
    <script src="js/jquery.js"></script>
    <script src="js/image.js"></script>

</body>
</html>

my css:

body{
 padding:0px;
 margin:0px;
 overflow-y:hidden;
}
.img{
 height:100px;
 width:200px;
 display:inline-block;
 margin-left:0px;
}

.loaded{
 width:100%;
}
#list{
 overflow-y:hidden;
 width:auto;
}

I have a list of images but i want all the images next to each other. and than with horizontal scroll.

I tried to set a max height for the scroll menu but that his hide the 2 images below. I also disabled vertical scroll but that doesn't work to.

If it is possible I want only use css. if I need javascript to fix it I use Jquery.

My html:

<!DOCTYPE html>
<html>
<head>
  <title>test</title>
  <link href="css/index.css" rel="stylesheet"/>
  <link href="css/global.css" rel="stylesheet"/>
  <meta charset="UTF-8">
</head>
<body>
    <div id="list">
        <img src="img/1.jpg" class="img">
        <img src="img/2.jpg" class="img">
        <img src="img/3.jpg" class="img">
        <img src="img/4.jpg" class="img">
        <img src="img/5.jpg" class="img">
        <img src="img/6.jpg" class="img">
        <img src="img/7.jpg" class="img">
        <img src="img/8.jpg" class="img">
    </div>
    <div id="output">

    </div>
    <script src="js/jquery.js"></script>
    <script src="js/image.js"></script>

</body>
</html>

my css:

body{
 padding:0px;
 margin:0px;
 overflow-y:hidden;
}
.img{
 height:100px;
 width:200px;
 display:inline-block;
 margin-left:0px;
}

.loaded{
 width:100%;
}
#list{
 overflow-y:hidden;
 width:auto;
}
Share Improve this question asked Nov 2, 2014 at 13:05 Vinc199789Vinc199789 1,0461 gold badge11 silver badges32 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

Simply add

  white-space: nowrap;

to #list

Since your .img images are correctly set to inline-block you can now control the parent element's whitespace with https://developer.mozilla/en-US/docs/Web/CSS/white-space (which applies to the inner inline, inline-block children.)

nowrap
Collapses whitespace as for normal, but suppresses line breaks (text wrapping) within text.

As @roko-c-buljan said simply add white-space: nowrap; to the #list. This suppresses line-breaks in the text, which the images are as they have display: inline-block.

body{
 padding:0px;
 margin:0px;
 overflow-y:hidden;
}
.img{
 height:100px;
 width:200px;
 display:inline-block;
 margin-left:0px;
}

.loaded{
 width:100%;
}
#list{
 overflow-y:hidden;
 width:auto;
 white-space:nowrap;
}
<!DOCTYPE html>
<html>
<head>
  <title>test</title>
  <link href="css/index.css" rel="stylesheet"/>
  <link href="css/global.css" rel="stylesheet"/>
  <meta charset="UTF-8">
</head>
<body>
    <div id="list">
        <img src="img/1.jpg" class="img">
        <img src="img/2.jpg" class="img">
        <img src="img/3.jpg" class="img">
        <img src="img/4.jpg" class="img">
        <img src="img/5.jpg" class="img">
        <img src="img/6.jpg" class="img">
        <img src="img/7.jpg" class="img">
        <img src="img/8.jpg" class="img">
    </div>
    <div id="output">

    </div>
    <script src="js/jquery.js"></script>
    <script src="js/image.js"></script>

</body>
</html>

Are you looking for something like this?

http://codepen.io/kozumii/pen/IoAFb

#list{
  overflow-x:scroll;
  white-space:nowrap;
}

If you're familiar with floats,you can add

#list img {
  float:left;
}
Post a comment

comment list (0)

  1. No comments so far