我有一个有序列表,我想跳过特定项目的数字输出。
传统输出:
1. List item
2. List item
3. List item
4. List item
5. List item
期望的输出:
1. List item
2. List item
Skipped list item
3. List item
4. List item
5. List item
这在 CSS 中可以实现吗?我发现了一个我以前不知道的<ol>
“开始”属性,但似乎对我没有帮助。
我有一个有序列表,我想跳过特定项目的数字输出。
传统输出:
1. List item
2. List item
3. List item
4. List item
5. List item
期望的输出:
1. List item
2. List item
Skipped list item
3. List item
4. List item
5. List item
这在 CSS 中可以实现吗?我发现了一个我以前不知道的<ol>
“开始”属性,但似乎对我没有帮助。
另一种选择是使用 CSS3 计数器:demo
HTML
<ul>
<li>One</li>
<li>Two</li>
<li class="skip">Skip</li>
<li>Three</li>
<li>Four</li>
</ul>
CSS
ul {
counter-reset:yourCounter;
}
ul li:not(.skip) {
counter-increment:yourCounter;
list-style:none;
}
ul li:not(.skip):before {
content:counter(yourCounter) ".";
}
ul li.skip:before {
content:"\a0\a0\a0"; /* some white-space... optional */
}
现在很简单。
只需将以下内容添加到类 .skip 中,其中 .skip 用于跳过的列表项:
ol li.skip {
list-style-type:none;
counter-increment:none;
}
在 HTML 中它将是:
<ol>
<li>first list item</li>
<li class="skip">2nd list item</li>
<li>third list item</li>
</ol>
导致:
这是我的解决方案(仅使用 CSS),它允许您从 ol(有序列表)中保持 li(列表项)的正常使用。这意味着所有li
项目都是一致的,并且没有项目与其邻居代表相同的事物。
代码就在下面:
.term-and-condition li {
position: relative;
}
.term-and-condition ol ol {
list-style-type: lower-latin;
}
.term-and-condition h2 {
position: absolute;
top: -2.4em;
left: -1.5em;
}
.term-and-condition h3 {
position: absolute;
top: -2.4em;
left: -1.5em;
}
.term-and-condition h2 + *,
.term-and-condition h3 + * {
margin-top: 4em;
}
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Skip ordered list item numbering</title>
</head>
<body>
<article class="term-and-condition">
<h1>Participants agree to be bound by these Terms and Conditions</h1>
<ol>
<li>
<h2>The Promoter</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Veritatis dolorum consequuntur, qui cupiditate fugit quo ipsa hic, in amet libero magnam consectetur facere, accusantium sed, adipisci aliquam unde! Sit, iure.</p>
</li>
<li><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Veritatis dolorum consequuntur, qui cupiditate fugit quo ipsa hic, in amet libero magnam consectetur facere, accusantium sed, adipisci aliquam unde! Sit, iure.</p></li>
<li>
<h2>Eligibility</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Veritatis dolorum consequuntur, qui cupiditate fugit quo ipsa hic, in amet libero magnam consectetur facere, accusantium sed, adipisci aliquam unde! Sit, iure.</p>
</li>
<li>
<h2>Entry</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Veritatis dolorum consequuntur, qui cupiditate fugit quo ipsa hic, in amet libero magnam consectetur facere, accusantium sed, adipisci aliquam unde! Sit, iure.</p>
</li>
<li><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Veritatis dolorum consequuntur, qui cupiditate fugit quo ipsa hic, in amet libero magnam consectetur facere, accusantium sed, adipisci aliquam unde! Sit, iure.Lorem ipsum dolor sit amet, consectetur adipisicing elit. Veritatis dolorum consequuntur, qui cupiditate fugit quo ipsa hic, in amet libero magnam consectetur facere, accusantium sed, adipisci aliquam unde! Sit, iure.</p></li>
<li><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Veritatis dolorum consequuntur, qui cupiditate fugit quo ipsa hic, in amet libero magnam consectetur facere, accusantium sed, adipisci aliquam unde! Sit, iure.</p> </li>
<li>
<p>Entrants must upload an image and text that:</p>
<ol>
<li><p>Lorem ipsum dolor sit amet, qui cupiditate fugit quo ipsa hic, in amet libero magnam consectetur facere</p></li>
<li><p>consectetur adipisicing elit. Veritatis dolorum consequuntur,</p></li>
<li><p>in amet libero magnam consectetur facere,</p></li>
<li><p>accusantium sed, adipisci aliquam unde! Sit, iure.</p></li>
<li>
<h3>Additional requirements</h3>
<p>All entries must be the participant’s own image or have the relevant permission</p>
</li>
<li><p>The entrant must be the lorem</p></li>
</ol>
</li>
<li>
<h2>Winner Selection</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Suscipit tenetur minus dolores! Natus harum pariatur voluptas, voluptatibus magnam, animi explicabo eos laudantium delectus nobis repellat dicta eveniet, et blanditiis ducimus!</p>
</li>
</ol>
</article>
</body>
</html>
我尝试过的似乎可行的一件事就是添加了一个 font-size: 0。这可以应用于前面提到的“skip”类。
诚然,这个解决方案缺乏一些 HTML 纯度,但非常简单。