如何在CSS中的可调整大小的空间中排列不同长度的项目?

发布于 2024-07-06 03:44:56 字数 493 浏览 8 评论 0原文

我想大致这样排列项目:

item1      item2           i3           longitemname
i4         longitemname2   anotheritem  i5

基本上,不同长度的项目排列在类似表格的结构中。 棘手的部分是这些容器的大小可能会有所不同,我想在每一行中容纳尽可能多的项目 - 换句话说,我事先不知道一行中可以容纳多少项目,以及页面是否适合调整大小后,项目应该重新流动以适应。 例如,最初每行可以容纳 10 个项目,但在调整大小时可以减少到 5 个。

我认为我不能使用 html 表格,因为我不知道列数(因为我不知道有多少列)将适合一条线)。 我可以使用 css 来浮动它们,但由于它们的大小不同,因此它们不会对齐。

到目前为止,我唯一能想到的就是使用 javascript 获取最大项目的大小,将所有项目的大小设置为该大小,然后浮动剩下的所有内容。

还有更好的建议吗?

I'd like to line up items approximately like this:

item1      item2           i3           longitemname
i4         longitemname2   anotheritem  i5

Basically items of varying length arranged in a table like structure. The tricky part is the container for these can vary in size and I'd like to fit as many as I can in each row - in other words, I won't know beforehand how many items fit in a line, and if the page is resized the items should re-flow themselves to accommodate. E.g. initially 10 items could fit on each line, but on resize it could be reduced to 5.

I don't think I can use an html table since I don't know the number of columns (since I don't know how many will fit on a line). I can use css to float them, but since they're of varying size they won't line up.

So far the only thing I can think of is to use javascript to get the size of largest item, set the size of all items to that size, and float everything left.

Any better suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

转身以后 2024-07-13 03:44:56

当你得到一件大得离谱的东西而让其他东西看起来很小时,会发生什么? 我会考虑两种解决方案:

  1. 您已经想出的涉及 float:left; 的 解决方案 规则和 jQuery,但也有一个最大 max_width 或只需
  2. 根据您期望的值预先决定所有项目的预设宽度

然后添加溢出:隐藏; 规则,以便较长的物品不会破坏桌面外观。 您甚至可以更改 jQuery 函数来修剪较长的项目,在末尾添加省略号 (...)。

What happens when you get one item thats rediculously large and makes the rest look small? I would consider two solutions:

  1. What you've already come up with involving a float:left; rule and jQuery, but with a max max_width as well or
  2. Just decide on a preset width for all items before hand, based on what values you expect to be in there

Then add an overflow:hidden; rule so items that are longer don't scew the table-look. You could even change the jQuery function to trim items that are longer, adding an elipsis (...) to the end.

揽月 2024-07-13 03:44:56

这可以使用浮动 div 来完成,计算最大宽度,并将所有宽度设置为最大值。 这是执行此操作的 jquery 代码:

html:

<div class="item">something</div>
<div class="item">something else</div>

css:

div.item { float: left; }

jquery:

var max_width=0;
$('div.item').each( function() { if ($(this).width() > max_width) { max_width=$(this).width(); } } ).width(max_width);

不太难看,但也不太漂亮,我仍然愿意接受更好的建议...

This can be done using floated div's, calculating the max width, and setting all widths to the max. Here's jquery code to do it:

html:

<div class="item">something</div>
<div class="item">something else</div>

css:

div.item { float: left; }

jquery:

var max_width=0;
$('div.item').each( function() { if ($(this).width() > max_width) { max_width=$(this).width(); } } ).width(max_width);

Not too ugly but not too pretty either, I'm still open to better suggestions...

匿名。 2024-07-13 03:44:56

您可以使用以下内容,使每列具有相同的宽度。

您将拥有固定的列宽,但列表会自动重排。
我添加了一些额外的 HTML,但现在它可以在 FF 和 IE 中运行。

html:

<ul class="ColumnBasedList">
  <li><span>Item1 2</span></li>
  <li><span>Item2 3</span></li>
  <li><span>Item3 5</span></li>
  <li><span>Item4 6</span></li>
  <li><span>Item5 7</span></li>
  <li><span>Item6 8</span></li>
</ul>

css:

.ColumnBasedList
{
    width: 80%;
    margin: 0;
    padding: 0;
}

.ColumnBasedList li
{
    list-style-type: none;
    display:inline;
}

.ColumnBasedList li span
{
    display: -moz-inline-block;
    display: inline-block;
    width: 20em;
    margin: 0.3em;
}

You could use the following with each column the same width.

You'll have a fixed column width, but the list will reflow itself automatically.
I added a little bit of extra HTML but now it works in FF en IE.

html:

<ul class="ColumnBasedList">
  <li><span>Item1 2</span></li>
  <li><span>Item2 3</span></li>
  <li><span>Item3 5</span></li>
  <li><span>Item4 6</span></li>
  <li><span>Item5 7</span></li>
  <li><span>Item6 8</span></li>
</ul>

css:

.ColumnBasedList
{
    width: 80%;
    margin: 0;
    padding: 0;
}

.ColumnBasedList li
{
    list-style-type: none;
    display:inline;
}

.ColumnBasedList li span
{
    display: -moz-inline-block;
    display: inline-block;
    width: 20em;
    margin: 0.3em;
}
找个人就嫁了吧 2024-07-13 03:44:56

您可以浮动几个无序列表,如下所示:

<ul style="float: left;">
    <li>Short</li>
    <li>Loooong</li>
    <li>Even longer</li>
</ul>
<ul style="float: left;">
    <li>Loooong</li>
    <li>Short</li>
    <li>...</li>
</ul>
<ul style="float: left;">
    <li>Semi long</li>
    <li>...</li>
    <li>Short</li>
</ul>

这需要您对应将多少列表中的多少列表项推入 DOM 进行一些计算,但是这样 - 当您重新调整容纳容器的大小时列表 - 列表将浮动以适合该容器。

You could float a couple of unordered lists, like this:

<ul style="float: left;">
    <li>Short</li>
    <li>Loooong</li>
    <li>Even longer</li>
</ul>
<ul style="float: left;">
    <li>Loooong</li>
    <li>Short</li>
    <li>...</li>
</ul>
<ul style="float: left;">
    <li>Semi long</li>
    <li>...</li>
    <li>Short</li>
</ul>

It would require you to do some calculations on how many list-items in how many lists should be shoved into the DOM, but this way - when you re-size the container which holds the lists - the lists will float to fit into that container.

染墨丶若流云 2024-07-13 03:44:56

那么,您可以为每个“项目”创建一个浮动 div,然后循环遍历其属性并使用换行符吗? 然后,当您完成属性循环后,关闭 div 并开始一个新的? 这样它们就会像柱子一样并且彼此相邻地漂浮。 如果窗口很小,它们会下降到下一个“行”,调整大小时,会浮回右侧(实际上是左侧:-)

Well, could you just create a floated div for each 'item', and loop through its properties and use linebreaks? Then when you finish looping over the properties, close the div and start a new one? That way they will be like columns and just float next to each other. If the window is small, they'll drop down to the next 'line' and when resized, will float back up to the right (which is really the left :-)

温柔戏命师 2024-07-13 03:44:56

实际上,您需要计算 maxWidth 和 maxHeight,然后在页面加载后使用 Javascript 中的调整大小函数。 我必须为之前的项目执行此操作,并且如果 div 的高度发生变化,其中一个浏览器(FF?)将捕获/偏移下面的浏览器。

You would actually need to calculate the maxWidth and the maxHeight and then go through with a resize function in Javascript after the page loads. I had to do this for a prior project and one of the browsers (FF?) will snag/offset the ones underneath if the height of the divs vary.

说好的呢 2024-07-13 03:44:56

您可以使用向左浮动的块级元素,但是您将需要一些 javascript 来检查大小,找到最大的元素,并将它们全部设置为该宽度。

编辑:刚刚阅读了您帖子的后半部分,并看到您建议了这个修复。 将此帖子视为您当前想法的 +1 :)

You could use block level elements floated left, but you will need some javascript to check the sizes, find the largest one, and set them all to that width.

EDIT: Just read the second half of your post, and saw that you suggested just this fix. Count this post as +1 for your current idea :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文