如何选择 CSS 中同级元素的 ::before 部分?

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

How can I select the ::before of a sibling element with css?

问题

我已经使用以下HTML创建了一个复选框:

<div class="chkbox">
  <input id="accept-filter" type="checkbox" class="filter-handler" checked="">
  <label for="accept-filter">
    ::before
    <span class="label-txt">我接受条款</span>
  </label>
</div>

我想要样式化::before伪元素,但只有在复选框被选中时才这样做。

我已成功使用:has选择器来实现:

.chkbox:has(.filter-handler:checked) label::before

然而,在Firefox中这不起作用(似乎不支持:has选择器)。

我希望有一种方法可以使用+兄弟选择器来实现,但以下方法似乎不起作用(jsfiddle链接):

.filter-handler:checked + label::before

是否有一种方法可以实现这个效果?

英文:

I have created a checkbox using the following HTML:

&lt;div class=&quot;chkbox&quot;&gt;
  &lt;input id=&quot;accept-filter&quot; type=&quot;checkbox&quot; class=&quot;filter-handler&quot; checked=&quot;&quot;&gt; 
  &lt;label for=&quot;accept-filter&quot;&gt;
   ::before
   &lt;span class=&quot;label-txt&quot;&gt;I accept the terms&lt;/span&gt;
  &lt;/label&gt;
&lt;/div&gt;

I would like to style the ::before pseudo-element, but only when the checkbox is checked.

I have done this successfully using the :has selector:

.chkbox:has(.filter-handler:checked) label::before

However this doesn't work in Firefox (it seems the :has selector isn't supported).

I was hoping there was a way to do this by using the + sibling selector, but the following doesn't seem to work (jsfiddle):

.filter-handler:checked + label::before

Is there a way to do this?

答案1

得分: 2

你所使用的方法是有效的。你可以使用'+'选择器来实现这个功能。

它适用于类名和标签名,如下所示:

.filter-handler:checked + .filter-label::before

或者

.filter-handler:checked + label::before

找到下面的代码:

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

<!-- language: lang-css -->
.filter-handler:checked + .filter-label::before {
  content: 'Selected';
  display: inline-block;
  background-color: blue;
  color: '#fff';
}

<!-- language: lang-html -->
<div class="chkbox">
  <input id="accept-filter" type="checkbox" class="filter-handler" checked="">
  <label for="accept-filter" class="filter-label">
    <span class="label-txt">I accept the terms</span>
  </label>
</div>

<!-- end snippet -->
英文:

The approach you used will work. You can use the '+' selector to do this.

It works with a class name as well as a tag name as follows:

.filter-handler:checked + .filter-label::before

or

.filter-handler:checked + label::before

Find the below code

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

<!-- language: lang-css -->

.filter-handler:checked + .filter-label::before {
  content: &#39;Selected&#39;;
  display: inline-block;
  background-color: blue;
  color: &#39;#fff&#39;;
}

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

&lt;div class=&quot;chkbox&quot;&gt;
  &lt;input id=&quot;accept-filter&quot; type=&quot;checkbox&quot; class=&quot;filter-handler&quot; checked=&quot;&quot;&gt; 
  &lt;label for=&quot;accept-filter&quot; class=&quot;filter-label&quot;&gt;
   &lt;span class=&quot;label-txt&quot;&gt;I accept the terms&lt;/span&gt;
  &lt;/label&gt;
&lt;/div&gt;

<!-- end snippet -->

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

发表评论

匿名网友

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

确定