css - 使用水平滚动条显示图像

时间:2022-04-30 20:37:04

I'm trying to display a number of images horizontally inside of a fixed-width div. I would like to use a horizontal scroll bar to display the images which do not fit inside the div.

我试图在固定宽度div内水平显示多个图像。我想使用水平滚动条来显示不适合div内部的图像。

However, the images are displaying vertically, rather than horizontally. Is there a way to force them to display side-by-side?

但是,图像是垂直显示,而不是水平显示。有没有办法迫使他们并排显示?

div#event {
width: 150px;
overflow-x: scroll;
overflow-y: hidden;
}

div#event ul { list-style: none; }

div#event img {
width: 100px;
float: left;
}

<div id="lasteventimg">
<ul><li><img src="./gfx/gallery/image1.jpg" /></li>
<li><img src="./gfx/gallery/image2.jpg" /></li>
<li><img src="./gfx/gallery/image3.jpg" /></li>
</ul>
</div>  

6 个解决方案

#1


8  

You will have to display the list items inline or float them and give the ul a very large width to avoid items moving to the next line:

您必须显示列表项内联或浮动它们并给ul一个非常大的宽度,以避免项目移动到下一行:

ul {
  width: 10000px;    // for example
  white-space: nowrap;
}
li {
  float: left:
  // or
  display: inline;
}

#2


12  

Correct code for arbitrary number of images, if you don't know ul width:

如果您不知道ul宽度,请更正任意数量图像的代码:

#lasteventimg {width:150px;overflow-x:scroll;overflow-y:hidden;}
ul {list-style:none; display:block;white-space:nowrap;}
li {width: 100px;display:inline;}


<div id="lasteventimg">
<ul>
<li><img src="./gfx/gallery/image1.jpg" /></li>
<li><img src="./gfx/gallery/image2.jpg" /></li>
<li><img src="./gfx/gallery/image3.jpg" /></li>
<li><img src="./gfx/gallery/image4.jpg" /></li>
<li><img src="./gfx/gallery/image5.jpg" /></li>
</ul>
</div>

#3


1  

I'd go with

我会去的

div#lasteventimg ul li {
  display: inline;
}

To make sure the li elements aren't rendered as block elements.

确保li元素不会呈现为块元素。

#4


1  

Well first of all, you need to change your styles from #event to #lasteventimg. Then, if you set the width of the ul to be wide enough to accommodate all the images, you should see the behavior that you're trying to get:

首先,您需要将样式从#event更改为#lasteventimg。然后,如果您将ul的宽度设置得足够宽以容纳所有图像,您应该会看到您尝试获取的行为:

div#lasteventimg {
width: 150px;
overflow-x: scroll;
overflow-y: hidden;
}

div#lasteventimg ul { list-style: none; width: 300px; }

div#lasteventimg img {
width: 100px;
float: left;
}

<div id="lasteventimg">
<ul><li><img src="./gfx/gallery/image1.jpg" /></li>
<li><img src="./gfx/gallery/image2.jpg" /></li>
<li><img src="./gfx/gallery/image3.jpg" /></li>
</ul>
</div>    

#5


0  

You need to change your list, so the elements are rendered horizontally rather then the default vertical.

您需要更改列表,因此元素将水平呈现,而不是默认垂直。

#imagelist li
{
display: inline;
list-style-type: none;
}

#6


0  

Why don't you try this?

你为什么不试试这个?

ul
{
   width: 10000px; 
   white-space: nowrap;
}

li 
{
   display: block;
   float:left;
}

#1


8  

You will have to display the list items inline or float them and give the ul a very large width to avoid items moving to the next line:

您必须显示列表项内联或浮动它们并给ul一个非常大的宽度,以避免项目移动到下一行:

ul {
  width: 10000px;    // for example
  white-space: nowrap;
}
li {
  float: left:
  // or
  display: inline;
}

#2


12  

Correct code for arbitrary number of images, if you don't know ul width:

如果您不知道ul宽度,请更正任意数量图像的代码:

#lasteventimg {width:150px;overflow-x:scroll;overflow-y:hidden;}
ul {list-style:none; display:block;white-space:nowrap;}
li {width: 100px;display:inline;}


<div id="lasteventimg">
<ul>
<li><img src="./gfx/gallery/image1.jpg" /></li>
<li><img src="./gfx/gallery/image2.jpg" /></li>
<li><img src="./gfx/gallery/image3.jpg" /></li>
<li><img src="./gfx/gallery/image4.jpg" /></li>
<li><img src="./gfx/gallery/image5.jpg" /></li>
</ul>
</div>

#3


1  

I'd go with

我会去的

div#lasteventimg ul li {
  display: inline;
}

To make sure the li elements aren't rendered as block elements.

确保li元素不会呈现为块元素。

#4


1  

Well first of all, you need to change your styles from #event to #lasteventimg. Then, if you set the width of the ul to be wide enough to accommodate all the images, you should see the behavior that you're trying to get:

首先,您需要将样式从#event更改为#lasteventimg。然后,如果您将ul的宽度设置得足够宽以容纳所有图像,您应该会看到您尝试获取的行为:

div#lasteventimg {
width: 150px;
overflow-x: scroll;
overflow-y: hidden;
}

div#lasteventimg ul { list-style: none; width: 300px; }

div#lasteventimg img {
width: 100px;
float: left;
}

<div id="lasteventimg">
<ul><li><img src="./gfx/gallery/image1.jpg" /></li>
<li><img src="./gfx/gallery/image2.jpg" /></li>
<li><img src="./gfx/gallery/image3.jpg" /></li>
</ul>
</div>    

#5


0  

You need to change your list, so the elements are rendered horizontally rather then the default vertical.

您需要更改列表,因此元素将水平呈现,而不是默认垂直。

#imagelist li
{
display: inline;
list-style-type: none;
}

#6


0  

Why don't you try this?

你为什么不试试这个?

ul
{
   width: 10000px; 
   white-space: nowrap;
}

li 
{
   display: block;
   float:left;
}