Security News
The Risks of Misguided Research in Supply Chain Security
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
css-counter-value
Advanced tools
Convert CSS counter()
values to integers
without JavaScript.
The limiting feature support required to use this utility is timeline-scope
.
css-counter-value
Features:counter()
value into an integer
for use in calc()
.td
up to the element above its table
)0
to 120
supported.$ npm install css-counter-value
Then include /node_modules/css-counter-value/counter-value.css
From html:
<link rel="stylesheet" type="text/css" href="https://unpkg.com/css-counter-value@1/counter-value.css">
@import url(https://unpkg.com/css-counter-value@1/counter-value.css);
Any element on the page can have a maximum of 8
converted counters in its scope, each counter occupying a variable 1
through 8
.
For example, a td
might have its table-row counter
(1) and its table-col counter
(2).
Additionally, the last td
on the table might scope its table-row counter
and table-col counter
up to the parent table, effectively putting two more counters in scope for the rest of the table.
This enables the table (or grid) to know how many rows and columns it has without writing complex selectors to account for every potential count.
In this scenario, there are still 4
additional counters that could be converted in scope of any td
on the table
.
css-counter-value
ExampleGiven this table, or a similarly structured grid, nested lists, or any other DOM tree - note the inline styles managing the counter states.
<table>
<thead>...</thead>
<tbody style="counter-reset: table-row 0;">
<tr style="counter-increment: table-row 1; counter-reset: table-col 0;">
<td style="counter-increment: table-col 1;">
Who am I, really?
</td>
<td style="counter-increment: table-col 1;">
Who am I, really?
</td>
</tr>
<tr style="counter-increment: table-row 1; counter-reset: table-col 0;">
<td style="counter-increment: table-col 1;">
Who am I, really?
</td>
<td style="counter-increment: table-col 1;">
Who am I, really?
</td>
</tr>
</tbody>
</table>
To convert a counter into an integer, add a directive
as a descendant of the elment being counted and css-counter-value
's built in CSS interpreter
will assign the output vars as directed.
By default, the directive
will assign the converted counter to output number 1
(of 8
) on its immediate parent.
It can assign the converted counter to an ancestor up to 5
levels above itself.
<table>
<thead>...</thead>
<tbody style="counter-reset: table-row 0;">
<tr style="counter-increment: table-row 1; counter-reset: table-col 0;">
<td style="counter-increment: table-col 1;">
Who am I, really?
</td>
<td style="counter-increment: table-col 1;">
<output data-counter-value style="--counter: table-col;"></output>
Know Thyself...?
</td>
</tr>
<tr style="counter-increment: table-row 1; counter-reset: table-col 0;">
<td style="counter-increment: table-col 1;">
Who am I, really?
</td>
<td style="counter-increment: table-col 1;">
<output data-counter-value style="--counter: table-col;"></output>
Know Thyself...?
</td>
</tr>
</tbody>
</table>
The two td
elements now hold a CSS var var(--counter-value\\1)
with an integer value of 2
.
We set the --counter
variable to [the name of the counter]
we wish to convert using the style
attribute inline, but this can be done from your CSS if you prefer.
To add the row number, we must choose a variable slot other than 1
. To do this, assign the desired number 1
to 8
to the data-counter-value
attribute like so:
<td style="counter-increment: table-col 1;">
<output data-counter-value style="--counter: table-col;"></output>
<output data-counter-value="2" style="--counter: table-row;"></output>
Know Thyself.
</td>
The two td
elements now hold a CSS var var(--counter-value\\2)
with an integer value of 1
or 2
depending on which row is looking.
If we pass up to the table
counter values for the row
and col
of the last td
in the table
, its styles will then be able to respond to the number of columns and rows it has inside easily.
To do this, we must add directives
with further instruction like so:
<table>
...
<tbody style="counter-reset: table-row 0;">
...
<tr style="counter-increment: table-row 1; counter-reset: table-col 0;">
..
<td style="counter-increment: table-col 1;">
<output data-counter-value style="--counter: table-col;"></output>
<output data-counter-value="2" style="--counter: table-row;"></output>
<output data-counter-value="3:on-nth-parent(4)" style="--counter: table-col;"></output>
<output data-counter-value="4:on-nth-parent(4)" style="--counter: table-row;"></output>
Know Thyself.
</td>
Assuming this is the last td
in the table
, the table
and every element inside of it will have access to var(--counter-value\\3)
which contains the total number of columns in the last row and var(--counter-value\\4)
which contains the total number of rows in the table.
The :on-nth-parent(X)
operator
must be preceeded by a number 1
to 8
and contain a value in its parentheses 1
to 5
.
Note: Even though the table-row
counter itself is stored on the tbody
, the whole tabel
has access to its integer counter value.
Since they're on the table
, every td
can use both of these vars without adding the additional directives
because the --counter-value\\3
and --counter-value\\4
vars are inherited.
For any given scope, each of the 8
available counters can only be set at most once.
If a directive
sets a variable on an element, no other directives
inside that element's scope can re-use that same variable.
If more than one does happen in the same scope, the value will fail to 0
until it is resolved.
Please do reach out if you need help with any of this, have feature requests, want to share what you've created, or wish to learn more.
PropJockey.io | CodePen | DEV Blog | GitHub | Mastodon |
---|---|---|---|---|
FAQs
Convert CSS counter() values to integers without JavaScript
The npm package css-counter-value receives a total of 16 weekly downloads. As such, css-counter-value popularity was classified as not popular.
We found that css-counter-value demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.