{"id":7096,"date":"2019-12-29T15:59:12","date_gmt":"2019-12-29T14:59:12","guid":{"rendered":"https:\/\/www.creativejuiz.fr\/blog?p=7096"},"modified":"2019-12-29T16:17:05","modified_gmt":"2019-12-29T15:17:05","slug":"custom-counters-in-pure-css","status":"publish","type":"post","link":"https:\/\/www.creativejuiz.fr\/blog\/en\/tutorials\/custom-counters-in-pure-css","title":{"rendered":"Custom Counters in pure CSS"},"content":{"rendered":"<p>CSS, since its version 2.1, offers a relatively unknown counter system due to an old incompatibility on IE. Some of you will probably defend the following position: CSS is not made for that, you have to use JavaScript. So be it! But know that it exists, and discover how to use it. Small introduction! <!--more--><\/p>\n<p class=\"message\">This blog post is a translation from the original french article <strong>written in 2012<\/strong>. Keep this history in mind \ud83d\ude42<\/p>\n<p>As I told you, the counters exist and are defined since <a href=\"http:\/\/www.w3.org\/TR\/CSS2\/syndata.html#counter\">CSS2.1 by the W3C<\/a>. The arrival of CSS3 allowed to complete the definition a bit and to include it in a module called &#8220;<a href=\"http:\/\/www.w3.org\/TR\/css3-content\/#counters\">Generated and Replaced Content<\/a>&#8220;.<\/p>\n<p>In this article, we will play with three properties and one function:<\/p>\n<ul>\n<li><code>counter-reset<\/code>: this property allows you to initialize a counter (create it). To do this, you need to fill in one or more counter names followed by its initial numerical value.<br \/>\nExample:<br \/>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-css code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-css code-embed-code\">counter-reset: count1 4 count2 0;<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div><\/p>\n<p>Here we&#8217;ve got two initialized counters: <code>count1<\/code> which starts at 4, and <code>count2<\/code> which starts at 0.<\/li>\n<li><code>counter-set<\/code> : this property allows you to redefine the value of a counter, without creating a new one. To do this, fill in one or more existing counter names followed by its new numerical value.<br \/>\nFor example:<br \/>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-css code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-css code-embed-code\">counter-set: count1 0 count2 3;<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div><\/p>\n<p>Here we have two counters redefined: <code>count1<\/code> which restarts at 0, and <code>count2<\/code> which restarts at 3.<\/li>\n<li><code>counter-increment<\/code> : this property allows you to increment a counter. To do this, enter the name of one or more counters, each followed by the number to be incremented. Without number following the counter name, the increment will be +1 by default.<br \/>\nFor example:<br \/>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-css code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-css code-embed-code\">counter-increment: count1 2 count2 4;<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div><\/p>\n<p>Here our two initial counters will be increased by 2 for count1 and 4 for count2.<\/li>\n<li><code>counter()<\/code>: this function associated with the pseudo-element <code>::before<\/code> or <code>::after<\/code> and the <code>content<\/code> property allows you to insert the counter in your page. This generates a dynamic content. It takes one parameter and a second optional parameter. The first one is the counter name, the second one is the <a href=\"http:\/\/www.w3.org\/TR\/css3-content\/#counter\">counter style<\/a>. (same values as <code>list-style-type<\/code>)<br \/>\nFor example:<div class=\"code-embed-wrapper\"> <pre class=\"language-css code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-css code-embed-code\">li::before {<br\/>\tcontent: counter(count1, decimal-leading-zero) &quot; \u2013 &quot;;<br\/>}<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div><\/p>\n<p>In this example, the content of a <code>li<\/code> element will be preceded by a content type of &#8220;04 -&#8220;.<\/li>\n<\/ul>\n<p class=\"note message\">About the style of the counter, it is possible to specify the values &#8220;disc&#8221;, &#8220;circle&#8221;, &#8220;square&#8221; and &#8220;none&#8221;, so it is possible to generate nothing. I haven&#8217;t found any real use case for these values yet.<\/p>\n<p class=\"important message\">As I write these lines, the <code>counter-set<\/code> property is not recognized by any browsers but Firefox.<\/p>\n<h2>Counter CSS D\u00e9mo<\/h2>\n<p>The simplest way to present those CSS Counters is to give you a well-organized document with a few headings at different levels.<br \/>\nHere is the demonstration:<br \/>\n<a class=\"demonstration\" href=\"\/blog\/doc\/css-counters.html\">CSS Counters Demo<\/a><\/p>\n<p>The code used in HTML is relatively simple: make sure you have <code>h2<\/code>, <code>h3<\/code> and <code>h4<\/code> in a block of text, for example. I give you a snippet if you want to test :<\/p>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-markup code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-markup code-embed-code\">&lt;h2&gt;This is the new way of life&lt;\/h2&gt;<br\/>&lt;p&gt;Lorem ipsum dolor sit amet, [\u2026]&lt;\/p&gt;<br\/><br\/>&lt;h3&gt;Lorem ipsum dolor sit amet, consectetur&lt;\/h3&gt;<br\/><br\/>&lt;h4&gt;Adipisicing elit&lt;\/h4&gt;<br\/>&lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipisicing[\u2026]&lt;\/p&gt;<br\/>&lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do [\u2026]&lt;\/p&gt;<br\/><br\/>&lt;h4&gt;Anim id est laborum&lt;\/h4&gt;<br\/>&lt;p&gt;Lorem ipsum [\u2026]&lt;\/p&gt;<br\/><br\/>&lt;h3&gt;Reprehenderit in voluptate&lt;\/h3&gt;<br\/>&lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore[\u2026]&lt;\/p&gt;<br\/><br\/>&lt;h2&gt;Come back later&lt;\/h2&gt;<br\/>&lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore [\u2026]&lt;\/p&gt;<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<p>The CSS is relatively short when it comes to counters, you just have to pay attention to the details: when counters are initiated and incremented.<\/p>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-css code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-css code-embed-code\">\/* we initiate counters named<br\/>   title2, title3 and title4 *\/<br\/>body {<br\/>\tcounter-reset: title2 title3 title4;<br\/>}<br\/><br\/>\/* we increment value of title2<br\/>   when we meet a h2 *\/<br\/>h2 {<br\/>\tcounter-increment: title2 1;<br\/>}<br\/><br\/>\/* then we generate a content before<br\/>   the element composed by the number<br\/>   in roman letter and un dash symbol *\/<br\/>h2::before {<br\/>\tcontent: counter(title2, upper-roman) &quot; \u2013 &quot;;<br\/>}<br\/><br\/>h3 {<br\/>\tcounter-increment: title3 1;<br\/>}<br\/><br\/>h3::before {<br\/>\tcontent: counter(title3, decimal) &quot; \u2013 &quot;;<br\/>}<br\/><br\/>h4 {<br\/>\tcounter-increment: title4 1;<br\/>}<br\/><br\/>\/* here, we compose the counter<br\/>   the number of the  previous<br\/>   title level, and a letter of<br\/>   the current counter. *\/<br\/>h4::before {<br\/>\tcontent: counter(title3, decimal) &quot;.&quot; counter(title4, lower-alpha) &quot; \u2013 &quot;;<br\/>}<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<h3>CSS Counters demo on an odered list<\/h3>\n<p>You&#8217;re going to tell me: What&#8217;s the point since the <code>ol<\/code> list is already numbered?<br \/>\nYes, that&#8217;s true, but the use of the generated content allows to apply richer styles thanks to CSS.<br \/>\nAn example in image (and code):<\/p>\n<p class='codepen'  data-height='365' data-theme-id='0' data-slug-hash='abzyRXX' data-default-tab='css,result' data-animations='run' data-editable='' data-embed-version='2'>\n<\/p>\n\n<p>There is a lot of stuff to know about lists, especially <code>ol<\/code> elements. <a href=\"https:\/\/www.creativejuiz.fr\/blog\/en\/tutorials\/html-start-reversed-type-attribute-ol-list-element\">Discover more about attribute start, reversed and type of counters<\/a>.<\/p>\n<p>Don&#8217;t hesitate if you have any question.<\/p>\n<div class=\"sources\">\n<h2>Useful links and sources<\/h2>\n<ul>\n<li><a href=\"http:\/\/dev.w3.org\/csswg\/css3-lists\/#counter-properties\">CSS Counter properties &#8211; CSSWG<\/a><\/li>\n<li><a href=\"http:\/\/www.w3.org\/wiki\/CSS\/Properties\/counter-increment\">W3C Wiki about CSS Counter<\/a><\/li>\n<\/ul>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>CSS, since its version 2.1, offers a relatively unknown counter system due to an old incompatibility on IE. Some of you will probably defend the following position: CSS is not made for that, you have to use JavaScript. So be it! But know that it exists, and discover how to use it. Small introduction!<\/p>\n","protected":false},"author":4,"featured_media":7102,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_bluesky_dont_syndicate":"","_bluesky_syndication_accounts":"","_bluesky_syndication_text":"","footnotes":""},"categories":[652,647],"tags":[864,807],"coauthors":[597],"class_list":["post-7096","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-css3","category-tutorials","tag-counter-en","tag-css-en"],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.creativejuiz.fr\/blog\/en\/wp-json\/wp\/v2\/posts\/7096","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.creativejuiz.fr\/blog\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.creativejuiz.fr\/blog\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.creativejuiz.fr\/blog\/en\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.creativejuiz.fr\/blog\/en\/wp-json\/wp\/v2\/comments?post=7096"}],"version-history":[{"count":3,"href":"https:\/\/www.creativejuiz.fr\/blog\/en\/wp-json\/wp\/v2\/posts\/7096\/revisions"}],"predecessor-version":[{"id":7107,"href":"https:\/\/www.creativejuiz.fr\/blog\/en\/wp-json\/wp\/v2\/posts\/7096\/revisions\/7107"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.creativejuiz.fr\/blog\/en\/wp-json\/wp\/v2\/media\/7102"}],"wp:attachment":[{"href":"https:\/\/www.creativejuiz.fr\/blog\/en\/wp-json\/wp\/v2\/media?parent=7096"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.creativejuiz.fr\/blog\/en\/wp-json\/wp\/v2\/categories?post=7096"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.creativejuiz.fr\/blog\/en\/wp-json\/wp\/v2\/tags?post=7096"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.creativejuiz.fr\/blog\/en\/wp-json\/wp\/v2\/coauthors?post=7096"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}