Flexbox:当连续包装时,如何堆叠两个以上的元素而没有额外的标记? [英] Flexbox: when wrapping in a row how can I stack two+ elements without extra markup?

查看:86
本文介绍了Flexbox:当连续包装时,如何堆叠两个以上的元素而没有额外的标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新: 因此,我能够使用Flexbox进行此操作 Codepen 社区指出,警告(例如固定高度)证明CSS网格是布局的最佳案例.

UPDATE: So I was able to make this work with Flexbox Codepen However, as some in the community point out, the caveats (such as fixed heights) prove that CSS grid is the best case for layout.

我试图依靠Flexbox行换行来获得尽可能多的布局.我一直遇到这样的场景:我想将两个或多个元素彼此堆叠(类似于浮动),但又不想添加会在此达到目的的标记.

I'm trying to rely on Flexbox row wrapping for as much layout as possible. I keep running into the scenario where I want to stack two or more elements on top of each other (similar to floating) but do not want to add markup which would defeat the purpose here.

我在任何地方都找不到任何示例,也许不可能?

I cannot find any examples of this anywhere, perhaps its impossible?

我创建了一个 CodePen ,以显示我如何尝试堆叠两个元素然后在另一行中堆叠3个项目.

I've created a CodePen to show how I'm trying to stack two elements and then in another row stack 3 items.

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.
-->

推荐答案

正如其他人所说,CSS Grid Layout可能会更容易,更简洁. Flexbox旨在使其元素沿一个方向流动,并且不像桌子或表格那样起作用.

As others have said, the CSS Grid Layout would probably be easier and cleaner for your intents. Flexbox is designed for its elements to flow in one direction, and does not act like a table or grid.

但是,依靠Flexbox,我重新创建了我可以想到的最佳方法.

But relying on Flexbox, I've recreated the best that I can think of using. Except that it does involve extra markup (can't really achieve all your goals - limitations of Flexbox).

它在div元素上设置了一个额外的类.flexcol,它将用作您要堆叠的元素的包装器.

It sets an extra class .flexcol on a div element, which will act as a wrapper for elements you want stacked.

.flexcol {
  flex-direction: column;
  align-items: stretch;
}

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 {
  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 flexcol">
  <div>one-third stack top</div>
  <div>one-third stack bottom</div>
</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 flexcol">
  <div>stack top</div>
  <div>stack middle</div>
  <div>stack bottom</div>
</div>

这篇关于Flexbox:当连续包装时,如何堆叠两个以上的元素而没有额外的标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