如何将数字与有序列表中的项目样式在一起?

huangapple go评论58阅读模式
英文:

How to style the number together with the item in an ordered list?

问题

当我为有序列表项定义一个.active类时,项目编号没有样式。

li.active {
  background-color: #c5ecbe;
  width: 14em;
  padding: 3px 6px;
  border-radius: 8px;
}

如何同时为项目编号设置样式?

英文:

When I define an .active class for an ordered list item, the item number is not styled.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

li.active {
  background-color: #c5ecbe;
  width: 14em;
  padding: 3px 6px;
  border-radius: 8px;
}

<!-- language: lang-html -->

&lt;ol&gt;
  &lt;li id=&quot;i1&quot; class=&quot;active&quot;&gt;Item 1&lt;/li&gt;
  &lt;li id=&quot;i2&quot;&gt;Item 2&lt;/li&gt;
  &lt;li id=&quot;i3&quot;&gt;Item 3&lt;/li&gt;
&lt;/ol&gt;

<!-- end snippet -->

How to style the number together with the item?

答案1

得分: 0

需要添加一些CSS和CSS计数器。

ol {
  list-style: none;
  counter-reset: item;
}

li {
  counter-increment: item;
  margin-bottom: 5px;
}

li:before {
  content: counter(item) ".";
  width: 15px;
  margin-right: 5px;
  text-align: center;
  display: inline-block;
}

li.active {
  background-color: #212121;
  width: 14em;
  padding: 3px 6px;
  border-radius: 8px;
}
<ol>
  <li id="i1" class="active">Item 1</li>
  <li id="i2">Item 2</li>
  <li id="i3">Item 3</li>
</ol>
英文:

Need to add some css and css counter

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

ol {
  list-style: none;
  counter-reset: item;
}

li {
  counter-increment: item;
  margin-bottom: 5px;
}

li:before {
  content: counter(item) &quot;.&quot;;
  width: 15px;
  margin-right: 5px;
  text-align: center;
  display: inline-block;
}

li.active {
  background-color: #212121;
  width: 14em;
  padding: 3px 6px;
  border-radius: 8px;
}

<!-- language: lang-html -->

&lt;ol&gt;
  &lt;li id=&quot;i1&quot; class=&quot;active&quot;&gt;Item 1&lt;/li&gt;
  &lt;li id=&quot;i2&quot;&gt;Item 2&lt;/li&gt;
  &lt;li id=&quot;i3&quot;&gt;Item 3&lt;/li&gt;
&lt;/ol&gt;

<!-- end snippet -->

答案2

得分: 0

使用 list-style-position: inside

li.active {
  background-color: #212121;
  width: 14em;
  padding: 3px 6px;
  border-radius: 8px;
}

ol {
  list-style-position: inside;
}
<ol>
  <li id="i1" class="active">Item 1</li>
  <li id="i2">Item 2</li>
  <li id="i3">Item 3</li>
</ol>
英文:

Use list-style-position: inside

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

li.active {
  background-color: #212121;
  width: 14em;
  padding: 3px 6px;
  border-radius: 8px;
}

ol {
  list-style-position: inside
}

<!-- language: lang-html -->

&lt;ol&gt;
  &lt;li id=&quot;i1&quot; class=&quot;active&quot;&gt;Item 1&lt;/li&gt;
  &lt;li id=&quot;i2&quot;&gt;Item 2&lt;/li&gt;
  &lt;li id=&quot;i3&quot;&gt;Item 3&lt;/li&gt;
&lt;/ol&gt;

<!-- end snippet -->

答案3

得分: -2

li.active {
  background-color: #212121;
  width: 14em;
  padding: 3px 6px;
  border-radius: 8px;
  font-size: 1.5em;
  color: red;
}
<ol>
  <li id="i1" class="active">Item 1</li>
  <li id="i2">Item 2</li>
  <li id="i3">Item 3</li>
</ol>
英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

li.active {
  background-color: #212121;
  width: 14em;
  padding: 3px 6px;
  border-radius: 8px;
  font-size: 1.5em;
  color: red;
}

<!-- language: lang-html -->

&lt;ol&gt;
  &lt;li id=&quot;i1&quot; class=&quot;active&quot;&gt;Item 1&lt;/li&gt;
  &lt;li id=&quot;i2&quot;&gt;Item 2&lt;/li&gt;
  &lt;li id=&quot;i3&quot;&gt;Item 3&lt;/li&gt;
&lt;/ol&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月19日 00:14:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75494641.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定