【发布时间】:2018-01-05 17:37:40
【问题描述】:
我有这个基本的 HTML 结构:
<div class="section-container">
<div class="section-expander">
<div class="section-content">
<div class="some-irrelevant-stuff"></div>
<div class="section-children">
<div class="section-container">
<div class="section-expander">
<div class="section-content">
<div class="some-irrelevant-stuff"></div>
<div class="section-children">
<div class="section-container">
<div class="section-expander">
<div class="section-content">
<div class="some-irrelevant-stuff"></div>
<div class="section-children">
...
</div> <!-- Closing 3rd level section-children -->
</div> <!-- Closing 3rd level section-content -->
</div> <!-- Closing 3rd level section-expander -->
</div> <!-- Closing 3rd level section-container -->
</div> <!-- Closing 2nd level section-children -->
</div> <!-- Closing 2nd level section-content -->
</div> <!-- Closing 2nd level section-expander -->
</div> <!-- Closing 2nd level section-container -->
</div> <!-- Closing 1st level section-children -->
</div> <!-- Closing 1st level section-content -->
</div> <!-- Closing 1st level section-expander -->
</div> <!-- Closing 1st level section-container -->
也许唯一不能自我解释的是 .section-expander 是左侧的彩色线条。
所以,我的问题是,是否有一种方法只使用 LESS,而不添加新类或使用 JS:
1) 更改左侧 .section-expander 的背景颜色,具体取决于它们在 DOM 中的嵌套级别
2) 循环 .section-content 的背景色,同样取决于嵌套级别,正如您在屏幕截图中看到的那样(我循环浅灰色和白色)。
我能想到的最简单的方法是:
.section-container {
.section-expander { background-color: green; }
.section-content {
background-color: lightgray;
.section-expander { background-color: blue; }
.section-content {
background-color: white;
.section-expander { background-color: red; }
.section-content {
background-color: lightgray;
...
}
}
}
}
我觉得这样做不太聪明。我想我应该使用某种 LESS 循环,但经过数小时的尝试,我真的无法想出按预期工作的东西。有人可以帮我想出更好的办法吗?
【问题讨论】:
-
@Phiter 有什么问题?
-
你应该找到一种使用 nth-of-type 的方法。
标签: html css nested less cycle