1

I am using CSS2.1 counters to apply numbers to men on the board in the implementation of a board game whose board diagrams use HTML and CSS, by doing something like:

.ply  {counter-increment:main;}
.move:before {content:counter(main);}

With HTML structured as

<ply>
  <move...>
  <ply>
    <move...>
  </ply>
</ply>

All this works fine, but I would like to conditionally style the counter value differently if it's two digits in length (squeeze the two digits together with a negative letter-spacing, for example). Any ideas about how to do this or workarounds?

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • 2
    it is not really answering your question, but what if you just set the negative letter-spacing in your :before css? while the content keeps one digit it won't have any visible effect, but when it comes to two digits they'll look squeezed as you want... – Carlo Moretti Jul 08 '12 at 08:38

4 Answers4

3

It turns out that we can retrieve the value of the counter in question with getComputedStyle, using the second argument which specifies the pseudo-element for which the counter was specified as the content:

value = window.getComputedStyle(elt, ':before').content;

Then we can apply a style such as

if (value>=100) { elt.style.letterSpacing = "-2px"; }

Which is what we want, although it requires traversing all the potentially affected elements with JS whenever they might have changed.

0

You can't do this using only CSS as there is nothing in css selectors about the size of the content.

That could be an anwser but if you want a workaround, it's possible using javascript.

Here's an example (using jquery) in which I change the color of the text when it's more than 2 chars long :

$('.move').each(function(){
    if ($(this).text().length>2) $(this).css({color:'red'});
});​

Demonstration : http://jsfiddle.net/dystroy/nKVvG/

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • No. I just showed how you can change the css based on content length. The css and markups of the question were wrong, I let OP finalize according to his personal (and very localized) needs. – Denys Séguret Jul 08 '12 at 08:38
0

I know it's not really what you wanted, but I made some research and couldn't find any css only way to do this, basically because as stated by @dystroy

"You can't do this using only CSS as there is nothing in css selectors about the size of the content."

So there's no way for the css to know how long is the content in the :before part.

So I guess you should really use jQuery a bit. Try this jsfiddle.

Basically what you can do is iterate over the elements you're indexing with the css counter and then use the .index() attribute to see wheter their counter is double or triple digit. Remember .index() starts from 0 when the counter starts from 1, so in the double digit condition check you should put

if($(this).index() > 8) ...

because counter = index + 1 -> if(counter > 9) = if(index > 8)

If the counter is double digit then you add a class to your movie element so you can freely style it in your css.

Carlo Moretti
  • 2,213
  • 2
  • 27
  • 41
-1

I think what you are looking for is discussed in this post: Selecting and manipulating CSS pseudo-elements such as ::before and ::after using jQuery. I don't think you can do this via plain css, but I may be mistaken.

Community
  • 1
  • 1
DZittersteyn
  • 628
  • 3
  • 8
  • This question is about CSS only. There's no jQuery involved – Rob W Jul 08 '12 at 08:26
  • To cite the OP: "Any ideas about how to do this ***or workarounds?***" – DZittersteyn Jul 08 '12 at 08:38
  • 1
    The OP is looking for a method to add specific styles to elements which match a certain index. The assertion that it is not possible through CSS is not 100% correct. For specific cases, there *is* a solution, e.g. `move + ply > move + ply` (repeat 5x). You did not propose a work-around. The linked question shows how to possibly manipulate pseudo-elements using jQuery, but the most important part of the question has not been answered: "... based on counter value". – Rob W Jul 08 '12 at 08:48