英文:
Style children based on their data attributes in TailwindCSS
问题
<div>
<p data-a="true"><strong>Text with bold font</strong></p>
<p data-a="false"></p>
<p data-a="false"></p>
<p data-a="false"></p>
</div>
所期望的行为是,当 p
元素的 data-a
为 true 时,字体变为粗体。我尝试将 [&>a]:data-[a="true"]:font-bold
应用于父 div
标签,但它会尝试检测父元素是否具有 data-a,当它为 true 时,将 font-bold 应用于所有子元素。在这里,你如何更改 "操作顺序"?
<details>
<summary>英文:</summary>
I have the following HTML
```html
<div>
<p data-a="true"></p>
<p data-a="false"></p>
<p data-a="false"></p>
<p data-a="false"></p>
</div>
The behaviour I am looking for, is that when the p
element's data-a
is true, a the font becomes bold.
I tried applying [&>a]:data-[a="true"]:font-bold
to the parent div
tag, but it tries to detect if the parent has data-a, and applied font-bold
to all children when it is true. How do I change the "order of operations" here?
答案1
得分: 0
<div>
上使用一个任意变体 [&>[data-a=true]>a]:
,如果你需要的话:
<!-- begin snippet: js hide: false console: false babel: false -->
<!-- language: lang-html -->
<script src="https://cdn.tailwindcss.com"></script>
<div class="[&>[data-a=true]>a]:font-bold">
<p data-a="true">
<a>Foo</a>
<span>Bar</span>
</p>
<p data-a="false">
<a>Foo</a>
<span>Bar</span>
</p>
</div>
<!-- end snippet -->
英文:
Use a single arbitrary variant, [&>[data-a=true]>a]:
if you need it on the <div>
:
<!-- begin snippet: js hide: false console: false babel: false -->
<!-- language: lang-html -->
<script src="https://cdn.tailwindcss.com"></script>
<div class="[&>[data-a=true]>a]:font-bold">
<p data-a="true">
<a>Foo</a>
<span>Bar</span>
</p>
<p data-a="false">
<a>Foo</a>
<span>Bar</span>
</p>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论