html - 基于 DOM 状态的条件 CSS 选择器
问题描述
我有以下要设置样式的代码:
<div class="group">
<div class="tabs">
<access type="full">
<span class="tab">Hello</span>
</access>
<span class="tab">World!</span>
</div>
</div>
此 HTML 将根据路由器防护而更改,但在任何给定时刻,我希望每个选项卡20px
之间都有一个边距,并且第一个选项卡的左边距为 0。
我的困惑源于这样一个事实,即 of 的第一个孩子tabs
要么是 type 的组件,access
要么只是一个 normal span
。
所以这就是我想要做的:
/* Set every tab to have 20px spacing in between */
.tab {
margin: 0 0 0 20px;
}
/* Select first child of tabs, whether access component or span and sets margin to 0 */
.tabs > *:nth-child(1) span:first-child,
.tabs span:first-child {
margin: 0
}
我的理由是,对于第二种风格的第一部分,.tabs > *:nth-child(1) span:first-child,
它是:
选择选项卡的第一个子项并选择第一个跨度子项
第二个要阅读:
选择第一个跨度孩子
tabs
我怎样才能做到这一点?
解决方案
在我的方法中,我在每个选项卡的右侧添加边距,这样第一个选项卡没有边距,ml:20px
但所有选项卡.tabs
都被 20px 分隔。
.tabs .tab {
margin-right: 20px;
}
access {
margin: 0;
padding: 0;
}
<div class="group">
<div class="tabs">
<access type="full">
<span class="tab">Hello</span>
</access>
<span class="tab">World!</span>
</div>
</div>
推荐阅读
- angular - 如何在本地构建 formio.js 库
- html - 从父元素访问角度元素
- haskell - 需要解释 Haskell 中的列表理解
- python - 我仍然收到错误“ImgurClientRateLimitError:超出速率限制!” 一个多月后
- apache-kafka-streams - GlobalTable 与 Ktable-Ktable 连接的非键连接?
- python - 如何通过我的所有 csv 文件运行代码?
- php - 带有 PHP 循环的 Google 登录 API
- r - R中多个绘图布局中的图例位置
- android - Recycler View 仅显示第一个视图
- java - 如何在android中发布参数并将数据作为JSONObject返回?