【问题标题】:CSS Floating Divs At Variable Heights [duplicate]CSS浮动div在可变高度[重复]
【发布时间】:2011-07-11 05:20:28
【问题描述】:

我有无限数量的 100 像素宽度的 div,可以放入 250 像素宽度的父级。无论高度如何,我都需要将 div 显示在行中,如图所示。我试过解决这个问题,但 div 高度似乎搞砸了。

非常感谢您的帮助。谢谢:)

        <style>
            #holder{
            width:250px;
            border:1px dotted blue;
            display:inline-block;
        }
        .box{
            width:100px;
            height:150px;
            background-color:#CCC;
            float:left;
            text-align:center;
            font-size:45px;
            display:inline-block;
        }
        .one{
            background-color:#0F0;
            height:200px;
        }

        .two{
            background-color:#0FF;
        }

        .three{
            background-color:#00F;
        }

        .four{
            background-color:#FF0;
        }
    </style>

    <div id="holder">
        <div class="box one">1</div>
        <div class="box two">2</div>
        <div class="box three">3</div>
        <div class="box four">4</div>
    </div>

这里是jsfiddle

这是我使用 javascript 所做并取得的成就 https://jsfiddle.net/8o0nwft9/

【问题讨论】:

标签: css html css-float


【解决方案1】:

据我所知,纯 CSS 无法解决此问题(适用于所有常见浏览器):

  • 浮动don't work。
  • display: inline-blockdoesn't work.
  • position: relative 和 position: absolute 需要 manual pixel tuning。如果您使用的是服务器端语言,并且正在处理图像(或具有可预测高度的东西),则可以使用服务器端代码“自动”处理像素调整。

改为使用jQuery Masonry。

【讨论】:

  • @stevenmc:没问题,但你肯定没有找到你提出的问题的解决方案:"I have infinite number of divs of a 100px width, which can fit into a 250px width parent. Regardless of height, I need the divs to be displayed in rows, as shown in the image."。
  • 现在你可以用 CSS 列相当合理地做到这一点,例如:w3bits.com/css-masonry
  • CSS 列不能完全解决问题,如果对齐应该像上面编号的那样发生。 CSS 列将导致布局,其中第 1 列将包含项目 1、2、3,第 2 列将包含项目 4、5、6。
  • 用flexbox也能做到吗?
  • @iChido:我不认为 flexbox 对这个问题有特别的帮助,但我不确定。我很久没有尝试只用 CSS 来做这件事了,我总是最终使用 JavaScript,因为它可以工作。我回答中的 Masonry 库甚至不再依赖 jQuery。单靠 CSS 能不能做到这一点取决于你的具体要求。
【解决方案2】:

假设您的需求更像您的彩色示例代码:

.box:nth-child(odd){
    clear:both;
}

如果是 3 行,那么 nth-child(3n+1)

【讨论】:

  • 我不想“清除”任何内容。
  • 这是最好的纯 CSS 解决方案。
  • 这解决了我的问题,因为我实际上想要一个不同的布局,但这并不是这个 SO 问题的真正答案。编辑:PS:No ie8 support 第 n 个孩子。
  • 我有 2 列,对我来说,这是最好的解决方案 :)
  • 这里有一个例子(3列)codepen.io/anon/pen/dPZvbb
【解决方案3】:

我提供这个答案是因为即使有很好的答案提供了解决方案 (using Masonry) 仍然不清楚为什么不能通过使用浮点数来实现这一点。

