Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It seems like you are trying to get the number of items inside your unordered list in CSS (maybe to change their size according to the number of siblings?). </p> <p>Indeed, you cannot use a data-attribute as a Sass variable. However <strong>there is a pure CSS way to apply conditional styles given the number of items in the parent</strong>. Plus, it is very easily written in Sass. </p> <p>Let's say your maximum number of items in your list is 10 and you want to compute the size of li tags based on the number of li tags there is in your list.</p> <pre><code>@for $i from 1 through 10 { li:first-child:nth-last-child(#{$i}), li:first-child:nth-last-child(#{$i}) ~ li { width: (100%/$i); } } </code></pre> <p>This will output the following CSS:</p> <pre><code>li:first-child:nth-last-child(1), li:first-child:nth-last-child(1) ~ li { width: 100%; } li:first-child:nth-last-child(2), li:first-child:nth-last-child(2) ~ li { width: 50%; } li:first-child:nth-last-child(3), li:first-child:nth-last-child(3) ~ li { width: 33.33333%; } li:first-child:nth-last-child(4), li:first-child:nth-last-child(4) ~ li { width: 25%; } li:first-child:nth-last-child(5), li:first-child:nth-last-child(5) ~ li { width: 20%; } li:first-child:nth-last-child(6), li:first-child:nth-last-child(6) ~ li { width: 16.66667%; } li:first-child:nth-last-child(7), li:first-child:nth-last-child(7) ~ li { width: 14.28571%; } li:first-child:nth-last-child(8), li:first-child:nth-last-child(8) ~ li { width: 12.5%; } li:first-child:nth-last-child(9), li:first-child:nth-last-child(9) ~ li { width: 11.11111%; } li:first-child:nth-last-child(10), li:first-child:nth-last-child(10) ~ li { width: 10%; } </code></pre> <p>Basically, this gives the li tags a width of:</p> <ul> <li><code>100.0%</code> when there is only one li tag</li> <li><code>50.00%</code> when there are 2 li tags</li> <li><code>33.33%</code> when there are 3 li tags</li> <li><code>25.00%</code> when there are 4 li tags</li> <li><code>20.00%</code> when there are 5 li tags</li> <li><code>16.66%</code> when there are 6 li tags</li> <li><code>14.28%</code> when there are 7 li tags</li> <li><code>12.50%</code> when there are 8 li tags</li> <li><code>11.11%</code> when there are 9 li tags</li> <li><code>10.00%</code> when there are 10 li tags</li> </ul> <p>For a live example, please refer to <a href="http://codepen.io/HugoGiraudel/pen/c3518482726229adc292de834df4e2c2" rel="nofollow">this demo</a> I did using the same trick. I hope it helps.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload