html - flex 容器中元素的宽度取决于最后一个元素中的内容
问题描述
我有行容器,其中包含 3 个带有内容的行单元格。第三个单元格可能为空
<div class="row-container">
<div class="row-cell">
<div class="content">
<h1>Some content 1</h1>
</div>
</div>
<div class="row-cell">
<div class="content">
<h1>Some content 2</h1>
</div>
</div>
<div class="row-cell">
<div class="content">
<h1>Some content 3</h1>
</div>
</div>
</div>
这些元素的宽度取决于数量元素。
如果第三个单元格不为空:
.row-cell{
&:nth-child(1){
flex: 1;
}
&:nth-child(2){
flex: 1;
}
&:nth-child(3){
flex: 1;
}
}
如果第三个单元格为空:
.row-cell{
&:nth-child(1){
flex: 1;
}
&:nth-child(2){
flex: 2;
}
}
我应该如何编写样式以使其同时工作?
解决方案
您可以在nth-child(2)
规则中嵌套一个额外的选择器,检查它是否也是最后一个元素。如果是,则增加弹性数。
.row-cell {
&:nth-child(1) {
flex: 1;
}
&:nth-child(2) {
flex: 1;
&:last-child {
flex: 2;
}
}
&:nth-child(3) {
flex: 1;
}
}
推荐阅读
- c# - C# 初学者 - “使用未分配的局部变量”问题
- c# - 如何将绝对路径路径到 System.Net.Mail 附件构造函数而不是使用默认的 .exe 路径
- asp.net-mvc - 如何在 ASP.NET MVC GridView 中添加用于过滤的行?
- multithreading - 将变量写入与后续读取同步
- android-studio - NDK 不包含任何平台
- python - 关于如何使用 ffmpeg/image2pipe 指定 GPX 动画视频长度的建议
- macros - 调整 Common Lisp “pushnew” 以返回成功/失败
- ruby - 如何配置 net-ssh 以便我可以通过 ssh 隧道使用 rest-client 获取数据?
- ansible - 将输出写入本地文件并从 Ansible for Windows 中的控制机获取
- vba - 用于缩进“特殊悬挂”的VBA方法...项目符号点和文本之间的间距