HTML順序リスト1.1、1.2(ネストされたカウンターとスコープ)が機能しない


87

ネストされたカウンターとスコープを使用して、順序付きリストを作成します。

ol {
    counter-reset: item;
    padding-left: 10px;
}
li {
    display: block
}
li:before {
    content: counters(item, ".") " ";
    counter-increment: item
}
<ol>
    <li>one</li>
    <li>two</li>
    <ol>
        <li>two.one</li>
        <li>two.two</li>
        <li>two.three</li>
    </ol>
    <li>three</li>
    <ol>
        <li>three.one</li>
        <li>three.two</li>
        <ol>
            <li>three.two.one</li>
            <li>three.two.two</li>
        </ol>
    </ol>
    <li>four</li>
</ol>

私は次の結果を期待しています:

1. one
2. two
  2.1. two.one
  2.2. two.two
  2.3. two.three
3. three
  3.1 three.one
  3.2 three.two
    3.2.1 three.two.one
    3.2.2 three.two.two
4. four

代わりに、これは私が見るものです(間違った番号付け)

1. one
2. two
  2.1. two.one
  2.2. two.two
  2.3. two.three
2.4 three <!-- this is where it goes wrong, when going back to the parent -->
  2.1 three.one
  2.2 three.two
    2.2.1 three.two.one
    2.2.2 three.two.two
2.3 four

私には手がかりがありません、誰かがそれがどこでうまくいかないのかわかりますか?

これがJSFiddleです:http://jsfiddle.net/qGCUk/2/

回答:


102

「CSSの正規化」のチェックを外します-http ://jsfiddle.net/qGCUk/3/で 使用されるCSSリセットは、デフォルトですべてのリストの余白とパディングを0に設定します

更新 http://jsfiddle.net/qGCUk/4/- メインにサブリストを含める必要があります<li>

ol {
  counter-reset: item
}
li {
  display: block
}
li:before {
  content: counters(item, ".") " ";
  counter-increment: item
}
<ol>
  <li>one</li>
  <li>two
    <ol>
      <li>two.one</li>
      <li>two.two</li>
      <li>two.three</li>
    </ol>
  </li>
  <li>three
    <ol>
      <li>three.one</li>
      <li>three.two
        <ol>
          <li>three.two.one</li>
          <li>three.two.two</li>
        </ol>
      </li>
    </ol>
  </li>
  <li>four</li>
</ol>


2
インデックスの後にポイントを付ける方法- 1.>1.1. 1.2. 1.3.などのように?
URL87 2014年

2
ナビゲーションリストなどに影響を与えないように、必ずcssセレクターを修正してください。
オコミケルコ2017年

@Okomikeruko「cssセレクターを修正する」とはどういう意味ですか?私はあなたがほのめかした問題を正確に実行しているので、このトリックは、使用したい番号付きリストだけでなく、HTMLの他のリストにも影響します。:-\気にしないでください:モシェシマントフの答えはそれを修正します。:)
アントレッド

1
@antred要素の属性はidclassセレクターを使用してそれらの要素に固有のcssを定義できます。あなたは毛布を使用している場合はliulolなど、CSSはそれのすべてのインスタンスに影響を与えます。ただし、要素をに設定<ol class="cleared">し、cssセレクターをol.clearedに設定すると、他のol要素に不必要に影響を与えることはありません。
オコミケルコ

64

このスタイルを使用して、ネストされたリストのみを変更します。

ol {
    counter-reset: item;
}

ol > li {
    counter-increment: item;
}

ol ol > li {
    display: block;
}

ol ol > li:before {
    content: counters(item, ".") ". ";
    margin-left: -20px;
}

2
すごい!私のために箱から出して働いた!戸田。
yO_

3
これは、ポイントとコンテンツを挿入する最初のレベルもあるため、最良の回答です。
Martin Eckleben

ネストされたリストに追加font-weight: boldol ol > li:beforeた場合bold、カウンターはありますが、最初のレベルのリストのカウンターは作成されませんboldか?
Sushmit Sagar

14

これをチェックしてください:

http://jsfiddle.net/PTbGc/

問題は修正されたようです。


私に表示されるもの(ChromeおよびMac OS Xの場合)

1. one
2. two
  2.1. two.one
  2.2. two.two
  2.3. two.three
3. three
  3.1 three.one
  3.2 three.two
    3.2.1 three.two.one
    3.2.2 three.two.two
4. four

どうやってやったの?


の代わりに :

<li>Item 1</li>
<li>Item 2</li>
   <ol>
        <li>Subitem 1</li>
        <li>Subitem 2</li>
   </ol>

行う :

<li>Item 1</li>
<li>Item 2
   <ol>
        <li>Subitem 1</li>
        <li>Subitem 2</li>
   </ol>
</li>

7

これは素晴らしい解決策です!いくつかの追加のCSSルールを使用すると、最初の行のインデントがぶら下がっているMSWordのアウトラインリストのようにフォーマットできます。

