【发布时间】:2021-09-17 00:38:07
【问题描述】:
我有一个包含项目列表及其描述的 HTML 页面。样式仅在 CSS 中完成,旨在将信息与布局分开。
是否可以只使用 CSS 以便每隔一行反转列?我想避免使用 HTML table,或手动反转 HTML 中的列,我想继续使用 <ul> 和 <li>,因为它与显示的数据类型匹配。
我有什么:
img {
max-width: 50px;
height: auto;
}
ul {
list-style-type: none;
border-collapse: collapse;
display: table;
}
.item-name, .item-details {
display: table-row;
}
.item-pic, .item-text {
display: table-cell;
vertical-align: top;
padding: 10px;
}
.item-name {
font-weight: bold;
}
<html>
<body>
<ul>
<li><span class="item-pic"><img src="green.gif"></span>
<span class="item-text">
<span class="item-name">Green</span>
<span class="item-details">This is a green item</span>
</span>
</li>
<li><span class="item-pic"><img src="red.gif"></span>
<span class="item-text">
<span class="item-name">Red</span>
<span class="item-details">This is a red item</span>
</span>
</li>
<li><span class="item-pic"><img src="blue.gif"></span>
<span class="item-text">
<span class="item-name">Blue</span>
<span class="item-details">This is a blue item</span>
</span>
</li>
<li><span class="item-pic"><img src="yellow.gif"></span>
<span class="item-text">
<span class="item-name">Yellow</span>
<span class="item-details">This is a yellow item</span>
</span>
</li>
</ul>
</body>
</html>
我想要什么:
【问题讨论】:
-
就定位而言,您可以使用 nth-child developer.mozilla.org/en-US/docs/Web/CSS/:nth-child 定位替代行,然后您可以使用 flex 来反转项目的顺序,甚至可以使用 float 我相信
-
为什么首先使用
display: table;而不是正确的工具,例如Flexbox和CSS-Grid,它们也允许使用order-property 或grid-area- 声明。 -
我尝试使用
flex和flex-direction: column-reverse,但我不知道如何将它与display: table结合使用。我没有考虑完全放弃display: table,因为在我的脑海中,布局是一张经典的桌子——但我并没有接受这个想法。文本侧包含多行的要求仍然存在。
标签: html css html-table