(这很重要 - #1)。

浮动元素会尽可能地向左或向右移动 原来的位置

这么说吧:

我们有 2 个 div

<div class="div5">div5</div>
<div class="div6">div6</div>

.div-blue{
    width:100px;
    height:100px;
    background: blue;
}

.div-red{
    width:50px;
    height:50px;
    background: red;
}

没有float,它们将是一个在另一个之下

如果我们float: rightdiv5,div6 位于div5 所在的行上,

/*the lines are just for illustrate*/

所以如果现在我们 float: left div6 它将尽可能向左移动,“在这一行”(见上面的#1),所以如果 div5改变它的行,div6 将跟随它。

现在让我们在等式中添加其他 div

<div class="div4">div4</div>
<div class="div5">div5</div>
<div class="div6">div6</div>

.div-gree{
    width:150px;
    height:150px;
    background: green;

    float:right;
}

我们有这个

如果我们将clear: right 设置为div5,我们将强制它采用下面的行div4

而div6 将在这个新行中浮动,无论是向右还是向左。

现在让我们以由于重复 Forcing div stack from left to right 而导致我来到这里的问题为例

这里是sn-p来测试它:

div{
    width:24%;
    margin-right: 1%;
    float: left;
    margin-top:5px;
    color: #fff;
    font-size: 24px;
    text-align: center;
}

.one{
    background-color:red;
    height: 50px;
}

.two{
    background-color:green;
    height:40px;
}

.three{
    background-color:orange;
    height:55px;
}

.four{
    background-color:magenta;
    height:25px;
}

.five{
    background-color:black;
    height:55px;
}
<div class="one">1</div>
<div class="two">2</div>
<div class="three">3</div>
<div class="four">4</div>
<div class="five">5</div>

<div class="one">1*</div>
<div class="three">2*</div>
<div class="four">3*</div>
<div class="two">4*</div>
<div class="five">5*</div>

在上图中,您可以看到div.5 是如何在div.3 旁边放置的,这是因为在它的行(由div.4 的行框定义)中,它可以走的最远,div.1* , div.2* 等,也浮动在 div.5 的左侧,但由于它们不适合该行,它们会转到下面的行(由 div.5 的行框定义)

现在请注意,当我们将div.2* 的高度减小到小于div.4* 时,它是如何传递给div.5* 的:

我希望这有助于澄清为什么不能使用浮点数来实现。因为标题“CSS Floating Divs At Variable Heights”,我只澄清了使用浮动(不是内联块),因为现在答案很长。

【讨论】:

    【解决方案4】:

    正如已经正确指出的那样,仅使用 CSS 是不可能的...谢天谢地,我现在在 http://isotope.metafizzy.co/ 中找到了解决方案

    似乎完全解决了问题。

    【讨论】:

    • 我真的认为你应该给@thirtydot这个答案 - 他/她建议使用 Masonry 这是实现这一目标的唯一方法。同位素是由同一个人建造的(自愿)付费的砌体版本。
    • 它不能解决任何问题。所有元素都具有相同的宽度并且绝对定位。
    【解决方案5】:

    在这条评论 (CSS Block float left) 的帮助下,我找到了答案。

    在我创建的每一“行”中,我添加一个类名left。
    在我创建的每个其他“行”上,我添加一个类名 right。

    然后我为每个类名向左浮动和向右浮动!

    唯一的复杂之处是我的内容顺序在“正确”行上颠倒了,但这可以使用 PHP 解决。

    感谢大家的帮助!

    #holder{
      width:200px;
      border:1px dotted blue;
      display:inline-block;
    }
    .box{
      width:100px;
      height:150px;
      background-color:#CCC;
      float:left;
      text-align:center;
      font-size:45px;
    }
    .one{
      background-color:#0F0;
      height:200px;
    }
    
    .two{
      background-color:#0FF;
    }
    
    .three{
      background-color:#00F;
      float:right;
    }
    
    .four{
      background-color:#FF0;
      float:right;
    }
    .left{float:left;}
    .right{float:right;}
    <div id="holder">
      <div class="box one left">1</div>
      <div class="box two left">2</div>
      <div class="box four right">4</div>
      <div class="box three right">3</div>
    </div>
    </body>

    【讨论】:

    • 除了你的演示看起来不像你的照片。正如您在问题中提出的那样,它不适用于“无限数量的 div”。例如,尝试调整您的这个答案中的代码以使用 8 个不同的高度.boxdivs。
    • 另外,在同一个元素上使用float: left 或float: right 和display: inline-block 是没有意义的。 display: inline-block 将通过 float 规则转换为 display: block(div 默认值)。换句话说,display: inline-block 完全没有做任何事情。
    • 好吧,你是对的,display:inline-block 不正确...我已经适当地编辑了我的解决方案。
    • 嗯。好的,你是对的......我仍在研究解决方案。完成后我会在这里发布。谢谢三十点。
    • 颠倒其中一列的顺序并不是一个好的解决方案。 HTML 应该对它的样式一无所知。
    【解决方案6】:

    多亏了三十点,我意识到我之前的回答并没有正确解决问题。这是我的第二次尝试,它使用 JQuery 作为仅 CSS 的解决方案似乎是不可能的:

    <html>
        <head>
            <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
            <script type="text/javascript" language="javascript">
                $(document).ready(function() {
                    var numberOfColumns = 3;
                    var numberOfColumnsPlusOne = numberOfColumns+1;
                    var marginBottom = 10;  //Top and bottom margins added
                    var kids = $('#holder:first-child').children();
                    var add;
                    $.each(kids, function(key, value) {
                        add = numberOfColumnsPlusOne+key;
                        if (add <= kids.length){
                            $('#holder:first-child :nth-child('+(numberOfColumnsPlusOne+key)+')').offset({ top: $('#holder:first-child :nth-child('+(key+1)+')').offset().top+$('#holder:first-child :nth-child('+(key+1)+')').height()+marginBottom });
                        }
                    });
                });             
            </script>
            <style>
                #holder{
                    width:270px;
                    border:1px dotted blue;
                    display:inline-block; /* Enables the holder to hold floated elements (look at dotted blue line with and without */
                }
                .box{
                    width:80px;
                    height:150px;
                    background-color:#CCC;
                    margin:5px;
                    text-align:center;
                    font-size:45px;
                }
                .one{
                    height:86px;
                }
                .two{
                    height:130px;
                }
                .three{
                    height:60px;
                }
                .four{
                    clear:both;
                    height:107px;
                }
                .five{
                    height:89px;
                }
                .six{
                    height:89px;
                }
                .left{float:left;}
                .right{float:right;}
            </style>
        </head>
        <body>      
            <div id="holder">
                <div class="box one left">1</div>
                <div class="box two left">2</div>
                <div class="box three left">3</div>
                <div class="box four left">4</div>
                <div class="box five left">5</div>
                <div class="box six left">6</div>
            </div>
        </body>
    </body>
    

    我的解决方案剩下的唯一问题是,当一个盒子是两个盒子宽度而不是一个盒子时会发生什么。我仍在研究这个解决方案。完成后我会发布。

    【讨论】:

    • “这是我的第二次尝试”..?什么?为什么您将所有尝试发布为不同的答案..?你可以简单地edit你的答案并按照你想要的方式更新它。
    【解决方案7】:

    如果有人仍在寻找替代品,这里有一个。尝试使用 (-moz-/-webkit-) 列宽属性。它处理可变 div 高度问题。但是 column-width 在列的末尾添加了新的 div。

    否则,jQuery Masonry 效果最好。

    【讨论】:

      【解决方案8】:

      这可能不是每个人的确切解决方案,但我发现(从字面上看)跳出框框思考适用于许多情况:在许多情况下,您可以填充左栏,而不是从左到右显示框首先,比去中间,用方框填充它,最后用方框填充右列。 您的图像将是:

      :

      如果您使用像 php 这样的脚本语言,您还可以通过向其中添加一个新框并在所有列都填满时输出来从左到右填充列。例如(未经测试的 php 代码):

      $col1 = '<div class="col1"> <div>box1</div>';
      $col2 = '<div class="col2"> <div>box2</div>';
      $col3 = '<div class="col3"> <div>box3</div>';
      $col1 .= '<div>box4</div> </div>'; //last </div> closes the col1 div
      $col2 .= '<div>box5</div> </div>';
      $col3 .= '<div>box6</div> </div>';
      echo $col1.$col2.$col3;
      

      $col1、$col2 和 $col3 可以有 float:left 和 width:33%,将 div 内的框设置为全宽且不浮动。

      显然,如果您使用 javascript / jquery 动态加载框,则最好也采用这种方式设置它们的样式,如该线程的其他答案中所述。

      【讨论】:

        【解决方案9】:

        要显示,只需将此 css 放入您的 div,然后您就有了所需的布局。 无需任何插件或JS。

         .Your_Outer {
            background-color: #FFF;
            border: 1px solid #aaaaaa;
            float: none;
            display:inline-block;
            vertical-align:top;
            margin-left: 5px;
            margin-bottom: 5px;
            min-width: 152.5px;
            max-width: 152.5px;
        }
        

        您可以根据需要编辑代码:)

        【讨论】:

          【解决方案10】:

          在现代浏览器上,您可以这样做:

          display: inline-block;
          vertical-align: top;
          

          【讨论】:

          • 为什么会被否决?
          • 嗯,它确实有效,并且您链接到的 sn-p 也有效(至少在 Chrome 中)。所以你可能想给我们一个更好的解释。
          • 上一个问题链接了以下小提琴,它显示了这种方法的结果:jsfiddle.net/bCgea/1 这还不错,但也不是他们想要的。柱流不连续。
          猜你喜欢
          • 2014-03-21
          • 1970-01-01
          • 2011-06-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-01-20
          相关资源
          最近更新 更多