body {
background-color:#DFE1E1;
font-family:sans-serif;
margin-left:30%;
}
/*1st */
h1:nth-child(1) {
color: red;
border:5px solid rebeccapurple;
overflow:hidden;
animation: animate 5s ease-in 1 forwards;
}
/*2nd */
h1:nth-child(2) {
color: red;
border:5px solid blue;
overflow:hidden;
animation: animate 5s ease-in 1 forwards;
}
/*3rd */
h1:nth-child(3) {
color: red;
border:5px solid green;
overflow:hidden;
animation: animate 5s ease-in 1 forwards;
}
@keyframes animate{
0%{
width: 0;
}
100%{
width:250px;
}
}
<h1>A for Apple</h1>
<h1>B for Blue</h1>
<h1>C for Car</h1>
<h1>D for Doctor</h1>
<h1>E for Egg</h1>
<h1>F for Frog</h1>
<h1>G for Girl</h1>
As you can observe I have mostly ended up duplicating my CSS Code in h1:nth-child tags other than the border:5px solid ;
tag ,so can someone show an alternative way to reduce this nth of css code duplicacy .scenario would be like: nth code for A for apple will have a different border color, n+1 code for B for Boy will have some other different color tag so all in all 26 different border color tag
2
Answers
Can’t you just have:
Just use comma
,
for remove CSS duplicates like this:Or if you want simplify it again, you can use
:nth-child(-n+3)
to select the first 3 of the child element like this: