javascript - 当它换行到新行时,我可以将 CSS 应用于 flex-item 吗?
问题描述
.wrapper {
border: 5px solid pink;
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.a-fc {
background-color: purple;
width: 300px;
/*height: 100px;*/
}
.b-fc {
background-color: orange;
display: flex;
flex-direction: column;
/*flex-wrap: wrap;*/
flex-basis:70px;
flex-grow:1;
}
.b-fc > * {
flex-grow: 1;
flex-basis: 100px;
}
.b-fc > *:nth-child(1) {
background-color: red;
}
.b-fc > *:nth-child(2) {
background-color: blue;
}
.b-fc > *:nth-child(3) {
background-color: green;
}
<div class="wrapper">
<div class="a-fc">
<div>a1</div>
</div>
<div class="b-fc">
<div>b1</div><div>b2</div><div>b3</div>
</div>
</div>
FC = 弹性容器。FI = 弹性项目。
.b-fc
当原始行上存在的空间低于 70 像素时,我可以将其放置到新行上。
我的任务:我希望b-fc
FI 在没有创建新行/不换行时垂直堆叠。我希望FI 在换b-fc
行时水平对齐。b-fc
当前解决方案
因此,我认为能够在.b-fc
换行时应用 CSS 会更强大。这可能吗?
也许使用 CSS 变量/SASS,我可以不断评估 FC - .a-fc
<= 是否超过 70px。如果为真,则将样式应用于.b-fc
.
想法 2:媒体查询
另一种选择是测试何时生成 row2,使用媒体查询来捕获这一点并将 CSS 应用于.b-fc
媒体查询。
PS 2015 年之前曾在这里提出过类似的问题。也许从那以后出现了新技术。
解决方案
对于这种特殊情况,您可以考虑使用max()
combine with flex-basis
。诀窍是要么拥有0px
(水平项目)或非常大的价值(垂直项目)。
您会注意到这不是通用解决方案,其值取决于您的 html 结构:
395px = 300px (width of a-fx) + 70px (flex-basis of b-fc) + 10px (border of wrapper) + 16px (default body margin) - 1px
.wrapper {
border: 5px solid pink;
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.a-fc {
background-color: purple;
width: 300px;
}
.b-fc {
background-color: orange;
display: flex;
flex-wrap: wrap;
flex-basis: 70px;
flex-grow: 1;
}
.b-fc>* {
flex-grow: 1;
flex-basis: max(0px, (100vw - 395px)*100);
height: 100px;
}
.b-fc>*:nth-child(1) {
background-color: red;
}
.b-fc>*:nth-child(2) {
background-color: blue;
}
.b-fc>*:nth-child(3) {
background-color: green;
}
<div class="wrapper">
<div class="a-fc">
<div>a1</div>
</div>
<div class="b-fc">
<div>b1</div>
<div>b2</div>
<div>b3</div>
</div>
</div>
所以回答你的问题:不,我们不能在包装上应用 CSS(CSS 无法检测包装),但我们总能找到每种情况的解决方法。
类似的问题:
推荐阅读
- javascript - 在 Node.js 中同步调用以解析请求并在处理请求后发送响应
- amazon-web-services - 使用 Cloud Formation 模板创建 AWS Athena View
- angular - 核心、共享和功能模块中应该包含哪些内容?
- c++ - 尝试在循环中动态初始化 3 维向量时出现分段错误
- javascript - 如何将单个键值对对象转换为具有键值对的对象数组?
- php - 验证ajax,如果数据不存在禁用提交按钮
- python - 时期的张量流样本大小
- python - Tkinter 按钮和多线程?
- java - 从具有 TABLE_PER_CLASS 继承的多对多关系中删除时,Hibernate 无法确定集合所有者
- python - Matplotlib plot Pandas df,如果条件在索引值之间填充