OL { 
  counter-reset: item; 
}
LI { 
  display: block; 
}
LI:before { 
  content: counters(item, ".") "."; 
  counter-increment: item; 
  padding-right:10px; 
  margin-left:-20px;
}

2
このアプローチの問題は、リスト番号をコピーできないことです。したがって、巨大なリストをコピーすると、数字が
なくなり

1

最近、同様の問題が発生しました。修正は、順序付きリスト内のliアイテムのdisplayプロパティをdisplayブロックではなくlist-itemに設定し、olのdisplayプロパティがlist-itemではないことを確認することです。すなわち

li { display: list-item;}

これにより、htmlパーサーはすべてのliをリスト項目として認識し、それに適切な値を割り当て、olを設定に基づいてインラインブロックまたはブロック要素として認識し、カウント値を割り当てようとしません。それ。


これにより、番号が重複します。
TylerH 2018

0

Mosheのソリューションは素晴らしいですが、リストをの中に入れる必要がある場合は、問題がまだ存在する可能性がありますdiv。(読み取り:ネストされたリストでのCSSカウンターリセット

このスタイルはその問題を防ぐことができます:

ol > li {
    counter-increment: item;
}

ol > li:first-child {
  counter-reset: item;
}

ol ol > li {
    display: block;
}

ol ol > li:before {
    content: counters(item, ".") ". ";
    margin-left: -20px;
}
<ol>
  <li>list not nested in div</li>
</ol>

<hr>

<div>
  <ol>
  <li>nested in div</li>
  <li>two
    <ol>
      <li>two.one</li>
      <li>two.two</li>
      <li>two.three</li>
    </ol>
  </li>
  <li>three
    <ol>
      <li>three.one</li>
      <li>three.two
        <ol>
          <li>three.two.one</li>
          <li>three.two.two</li>
        </ol>
      </li>
    </ol>
  </li>
  <li>four</li>
  </ol>
</div>

カウンターリセットをに設定することもできli:beforeます。


.ネストされたリストの末尾ではなくルートリストが必要な場合はどうなりますか?
Sushmit Sagar

0

私がこれを思いついた他の答えを通過した後nested-counter-list、rootolタグにクラスを適用するだけです:

sassコード:

ol.nested-counter-list {
  counter-reset: item;

  li {
    display: block;

    &::before {
      content: counters(item, ".") ". ";
      counter-increment: item;
      font-weight: bold;
    }
  }

  ol {
    counter-reset: item;

    & > li {
      display: block;

      &::before {
        content: counters(item, ".") " ";
        counter-increment: item;
        font-weight: bold;
      }
    }
  }
}

cssコード

ol.nested-counter-list {
  counter-reset: item;
}
ol.nested-counter-list li {
  display: block;
}
ol.nested-counter-list li::before {
  content: counters(item, ".") ". ";
  counter-increment: item;
  font-weight: bold;
}
ol.nested-counter-list ol {
  counter-reset: item;
}
ol.nested-counter-list ol > li {
  display: block;
}
ol.nested-counter-list ol > li::before {
  content: counters(item, ".") " ";
  counter-increment: item;
  font-weight: bold;
}

ol.nested-counter-list {
  counter-reset: item;
}

ol.nested-counter-list li {
  display: block;
}

ol.nested-counter-list li::before {
  content: counters(item, ".") ". ";
  counter-increment: item;
  font-weight: bold;
}

ol.nested-counter-list ol {
  counter-reset: item;
}

ol.nested-counter-list ol>li {
  display: block;
}

ol.nested-counter-list ol>li::before {
  content: counters(item, ".") " ";
  counter-increment: item;
  font-weight: bold;
}
<ol class="nested-counter-list">
  <li>one</li>
  <li>two
    <ol>
      <li>two.one</li>
      <li>two.two</li>
      <li>two.three</li>
    </ol>
  </li>
  <li>three
    <ol>
      <li>three.one</li>
      <li>three.two
        <ol>
          <li>three.two.one</li>
          <li>three.two.two</li>
        </ol>
      </li>
    </ol>
  </li>
  <li>four</li>
</ol>

また.、ネストされたリストのカウンターの最後に末尾を付ける必要がある場合は、次を使用します。

ol.nested-counter-list {
  counter-reset: item;
}

ol.nested-counter-list li {
  display: block;
}

ol.nested-counter-list li::before {
  content: counters(item, ".") ". ";
  counter-increment: item;
  font-weight: bold;
}

ol.nested-counter-list ol {
  counter-reset: item;
}
<ol class="nested-counter-list">
  <li>one</li>
  <li>two
    <ol>
      <li>two.one</li>
      <li>two.two</li>
      <li>two.three</li>
    </ol>
  </li>
  <li>three
    <ol>
      <li>three.one</li>
      <li>three.two
        <ol>
          <li>three.two.one</li>
          <li>three.two.two</li>
        </ol>
      </li>
    </ol>
  </li>
  <li>four</li>
</ol>

弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.