gpt4 book ai didi

html - Flexbox:当连续包装时,如何在没有额外标记的情况下堆叠两个元素?

转载 作者:行者123 更新时间:2023-11-28 02:29:23 24 4
gpt4 key购买 nike

<分区>

更新:所以我能够使用 Flexbox 来完成这项工作 Codepen然而,正如社区中的一些人指出的那样,注意事项(例如固定高度)证明 CSS 网格是布局的最佳案例。

我正在尝试依靠 Flexbox 换行来实现尽可能多的布局。我一直遇到这样一种情况,我想将两个或更多元素堆叠在一起(类似于 float ),但又不想添加会破坏此处目的的标记。

我在任何地方都找不到这样的例子,也许这是不可能的?

我创建了一个 CodePen展示我如何尝试堆叠两个元素,然后在另一行中堆叠 3 个元素。

body {
display: flex;
flex-wrap: wrap;
}

.full-width {
flex: 100%;
}

.fifty {
flex: 0 0 50%;
}

.one-third {
flex: 0 0 33%;
}

.twnty-five {
flex: 0 0 25%;
}

div.one-third:nth-child(6),
div.one-third:nth-child(7) {
// margin: auto;
flex: 0 0 33%;
max-height: 50px;
height: 45px;
min-height: 45px;
}

div.twnty-five:nth-child(11),
div.twnty-five:nth-child(12),
div.twnty-five:nth-child(13) {
// margin-left:auto;
flex: 0 0 25%;
height: 25px;
min-height: 25px;
}

div {
display: flex;
min-height: 100px;
align-items: center;
justify-content: center;
text-align: center;
border: 1px solid #DFDFDF;
box-sizing: border-box;
}

* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
<div class="full-width">full-width</div>
<div class="fifty">fifty</div>
<div class="fifty">fifty</div>

<div class="one-third">one-third</div>
<div class="one-third">one-third</div>
<div class="one-third">one-third stack top</div>
<div class="one-third">one-third stack bottom</div>

<div class="twnty-five">twnty-five</div>
<div class="twnty-five">twnty-five</div>
<div class="twnty-five">twnty-five</div>
<div class="twnty-five">stack top</div>
<div class="twnty-five">stack middle</div>
<div class="twnty-five">stack bottom</div>

<!--
Copyright (c) 2018 by Ben Racicot (https://codepen.io/BRacicot/pen/bxGrYP)
Fork of an original work by Ben Racicot (https://codepen.io/BRacicot/pen/ZjrZQw)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-->

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