Last active
March 19, 2023 15:53
-
-
Save Polkovn1k/c2c36c02c48b8b86f0ae4edbfbb451f0 to your computer and use it in GitHub Desktop.
Different styles depending on the number of elements (HTML + CSS)
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
<div class="parent"> | |
<div class="child">111</div> | |
<div class="child">222</div> | |
<div class="child">333</div> | |
<div class="child">444</div> | |
<div class="child">555</div> | |
<div class="child">666</div> | |
</div> | |
<style> | |
/* Стиль если 1 элемент */ | |
.child:first-child:nth-last-child(n + 1), | |
.child:first-child:nth-last-child(n + 1) ~ .child { | |
background: orange; | |
} | |
/* Стиль если 2 элемента */ | |
.child:first-child:nth-last-child(n + 2), | |
.child:first-child:nth-last-child(n + 2) ~ .child { | |
background: blue; | |
} | |
/* Стиль если 3 элемента */ | |
.child:first-child:nth-last-child(n + 3), | |
.child:first-child:nth-last-child(n + 3) ~ .child { | |
background: green; | |
} | |
/* Стиль если 4 и больше элементов */ | |
.child:first-child:nth-last-child(n + 4), | |
.child:first-child:nth-last-child(n + 4) ~ .child { | |
background: grey; | |
} | |
</style> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment