根据设备宽度将 div 顺序更改为 CSS

Change div order with CSS depending on device-width

我在响应式网站上工作时遇到了一个有趣的问题。我有一些 div 并排。可能有 2 到 6 个左右。当屏幕宽度不足以正确显示所有内容时,div 会垂直堆叠。 CSS.

就够简单了

问题是,我需要它们根据布局以不同的顺序排列。使用 2 或 3 个 div (Changing divs order based on width) 很容易做到这一点,但如果添加第四个 div 则更具挑战性。

我可以使用 position: absolute; 并手动设置位置,但这会导致父级缩小并且无法正确包含它们。

为了让这更复杂,我不能使用 JavaScript。

使用两列:

(未经测试)

HTML:

<div id="container">
    <div class="column-half column-half-2">
        First div on mobile, right div on desktop
    </div>
    <div class="column-half column-half-1">
        Second div on mobile, left div on desktop
    </div>
</div>

CSS:

.container {
    width: 80%;
    max-width: 1200px;
    margin: 0 auto;
    padding-bottom: 20px;
    position: relative;
}
.column-half {
    display: table-cell;
    padding: 25px;
    vertical-align: top;
    width: 40%;
}
.column-half-1 {
    float: left;
}
.column-half-2 {
    float: right;
}

HTML,有 4 列:

<div id="container">
    <div class="column-quarter column-quarter-3">
        First div on mobile, third div on desktop
    </div>
    <div class="column-quarter column-quarter-2">
        Second div on mobile, second div on desktop
    </div>
    <div class="column-quarter column-quarter-1">
        Third div on mobile, first div on desktop
    </div>
    <div class="column-quarter column-quarter-4">
        Fourth div on mobile, fourth div on desktop
    </div>
</div>

由于出色的 flexbox spec. Using the order and flex-flow 属性,这在 CSS 中是可行的,我们可以实现您想要的。无前缀,IE11 和所有长青浏览器都会支持这个。 IE10 前缀 -ms-order 不支持 flex-flow.

该解决方案考虑了您列出的所有限制条件:

  • 将给定顺序的元素列表显示为一行。
  • 当window太小时,改为分列显示。
  • 更改元素在列中显示时的顺序。

由于 Stack Snippets 的限制,您需要以全页模式查看演示,并调整浏览器大小才能看到效果。

.container div {
    width: 100px;
    height: 50px;
    display: inline-block;
}

.one { background: red; }
.two { background: orange; }
.three { background: yellow; }
.four { background: green; }
.five { background: blue; }

@media screen and (max-width: 531px) {
    .container { display: flex; flex-flow: column; }
    .five { order: 1; }
    .four { order: 2;  }
    .three { order: 3; }
    .two { order: 4; }
    .one { order: 5 }
}
<div class="container">
    <div class="one">I'm first</div>
    <div class="two">I'm second</div>
    <div class="three">I'm third</div>
    <div class="four">I'm fourth</div>
    <div class="five">I'm fifth</div>
</div>

或者,这里有一个 JSFiddle 演示。


如果您不喜欢冗长 CSS,您也可以简单地使用 flex-flow: column-reverse,而无需将 order 属性 分配给每个 div。同样的演示限制适用;全屏查看此演示并相应地调整浏览器大小 window。

.container div {
    width: 100px;
    height: 50px;
    display: inline-block;
}

.one { background: red; }
.two { background: orange; }
.three { background: yellow; }
.four { background: green; }
.five { background: blue; }

@media screen and (max-width: 531px) {
    .container { display: flex; flex-flow: column-reverse; }
}
<div class="container">
    <div class="one">I'm first</div>
    <div class="two">I'm second</div>
    <div class="three">I'm third</div>
    <div class="four">I'm fourth</div>
    <div class="five">I'm fifth</div>
</div>

值得指出的是 flex-flow 是一个 shorthand 属性 包含 flex-directionflex-wrap 属性。