如何使用Tailwind为多个子元素设置一个属性

huangapple go评论57阅读模式
英文:

How to set one property for multiple children element using tailwind

问题

我在我的页面上有一个```contenteditable```的div,并且我想让每个子ul和ol元素都有左内边距,我要使用tailwind。

```html
<div class="[&_ol]:pl-4 [&_ul]:pl-4" contenteditable>
  <ol>
    <li>有序列表项</li>
  </ol>

  <ul>
    <li>无序列表项</li>
  </ul>
</div>

是否可能合并选择器,以便将来不必更改每个元素的内边距?类似于class="[&_ol,&_ul]:pl-4"


<details>
<summary>英文:</summary>

I have a ```contenteditable``` div on my page and i want to every child ul and ol element to have a left padding in it using tailwind

<div class="[&_ol]:pl-4 [&_ul]:pl-4" contenteditable>
<ol>
<li>ordered list item</li>
</ol>

<ul>
<li>ordered list item</li>
</ul>
</div>


is it possible to unite selectors so i don&#39;t have to changed padding for every element in the future? something like ```class=&quot;[&amp;_ol,&amp;_ul]:pl-4&quot;```?

</details>


# 答案1
**得分**: 1

多个选择器在任意变体中[明确不受支持](https://github.com/tailwindlabs/tailwindcss/pull/10655),但您可以考虑通过使用[`:is()`伪类](https://developer.mozilla.org/en-US/docs/Web/CSS/:is)来绕过此限制:

```html
<script src="https://cdn.tailwindcss.com"></script>

<div class="[&amp;_:is(ol,ul)]:pl-4" contenteditable>
  <ol>
    <li>有序列表项</li>
  </ol>

  <ul>
    <li>无序列表项</li>
  </ul>
</div>
英文:

Multiple selectors in arbitrary variants is explicitly not supported but you could consider working around this restriction by using the :is() pseudo-class:

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-html -->

&lt;script src=&quot;https://cdn.tailwindcss.com&quot;&gt;&lt;/script&gt;

&lt;div class=&quot;[&amp;_:is(ol,ul)]:pl-4&quot; contenteditable&gt;
  &lt;ol&gt;
    &lt;li&gt;ordered list item&lt;/li&gt;
  &lt;/ol&gt;

  &lt;ul&gt;
    &lt;li&gt;ordered list item&lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月19日 13:37:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76503854.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定