gpt4 book ai didi

html - CSS flexbox |在移动设备中重新排序元素

转载 作者:太空宇宙 更新时间:2023-11-03 21:04:07 24 4
gpt4 key购买 nike

我正在尝试学习 CSS Flexbox,但发现了一个障碍。

我有在桌面屏幕尺寸上向右和向左显示的内容,对于移动设备,我有 flex-direction: column

见下图:

桌面: enter image description here

手机: enter image description here

这是实现这样的代码:

 <div class="container">
<div class="box box1">
<div class="a">a</div>
<div class="b">b</div>
</div>
<div class="box box2">
<div class="c">c</div>
<div class="d">d</div>
</div>
</div>

这些是 flexbox 样式:

.box {
color: white;
font-size: 100px;
text-align: center;
text-shadow: 4px 4px 0 rgba(0, 0, 0, 0.1);
padding: 10px;
width: 100vw;
}

.container {
display: flex;
height: 100vh;
}

.a, .b, .c, .d {
height: 50%;
}

@media all and (max-width: 500px) {
.container {
flex-direction: column;
}

.box {
height: 50vh;
}
}

在移动设备中,我如何才能让以下 div 显示在列中(按原样)但是按以下顺序:

一个

c

d

b

不幸的是,我似乎找不到解决方案。

我有一个 CodePen here重要的 CSS 行从第 162 行开始。

最佳答案

您可以考虑在 .box 元素上使用 display:contents ( https://caniuse.com/#feat=css-display-contents ),然后您将能够在内部元素上使用顺序:

.box {
color: white;
font-size: 80px;
text-align: center;
text-shadow: 4px 4px 0 rgba(0, 0, 0, 0.1);
padding: 10px;
width: 100vw;
}
body {
margin:0;
}
.container {
display: flex;
height: 100vh;
background:blue;
}

.a,.b,.c,.d {
height: 50%;
border:2px solid;
}

@media all and (max-width: 500px) {
.container {
flex-direction: column;
}
.box {
display:contents;
}
.b {
order:2;
}
}
<div class="container">
<div class="box box1">
<div class="a">a</div>
<div class="b">b</div>
</div>
<div class="box box2">
<div class="c">c</div>
<div class="d">d</div>
</div>
</div>

display: contents causes an element's children to appear as if they were direct children of the element's parent, ignoring the element itself. This can be useful when a wrapper element should be ignored when using CSS grid or similar layout techniques.


如果你愿意改变 html,你可以像下面那样做:

.container > * {
color: white;
font-size: 80px;
text-align: center;
text-shadow: 4px 4px 0 rgba(0, 0, 0, 0.1);
padding: 10px;
border:2px solid;
box-sizing:border-box;
}
body {
margin:0;
}
.container {
display: flex;
flex-direction:column;
flex-wrap:wrap;
height: 100vh;
background:blue;
padding:10px;
box-sizing:border-box;
}

.a,.b,.c,.d {
height: 50%;
width:50%;
}

@media all and (max-width: 500px) {
.a,.b,.c,.d {
width:100%;
height:25%;
}
.b {
order:2;
}
}
<div class="container">
<div class="a">a</div>
<div class="b">b</div>
<div class="c">c</div>
<div class="d">d</div>
</div>

还有 CSS 网格:

.container > * {
color: white;
font-size: 80px;
text-align: center;
text-shadow: 4px 4px 0 rgba(0, 0, 0, 0.1);
padding: 10px;
box-sizing:border-box;
border:2px solid;
}
body {
margin:0;
}
.container {
display: grid;
grid-template-areas:
'a b'
'c d';
grid-template-columns:1fr 1fr;
grid-template-rows:1fr 1fr;
grid-gap:10px;
min-height: 100vh;
background:blue;
padding:10px;
box-sizing:border-box;
}

.a {
grid-area:a;
}
.b {
grid-area:b;
}
.c {
grid-area:c;
}
.d {
grid-area:d;
}

@media all and (max-width: 500px) {
.container {
grid-template-areas:
'a'
'c'
'd'
'b';
grid-template-columns:1fr;
grid-template-rows:1fr 1fr 1fr 1fr;
}
}
<div class="container">
<div class="a">a</div>
<div class="b">b</div>
<div class="c">c</div>
<div class="d">d</div>
</div>

关于html - CSS flexbox |在移动设备中重新排序元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55679244/

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