【问题标题】:Responsive Design. Grid layout for floating divs, but one exception响应式设计。浮动 div 的网格布局,但有一个例外
【发布时间】:2015-12-04 12:50:09
【问题描述】:

也许这是一个棘手的问题,也许不是。我有一个带有浮动 div 的网格布局。我想建立一个响应式设计。当您调整浏览器窗口的大小时,div 将浮动在隐藏区域中。这很容易。

但我尝试的是最后一个“更多”div 将始终是网格中的最后一个 div。

示例代码如下

HTML

<div class="bar">
    <div id="1" class="square">1</div>
        <div id="2" class="square">2</div>
        <div id="3" class="square">3</div>
        <div id="4" class="square">4</div>
        <div class="more">more</div>
    </div>
</div>

CSS

div.more {
width: 60px;
height: 60px;
background: red;
color: #000;
text-align: center; 
float: left;
}

div.container {
position: relative;
width: 100%;
height: 60px;
}

div.square {
position: relative;
background: green;
width: 60px;
height: 60px;
float: left;
margin-right: 5px;
}

div.bar {
width: 100%;
height: 60px;
background: #000;
overflow: hidden;
}

这里我有一个简单的小提琴演示。当您调整网格时,div 是浮动的。但是最后一个 div 也是浮动的,这不是我这里需要的。

fiddle demo

这可以通过纯css完成还是需要js?感谢您的帮助。

鲁文

【问题讨论】:

  • 不太确定我是否理解,但这与您要找的一样吗? jsfiddle.net/2s4zr470/5
  • @GCyrillus 我很确定 OP 想要它像我的答案一样,向右浮动有点用,但是当页面很大时,它看起来很有趣。
  • @GCyrillus 哦,是的,我是这么想的,哈哈 :)

标签: javascript html css responsive-design grid


【解决方案1】:

我会使用这样的媒体查询http://jsfiddle.net/DIRTY_SMITH/2s4zr470/4/

HTML

<div class="bar">
    <div id="one" class="square">1</div>
        <div id="two" class="square">2</div>
        <div id="three" class="square">3</div>
        <div id="four" class="square">4</div>
        <div class="more">more</div>
    </div>
</div>

CSS

div.more {
        width: 60px;
        height: 60px;
        background: red;
        color: #000;
        float: right;
        text-align: center; 
        float: left;

    }

div.container {
        position: relative;
        width: 100%;
        height: 60px;
    }

div.square {
        position: relative;
        background: green;
        width: 60px;
        height: 60px;
        float: left;
        margin-right: 5px;
    }

 div.bar {
        width: 100%;
        height: 60px;
        background: #000;
    }
@media (max-width: 340px){
    #four{display: none;}

}
@media (max-width: 280px){
    #three{display: none;}

}
@media (max-width: 220px){
    #two{display: none;}

}
@media (max-width: 160px){
    #one{display: none;}

}

【讨论】:

  • 哇哦,我从来没有用过它们。我是响应式设计的新手。但这正是我正在寻找的。谢谢你,兄弟。你为我节省了很多时间!
  • 只是想知道是否有另一种方式或更短的“自动化”方式,例如,当我有9 个方格 + 更多的一个。谢谢。
猜你喜欢
  • 2013-01-29
  • 2018-07-28
  • 1970-01-01
  • 2015-08-05
  • 1970-01-01
  • 1970-01-01
  • 2012-07-10
  • 2012-10-01
  • 2013-07-14
相关资源
最近更新 更多