【问题标题】:Floating divs with different height, fill empty space浮动不同高度的div,填充空白
【发布时间】:2014-06-10 00:34:04
【问题描述】:

我有 3 个浮动 div(第二个 /green/ 比第三个 /blue/ 高),虽然视口较窄,但我想最后一个(第三个)div 会占用(填充)第一个 div 下方的空白空间。

我可以通过使用媒体查询来实现这一点:

@media only screen and (max-width : 750px) 
{
    #third
    {
        margin-top: -100px;
    }
}

@media only screen and (max-width : 500px) 
{
    #third
    {
        margin-top: 0px;
    }
}

参见 jsFiddle:http://jsfiddle.net/s2G5J/15/ 如何在没有媒体查询的情况下实现此效果(我不知道 div 的高度)。提前谢谢!

【问题讨论】:

标签: css css-float


【解决方案1】:

试试这个:

HTML

    <div id="container">
        <div id="first"></div>
        <div id="second"></div>
        <div id="third"></div>
    </div>

CSS

#container
{
    max-width: 500px;
}

#first, #second, #third
{
    width: 250px;    
}

#first, #third
{
    float: left;    
    height: 100px;
}

#second
{
    float: right;
    height: 200px;
}

#first
{
    background-color: yellow;    
}

#second
{
    background-color: lime;
}

#third
{
    background-color: blue;
}

@media only screen and (max-width : *500px*) 
{
    #container > div
    {
        float: none;
    }
}

您可能会注意到石灰盒在达到限制后会向右浮动一段时间。那只是因为身体的边距和填充物以及它周围的东西。只需根据它周围的元素设置用星号包围的数字(516px 是 jsfiddle 和默认边距和填充的数字)。

【讨论】:

  • 我想要没有媒体查询的解决方案。澄清一下:当视口宽于 750px 时,所有 3 个 div 必须显示在一行中(并排)
  • 我认为如果没有媒体查询,您将无法做到这一点。您需要 javascript,如果您愿意,我可以显示。如果您希望它们并排使用min-width: 750 制作另一个并将所有内容浮动
猜你喜欢
  • 1970-01-01
  • 2023-03-21
  • 2010-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-25
  • 1970-01-01
相关资源
最近更新 更多