【发布时间】:2021-11-23 09:01:00
【问题描述】:
我正在进行一项评估,要求我仅使用 css 将内容呈现到带有空白标签的 html 文件。我创建了一个 mixin,它使用 counter-increment 属性对文档中的所有 li 元素进行编号。我试图用 'fizz' 替换每 3 个 li,但我似乎无法创建一个有效的 if 语句。这是我所拥有的:
HTML
<html lang="en">
<head>
<title>FizzBuzz</title>
<link rel="stylesheet" type="text/css" href="styles.css" />
</head>
<body>
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</body>
</html>
SCSS
@mixin render {
@for $i from 1 through 16 {
ul:nth-child(#{$i}) {
:before {
@if $i % 3 == 0 {
content: 'fizz';
counter-increment: number-counter;
} @else {
content: counter(number-counter);
counter-increment: number-counter;
}
}
}
}
}
body {
counter-reset: number-counter 0;
@include render();
}
li {
list-style-type: none;
}
【问题讨论】:
-
其他问题:我假设您的目标是第 n 个
li,ul的孩子。ul:nth-child表示“最后一个ul在其父级中。所以li:nth-child应该会更好。
标签: html sass scss-mixins