The My Opera forums have been replaced with forums.opera.com. Please head over there to discuss Opera's products and features

See the new Forums

CSS bug or wrong code in 'counter-increment'

Forums » Opera for Windows/Mac/Linux » Opera browser

You need to be logged in to post in the forums. If you do not have an account, please sign up first.

Go to last post

20. September 2011, 12:54:16

CParallax

Posts: 2

CSS bug or wrong code in 'counter-increment'

Designing a new web page, I found strange behavior of the CSS 'counter-increment' styles.
I have a CSS file with code:
@charset "utf-8";
body ol {
	list-style-type: decimal;
	list-style-position: inside;
	counter-reset:level1;
}
ol ol {
	list-style-type: decimal;
	list-style-position: inside;
	counter-reset:level2;
}
ol ol ol {
	list-style-type: decimal;
	list-style-position: inside;
	counter-reset:level3;
}
ol ol ol ol {
	list-style-type: decimal;
	list-style-position: inside;
	counter-reset:level4;
}
ol ol ol ol ol {
	list-style-type: decimal;
	list-style-position: inside;
	counter-reset:level5;
}
li {
	display:block;
}
ol li:before {
	content: counters(level1, ".") ". ";
	counter-increment:level1;
}
ol ol li:before {
	content: counters(level1, ".") "." counters(level2, ".") ". ";
	counter-increment:level2;
}
ol ol ol li:before {
	content: counters(level1, ".") "." counters(level2, ".") "." counters(level3, ".") ". ";
	counter-increment:level3;
}
ol ol ol ol li:before {
	content: counters(level1, ".") "." counters(level2, ".") "." counters(level3, ".") "." counters(level4, ".") ". ";
	counter-increment:level4;
}
ol ol ol ol ol li:before {
	content: counters(level1, ".") "." counters(level2, ".") "." counters(level3, ".") "." counters(level4, ".") "." counters(level5, ".") ". ";
	counter-increment:level5;
}


and in HTML page I have a code like that:
<ol>
	<li>block 1</li>
	<li>block 2</li>
	<ol><li>subblock 1</li>
	    <li>subblock 2</li>
	    <ol><li>subblock 1</li>
		<li>subblock 2</li>
		<li>subblock 3</li>
		<li>subblock 4</li>
	    </ol>
	    <li>block 3</li>
	    <ol><li>subblock 1</li>
		<li>subblock 2</li>
		<li>subblock 3</li>
	    </ol>
	</ol>
</ol>

when I view this page in Opera, I have a strange result:
1. block 1
2. block 2
2.1. subblock 1
2.2. subblock 2
2.2.1. subblock 1
2.2.2. subblock 2
2.2.3. subblock 3
2.2.4. subblock 3
2.3. block 3
2.3.4.1. subblock 1
2.3.4.2. subblock 2
2.3.4.3. subblock 3


meanwhile in IE I see correct counting:
1. block 1
2. block 2
2.1. subblock 1
2.2. subblock 2
2.2.1. subblock 1
2.2.2. subblock 2
2.2.3. subblock 3
2.2.4. subblock 4
2.3. block 3
2.3.1. subblock 1
2.3.2. subblock 2
2.3.3. subblock 3


Is this Opera bug, or my code is wrong?

21. September 2011, 05:57:00

Your HTML markup is invalid and your CSS is unnecessarily complicated.

An OL element can only have LI elements as immediate children. You cannot have an OL as an immediate child of another OL like in your example; it has to be encapsulated within an LI.

Also, instead of using separate counters for each level, just use one and take advantage of how CSS handles counters on nested elements:

<ol>
  <li>block 1</li>
  <li>block 2
    <ol>
      <li>subblock 1</li>
      <li>subblock 2
        <ol>
          <li>subblock1</li>
          <li>subblock2</li>
          <li>subblock3</li>
          <li>subblock4</li>
        </ol>
      </li>
      <li>subblock 3
        <ol>
          <li>subblock1</li>
          <li>subblock2</li>
          <li>subblock3</li>
        </ol>
      </li>
  </li>
</ol>


ol {
  list-style-type:none;
  margin:0;
  padding:0;
  counter-reset:item;
}

ol li {
  counter-increment:item;
}

ol li:before {
  padding-right:0.5em;
  content:counters(item, ".") ".";
}
Tommy Olsson

21. September 2011, 06:50:28

CParallax

Posts: 2

Thank you for help!
I will be harder learn CSS and HTML.

Forums » Opera for Windows/Mac/Linux » Opera browser