英文:
Select a href in a list item
问题
I have to select a a href
inside a list and exclude it from specific styling. Could not make it work with nth-child
and tailwind.
<ul>
<li><a href="/" class="">linka</a></li>
<li><a href="/" class="">linkb</a></li>
<li><a href="/" class="">linkc</a></li>
<li><a href="/" class="">linkd</a></li>
<li><a href="/" class="">linke</a></li>
<li><a href="/" class="">linkf</a></li>
</ul>
So I want to apply styling to all links, a to f, but excluding b and d. Again, I have to target the a href
and give it a class. Tailwinds arbitrary variants
seems the way to go.
Came up with this:
ul > li > a {
@apply [&>*:not(:nth-child(-n+1))]:block;
}
But that did not work.
See above. See above.
英文:
I have to select a a href
inside a list and exclude it from specific styling. Could not make it work with nth-child
and tailwind.
<ul>
<li><a href="/" class="">linka</a></li>
<li><a href="/" class="">linkb</a></li>
<li><a href="/" class="">linkc</a></li>
<li><a href="/" class="">linkd</a></li>
<li><a href="/" class="">linke</a></li>
<li><a href="/" class="">linkf</a></li>
</ul>
So I want to apply styling to all links, a to f, but excluding b and d. Again, I have to target the a href
and give it a class. Tailwinds arbitrary variants
seems the way to go.
Came up with this:
ul > li > a {
@apply [&>*:not(:nth-child(-n+1))]:block;
}
But that did not work.
See above. See above.
答案1
得分: 1
I'd suggest, after verifying the functionality in the comments:
li {
padding-block: 0.5rem;
padding-inline: 1rem;
}
a {
display: block;
}
/* here we select all <a> elements that are within
<li> elements that are NOT :nth-child(2) or
:nth-child(4) of among their siblings: */
li:not(:nth-child(2), :nth-child(4)) a {
background-color: hsl(300deg 80% 80% / 0.7);
}
英文:
I'd suggest, after verifying the functionality in the comments:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
li {
padding-block: 0.5rem;
padding-inline: 1rem;
}
a {
display: block;
}
/* here we select all <a> elements that are within
<li> elements that are NOT :nth-child(2) or
:nth-child(4) of among their siblings: */
li:not(:nth-child(2), :nth-child(4)) a {
background-color: hsl(300deg 80% 80% / 0.7);
}
<!-- language: lang-html -->
<ul>
<li><a href="/" class="">linka</a></li>
<li><a href="/" class="">linkb</a></li>
<li><a href="/" class="">linkc</a></li>
<li><a href="/" class="">linkd</a></li>
<li><a href="/" class="">linke</a></li>
<li><a href="/" class="">linkf</a></li>
</ul>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论