作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试根据之前的元素创建具有多个缩进级别的 div。
我看到我们可以用“ul”和“li”创建树,但我必须为我的情况使用 div )=
#container {
border: 2px solid black;
padding: 10px;
}
#container div {
height: 30px;
border: 1px solid black;
}
.title {
background-color: blueviolet;
}
.element {
background-color: greenyellow;
}
[data-id=title-2] {
margin-left: 20px;
}
[data-id=title-3] {
margin-left: 40px;
}
#container [data-id=title-2]~.element {
margin-left: 20px;
}
#container [data-id=title-3]~.element {
margin-left: 40px;
}
<div id="container">
<div class="title" data-id="title-1">Title 1</div>
<div class="title" data-id="title-2">Title 2</div>
<div class="element">Element A</div>
<div class="title" data-id="title-1">Title 1</div>
<div class="title" data-id="title-2">Title 2</div>
<div class="title" data-id="title-3">Title 3</div>
<div class="element">Element B</div>
<div class="title" data-id="title-1">Title 1</div>
<div class="title" data-id="title-2">Title 2</div>
<div class="element">Element C</div>
</div>
我希望我的“元素 C”与我的“title-2”对齐,因为它前面有一个“title-2”。
https://jsfiddle.net/ghncdfpt/
如果你有任何线索可以帮助我,谢谢。
最佳答案
这是一个老掉牙的想法,但如果您知道大约有多少个连续的部分,它可能会奏效。
此示例适用于 4 个连续的部分。注意最后一个是怎么错的:
#container {
border: 2px solid black;
padding: 10px;
}
#container div {
height: 30px;
border: 1px solid black;
}
.title {
background-color: blueviolet;
}
.element {
background-color: greenyellow;
}
[data-id=title-2] {
margin-left: 20px;
}
[data-id=title-3] {
margin-left: 40px;
}
[data-id=title-2]~.element,
[data-id*=title]~.element~[data-id=title-2]~.element,
[data-id*=title]~.element~[data-id*=title]~.element~[data-id=title-2]~.element,
[data-id*=title]~.element~[data-id*=title]~.element~[data-id*=title]~.element~[data-id=title-2]~.element {
margin-left: 20px;
}
[data-id=title-3]~.element,
[data-id*=title]~.element~[data-id=title-3]~.element,
[data-id*=title]~.element~[data-id*=title]~.element~[data-id=title-3]~.element,
[data-id*=title]~.element~[data-id*=title]~.element~[data-id*=title]~.element~[data-id=title-3]~.element {
margin-left: 40px;
}
<div id="container">
<div class="title" data-id="title-1">Title 1</div>
<div class="title" data-id="title-2">Title 2</div>
<div class="element">Element A</div>
<div class="element">Element A</div>
<div class="element">Element A</div>
<div class="title" data-id="title-1">Title 1</div>
<div class="title" data-id="title-2">Title 2</div>
<div class="title" data-id="title-3">Title 3</div>
<div class="element">Element B</div>
<div class="element">Element B</div>
<div class="title" data-id="title-1">Title 1</div>
<div class="title" data-id="title-2">Title 2</div>
<div class="element">Element C</div>
<div class="element">Element C</div>
<div class="element">Element C</div>
<div class="title" data-id="title-1">Title 1</div>
<div class="title" data-id="title-2">Title 2</div>
<div class="title" data-id="title-3">Title 3</div>
<div class="element">Element D</div>
<div class="element">Element D</div>
<div class="element">Element D</div>
<div class="title" data-id="title-1">Title 1</div>
<div class="title" data-id="title-2">Title 2</div>
<div class="element">wrong</div>
<div class="element">wrong</div>
<div class="element">wrong</div>
</div>
我们可以添加更多选择器,最后一部分将起作用:
#container {
border: 2px solid black;
padding: 10px;
}
#container div {
height: 30px;
border: 1px solid black;
}
.title {
background-color: blueviolet;
}
.element {
background-color: greenyellow;
}
[data-id=title-2] {
margin-left: 20px;
}
[data-id=title-3] {
margin-left: 40px;
}
[data-id=title-2]~.element,
[data-id*=title]~.element~[data-id=title-2]~.element,
[data-id*=title]~.element~[data-id*=title]~.element~[data-id=title-2]~.element,
[data-id*=title]~.element~[data-id*=title]~.element~[data-id*=title]~.element~[data-id=title-2]~.element,
[data-id*=title]~.element~[data-id*=title]~.element~[data-id*=title]~.element~[data-id*=title]~.element~[data-id=title-2]~.element {
margin-left: 20px;
}
[data-id=title-3]~.element,
[data-id*=title]~.element~[data-id=title-3]~.element,
[data-id*=title]~.element~[data-id*=title]~.element~[data-id=title-3]~.element,
[data-id*=title]~.element~[data-id*=title]~.element~[data-id*=title]~.element~[data-id=title-3]~.element,
[data-id*=title]~.element~[data-id*=title]~.element~[data-id*=title]~.element~[data-id*=title]~.element~[data-id=title-3]~.element {
margin-left: 40px;
}
<div id="container">
<div class="title" data-id="title-1">Title 1</div>
<div class="title" data-id="title-2">Title 2</div>
<div class="element">Element A</div>
<div class="element">Element A</div>
<div class="element">Element A</div>
<div class="title" data-id="title-1">Title 1</div>
<div class="title" data-id="title-2">Title 2</div>
<div class="title" data-id="title-3">Title 3</div>
<div class="element">Element B</div>
<div class="element">Element B</div>
<div class="title" data-id="title-1">Title 1</div>
<div class="title" data-id="title-2">Title 2</div>
<div class="element">Element C</div>
<div class="element">Element C</div>
<div class="element">Element C</div>
<div class="title" data-id="title-1">Title 1</div>
<div class="title" data-id="title-2">Title 2</div>
<div class="title" data-id="title-3">Title 3</div>
<div class="element">Element D</div>
<div class="element">Element D</div>
<div class="element">Element D</div>
<div class="title" data-id="title-1">Title 1</div>
<div class="title" data-id="title-2">Title 2</div>
<div class="element">correct</div>
<div class="element">correct</div>
<div class="element">correct</div>
</div>
这背后的技巧是我们在上一节的基础上增加了特异性,以确保我们始终应用正确的样式
关于html - 如何用div创建树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57117320/
我是一名优秀的程序员,十分优秀!