Calculate the number of elements with CSS

Asked

Viewed 619 times

9

I’m making a numbered list of regressive counters using the counter-reset. Currently I put the number of items in the list manually:

.faq { counter-reset:my-counter 2; }
.faq dt { counter-increment: my-counter -1; }
.faq dt:before { content: counter(my-counter, decimal-leading-zero) "."; }

With HTML:

<dl class="faq">    
    <dt>Item 1</dt>
    <dt>Item 2</dt>
</dl>

Prints:

01. Item 1
00. Item 2

But if you add one <dt> and not adjust the counter-reset, will print:

01. Item 1
00. Item 2
-01. Item 3

Jsfiddle.

Li Can CSS Detect the number of Children an element has? and tried to counter-reset:my-counter ~ dt, but I didn’t really understand the use of ~.

I know what I can do with jQuery

var reset = 'my-counter '+ $('.faq dt').length;
$('.faq').css( 'counter-reset', reset );

It is possible with pure CSS?

Reference: Numbering In Style | CSS-Tricks

  • 1

    That Soen’s answer works by seeing if the umpteenth element li exists and applying the rule both to him and to siblings his. Unfortunately I think there is no way to get a number just using css, you will have to use Magic number even (maybe using pre-processors of css role, but then I can’t say)

  • Checking the documentation on MDN I noticed that CSS Counter has three properties, these being: -counter - counter-increment - counter-reset in this case what can be done is to display using CSS the list in reverse order.

  • Correcting, resetting to the desired integer. In this case using CSS you can display the list in the reverse order to which it is in the DOM, thus achieving the desired effect.

  • Wow, ooredpurple, if you can confirm your theory and publish an answer, fantastic.

  • I did some digging and I don’t think I can. I suspect that CSS has no way of knowing this, it increments the counter when it finds each element, so it only knows the total after going through all.

  • Worse than in a <ol> would be extremely simple (except for IE maybe): http://www.impressivewebs.com/reverse-ordered-lists-html5/

  • I think what the purple ooredo meant was something similar to this here: Use css to invert the list in the race

Show 2 more comments

1 answer

4


There is a way to do using CSS transformations (transform), as I discovered in the Stackoverflow (english), with a few cons:

  • is not compatible with all browsers: Can I use CSS Transforms

    inserir a descrição da imagem aqui

  • I had to add some Markup to its original, so I think I broke the semantics a little... some divs, around each group dt+dd.

  • text selection, is in trouble, because the browser thinks that what is below, comes first in the selection flow

    inserir a descrição da imagem aqui

The solution

.faq {
  -webkit-transform: scaleY(-1);
  transform: scaleY(-1);
}
.faq > * {
  -webkit-transform: scaleY(-1);
  transform: scaleY(-1);
}
.faq {
  counter-reset: my-counter 0;
}
.faq dd {
  margin: 0 0 50px;
}
.faq dt:before {
  content: counter(my-counter, decimal-leading-zero)".";
  font: bold 50px/1 Sans-Serif;
  left: 0;
  position: absolute;
  top: 5px;
}
.faq dt,
.faq dd {
  padding-left: 90px;
}
.faq dt {
  counter-increment: my-counter 1;
  font: bold 16px Georgia;
  padding: 4px 0 10px 90px;
  position: relative;
}
body {
  padding: 20px;
  background-color: #eee;
}
<dl class="faq">
  <div>
    <dt>Como colocar o umbigo pra dentro?</dt>
    <dd>Aperte o umbago com o dedão. Fonte: Wikipedia.</dd>
  </div>
  <div>
    <dt>Onde eu baixo um processador dual core?</dt>
    <dd>AMD ou IBM?</dd>
  </div>
  <div>
    <dt>Carne vermelha faz mal, carne branca faz mal, o que devo comer?</dt>
    <dd>Carne humana.</dd>
  </div>
  <div>
    <dt>Quando devo falar sobre sexo com meu cachorro?</dt>
    <dd>Quando ele entrar na puberdade.</dd>
  </div>
</dl>

How it works?

The principle is to reverse the vertical coordinate system using CSS transformations:

transform: scaleY(-1)

This will reverse the Y axis, which is traditionally the vertical axis.

So step by step, this is what happens:

  1. If we apply this transformation only once, then what happens is that everything inside the element with the style applied turns upside down. In this case, we first apply the transformation to the element that encompasses the entire list:

    Invertendo o elemento mestre

  2. It turns out now, each of the sub-elements is gourd-down. So what we have to do is reverse each of them individually, which in practice will deflect them:

    Invertendo os sub-elementos

  • For anyone who wants one jsfiddle.

  • 2

    The trick is then to reverse the Y of each div? What exactly the scaleY(-1) is doing? Why does the scale reverse the position? . . . . Follow up on [Philosophy.se]: "Is there any limit to Human Creativity?" :)

  • 2

    Hahaha, nice trick, now I get it. @brasofilo This is an increasing list, but double flips (first the container, then each item), giving the regressive effect.

  • 1

    @bfavaretto, yes, there in the OS has the explanation of the code: mirror in the vertical axis both the container and the childs. The childs follow the inverted y axis of the container, but the children themselves are again head up. I find it curious that it is scale who does the trick.

  • These questions are so wtf for the solution. + 1

  • I added an explanation with some images to facilitate the visualization. The images so rough... I’m on a computer that only has Paint... but you can already have a visual notion.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.