gpt4 book ai didi

html - 使用 "order"属性在底部开始 div 并随着我添加更多而逐渐上升

转载 作者:行者123 更新时间:2023-11-28 03:38:20 26 4
gpt4 key购买 nike

正如标题所示,我正在搞乱 display: flexorder:。当我从第一个订购的 div (order: 0;) 开始时,它将从我页面的顶部开始。当我添加第一个 (order: 1;) 时,它从页面底部开始,依此类推。还有什么奇怪的是,当我在顺序中添加更多 div 时,它会逐渐将它们在页面上向上移动到它们最初应该开始的位置。

.container {
display: flex;
flex-wrap: wrap;
}

.box {
width: 100%;
height: 100px;
text-align: center;
}

.lime {
width: 100%;
order: 0;
background-color: lime;
}

.red {
width: 50%;
order: 1;
background-color: red;
}

.orange {
width: 50%;
order: 2;
background-color: orange;
}

.teal {
width: 100%;
order: 3;
background-color: teal;
}

.light_blue {
width: 20%;
order: 4;
background-color: lightblue;
}

.dark_blue {
width: 60%;
order: 5;
background-color: darkblue;
}

.green {
width: 20%;
order: 6;
background-color: green;
}
<body>
<div class="container">
<div class="box lime"></div>
<div class="box dark_blue"></div>
<div class="box light_blue"></div>
<div class="box green"></div>
<div class="box red"></div>
<div class="box orange"></div>
<div class="box teal"></div>
</div>
</body>

我想我的问题是为什么会这样,为什么在我添加 .green {width: 20%; 之后它才按预期工作?顺序:6; background-color: green;} 在我的 css 末尾?

最佳答案

您的标记按预期工作,(并且它仅在您设置 order:6 后按预期工作,因为在此之前该元素的默认值为 order:0)

记住 order :

By default, flex items are laid out in the source order. However, the order property controls the order in which they appear in the flex container.

但是,您可以简化代码中 order 的使用,只需在您正在使用的相同元素中使用 order 4,5 和 6。

body {
margin: 0
}

.container {
display: flex;
flex-wrap: wrap;
}

.box {
height: 100px;
}

.lime {
background-color: lime;
}

.red {
background-color: red;
}

.orange {
background-color: orange;
}

.teal {
background-color: teal;
}

.light_blue {
order: 4;
background-color: lightblue;
}

.dark_blue {
flex: 0 60%;
order: 5;
background-color: darkblue;
}

.green {
order: 6;
background-color: green;
}

.fifthish {
flex: 0 20%
}

.half {
flex: 0 50%
}

.full {
flex: 0 100%
}
<div class="container">
<div class="box lime full"></div>
<div class="box dark_blue"></div>
<div class="box light_blue fifthish"></div>
<div class="box green fifthish"></div>
<div class="box red half"></div>
<div class="box orange half"></div>
<div class="box teal full"></div>
</div>

关于html - 使用 "order"属性在底部开始 div 并随着我添加更多而逐渐上升,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44346315/

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