Created
February 6, 2023 03:43
-
-
Save jonrandy/f59cf070dec675109cbdb23539826487 to your computer and use it in GitHub Desktop.
An nth-child CSS trick
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:nth-child(1) { --nth-child: 1 } | |
:nth-child(2) { --nth-child: 2 } | |
:nth-child(3) { --nth-child: 3 } | |
:nth-child(4) { --nth-child: 4 } | |
:nth-child(5) { --nth-child: 5 } | |
/* ... */ | |
* { | |
background-color: hsl(calc(100 * var(--nth-child)) 100% 40%); | |
} | |
.class { | |
/* We want to start counting at 0 */ | |
--v: var(--nth-last-child) - 1; | |
opacity: 1 - calc(var(--v) * 0.15); | |
transform: | |
translate3d( | |
50%, | |
calc(-5px * var(--v)), | |
calc(-1px * var(--v)) | |
scale(calc(1 - var(--v) * 0.05); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment