gpt4 book ai didi

html - 根据设备宽度使用 CSS 更改 div 顺序

转载 作者:行者123 更新时间:2023-11-28 07:00:31 24 4
gpt4 key购买 nike

我在一个响应式网站上工作,遇到了一个有趣的问题。我有一些 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,这在 CSS 中是可行的规范使用 orderflex-flow属性,我们可以实现你想要的。无前缀,IE11 和所有长青浏览器都会支持这个。 IE10 前缀 -ms-order 不支持 flex-flow

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

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

由于 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 而无需为每个 div 分配 order 属性。同样的演示限制适用;全屏查看此演示并相应地调整浏览器窗口的大小。

.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 是包含 flex-directionflex-wrap 属性的简写属性。

关于html - 根据设备宽度使用 CSS 更改 div 顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33266343/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com