使用下面的 HTML 你可以做到这一点(坦率地说,你可以使用其他HTML 做到这一点......)但你应该使用表格。
无论如何,也就是说,我正在使用的 HTML:
<ul>
<li class="head">
<span class="col1">Food</span>
<span class="col2">Calories</span>
</li>
<li>Fruits
<ul>
<li>
<span class="col1">Apple</span>
<span class="col2">90</span>
</li>
<li>
<span class="col1">Grape</span>
<span class="col2">5</span>
</li>
<li>
<span class="col1">strawberry</span>
<span class="col2">16</span>
</li>
</ul></li>
<li>Vegetable
<ul>
<li>
<span class="col1">Cucumber</span>
<span class="col2">12</span>
</li>
<li>
<span class="col1">Onion</span>
<span class="col2">29</span>
</li>
</ul></li>
</ul>
和CSS:
li.head {
font-weight: bold;
}
span.col1,
span.col2 {
display: inline-block;
width: 48%;
}
ul > li > ul > li {
padding-left: 10%;
height: 0;
line-height: 0;
overflow: hidden;
-webkit-transition: all 1s linear;
}
ul > li:hover > ul > li {
height: 2em;
line-height: 2em;
-webkit-transition: all 1s linear;
}
ul > li > ul > li span:first-child::before {
content: '-';
width: 40%;
}
li li:nth-child(odd) span {
background-color: #aaf;
}
JS 小提琴演示。
为了允许键盘导航,并显示嵌套的食物/卡路里值以响应选项卡事件,我对 HTML 进行了一些修改,以用一个元素包装fruits
andvegetable
文本:a
<ul>
<li class="head">
<span class="col1">Food</span>
<span class="col2">Calories</span>
</li>
<li><a href="#">Fruits</a>
<ul>
<li>
<span class="col1">Apple</span>
<span class="col2">90</span>
</li>
<li>
<span class="col1">Grape</span>
<span class="col2">5</span>
</li>
<li>
<span class="col1">strawberry</span>
<span class="col2">16</span>
</li>
</ul></li>
<li><a href="#">Vegetable</a>
<ul>
<li>
<span class="col1">Cucumber</span>
<span class="col2">12</span>
</li>
<li>
<span class="col1">Onion</span>
<span class="col2">29</span>
</li>
</ul></li>
</ul>
使用以下 CSS:
li.head {
font-weight: bold;
}
li a {
color: inherit;
text-decoration: none;
}
span.col1,
span.col2 {
display: inline-block;
width: 48%;
}
ul > li > ul > li {
padding-left: 10%;
height: 0;
line-height: 0;
overflow: hidden;
-moz-transition: all 1s linear;
-ms-transition: all 1s linear;
-o-transition: all 1s linear;
-webkit-transition: all 1s linear;
transition: all 1s linear;
}
ul > li a:focus + ul > li,
ul > li:hover > ul > li {
height: 2em;
line-height: 2em;
-moz-transition: all 1s linear;
-ms-transition: all 1s linear;
-o-transition: all 1s linear;
-webkit-transition: all 1s linear;
transition: all 1s linear;
}
ul > li > ul > li span:first-child::before {
content: '-';
width: 40%;
}
li li:nth-child(odd) span {
background-color: #aaf;
}
JS 小提琴演示。
但是,这两个都假设您要自动隐藏,并根据用户交互显示。如果该假设不正确,则不需要进行转换。
顺便提一下,类似手风琴的table
解决方案:
<table>
<colgroup>
<col class="foods" />
<col class="calories" />
</colgroup>
<thead>
<tr>
<th>Food</th>
<th>Calories</th>
</tr>
</thead>
<tbody>
<tr class="header">
<td colspan="2">Fruits</td>
</tr>
<tr>
<td>Apple</td>
<td>90</td>
</tr>
<tr>
<td>Grape</td>
<td>5</td>
</tr>
<tr>
<td>Strawberry</td>
<td>16</td>
</tr>
</tbody>
<tbody>
<tr class="header">
<td colspan="2">Vegetable</td>
</tr>
<tr>
<td>Cucumber</td>
<td>12</td>
</tr>
<tr>
<td>Onion</td>
<td>29</td>
</tr>
</tbody>
</table>
CSS:
.foods,
.calories {
width: 8em;
}
tbody tr.header {
height: 2em;
line-height: 2em;
}
tbody tr,
tbody tr td {
max-height: 0;
line-height: 0;
overflow: hidden;
-moz-transition: all 1s linear;
-ms-transition: all 1s linear;
-o-transition: all 1s linear;
-webkit-transition: all 1s linear;
transition: all 1s linear;
}
tbody tr td:first-child {
padding-left: 2em;
}
tbody tr.header td {
padding: 0;
}
tbody:hover tr {
height: 2em;
max-height: 2em;
line-height: 2em;
-moz-transition: all 1s linear;
-ms-transition: all 1s linear;
-o-transition: all 1s linear;
-webkit-transition: all 1s linear;
transition: all 1s linear;
}
JS 小提琴演示。
这与之前的假设相同,即您希望通过将鼠标悬停在标题上来控制可见性以显示隐藏的内容。
而且,只是为了踢球,添加键盘导航(with tab),使用元素tabindex
上的属性tr.header
:
<table>
<colgroup>
<col class="foods" />
<col class="calories" />
</colgroup>
<thead>
<tr>
<th>Food</th>
<th>Calories</th>
</tr>
</thead>
<tbody>
<tr class="header" tabindex="1">
<td colspan="2">Fruits</td>
</tr>
<!-- unchanged from the previously-posted table mark-up -->
</tbody>
<tbody>
<tr class="header" tabindex="2">
<td colspan="2">Vegetable</td>
</tr>
<!-- unchanged from the previously-posted table mark-up -->
</tbody>
</table>
和CSS:
/* Other CSS remains the same */
tr.header:focus ~ tr,
tbody:hover tr {
height: 2em;
max-height: 2em;
line-height: 2em;
-moz-transition: all 1s linear;
-ms-transition: all 1s linear;
-o-transition: all 1s linear;
-webkit-transition: all 1s linear;
transition: all 1s linear;
}
tr.header:focus {
outline: none;
font-style: italic;
background-color: #ffa;
}
JS 小提琴演示。