Pseudoselector :has在Firefox上无效,但在Chrome和Opera上有效。

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

Pseudoselector :has not work on firefox but works on chrome and Opera

问题

<!--开始代码段:js 隐藏:false 控制台:true Babel:false -->

<!--语言:lang-css -->

    .main:has(article:nth-child(n + 2)) .role * {
    font-size:55px;  
    }


<!--语言:lang-html -->


    <div class="main">
      <article><div class="role"><span>Ruolo1</span> </div> contenuto1 </article> 
       <article><div class="role"><span>Ruolo2 </span> </div> contenuto2 </article> 
      <article><div class="role"><span>Ruolo3 </span> </div> contenuto3 </article> 
      <article><div class="role"><span>Ruolo4 </span> </div> contenuto4 </article> 

    </div>

<!--结束代码段 -->

CSS 在 Firefox 上为什么不起作用?

除第一个元素外,字体大小将为 55 像素
英文:

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

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

.main:has(article:nth-child(n + 2)) .role * {
font-size:55px;  
}

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

&lt;div class=&quot;main&quot;&gt;
  &lt;article&gt;&lt;div class=&quot;role&quot;&gt;&lt;span&gt;Ruolo1&lt;/span&gt; &lt;/div&gt; contenuto1 &lt;/article&gt; 
   &lt;article&gt;&lt;div class=&quot;role&quot;&gt;&lt;span&gt;Ruolo2 &lt;/span&gt; &lt;/div&gt; contenuto2 &lt;/article&gt; 
  &lt;article&gt;&lt;div class=&quot;role&quot;&gt;&lt;span&gt;Ruolo3 &lt;/span&gt; &lt;/div&gt; contenuto3 &lt;/article&gt; 
  &lt;article&gt;&lt;div class=&quot;role&quot;&gt;&lt;span&gt;Ruolo4 &lt;/span&gt; &lt;/div&gt; contenuto4 &lt;/article&gt; 

&lt;/div&gt;

<!-- end snippet -->

Why the CSS not working on firefox ?

The font size will be 55px for all elements except the first

答案1

得分: 1

如今(2023年4月),Firefox默认不支持:has()。

根据MDN的支持表格:
Pseudoselector :has在Firefox上无效,但在Chrome和Opera上有效。

因此,您可以通过设置标志在FF中进行测试,但如果您在FF上有大量用户,您可能希望实施一个解决方法。

这段代码将font-size应用于所有文章,除非只有一篇文章(即文章既是第一个又是最后一个子元素)。

.main article:not(:first-child:last-child) .role * {
  font-size: 55px;
}
<div class="main">
  <article>
    <div class="role"><span>Ruolo1</span></div> contenuto1 </article>
  <article>
    <div class="role"><span>Ruolo2</span></div> contenuto2 </article>
  <article>
    <div class="role"><span>Ruolo3</span></div> contenuto3 </article>
  <article>
    <div class="role"><span>Ruolo4</span></div> contenuto4 </article>
</div>
英文:

As of this moment (April 2023) Firefox does not support :has() by default.

From the support table in MDN:
Pseudoselector :has在Firefox上无效,但在Chrome和Opera上有效。

So you can test it in FF by setting the flag but if you have significant number of users on FF you may want to implement a workaround.

This snippet applies the font-size to all the articles unless there is only one article (i.e. unless the article is both the first and the last child).

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

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

/*.main:has(article:nth-child(n + 2)) .role * {
font-size:55px;  
}*/

.main article:not(:first-child:last-child) .role * {
  font-size: 55px;
}

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

&lt;div class=&quot;main&quot;&gt;
  &lt;article&gt;
    &lt;div class=&quot;role&quot;&gt;&lt;span&gt;Ruolo1&lt;/span&gt; &lt;/div&gt; contenuto1 &lt;/article&gt;
  &lt;article&gt;
    &lt;div class=&quot;role&quot;&gt;&lt;span&gt;Ruolo2 &lt;/span&gt; &lt;/div&gt; contenuto2 &lt;/article&gt;
  &lt;article&gt;
    &lt;div class=&quot;role&quot;&gt;&lt;span&gt;Ruolo3 &lt;/span&gt; &lt;/div&gt; contenuto3 &lt;/article&gt;
  &lt;article&gt;
    &lt;div class=&quot;role&quot;&gt;&lt;span&gt;Ruolo4 &lt;/span&gt; &lt;/div&gt; contenuto4 &lt;/article&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 0

你可以在这里看到,:has() 在 Firefox 上没有支持,几乎所有其他浏览器都支持。
有一些解决办法,比如这个:
https://stackoverflow.com/questions/73936048/how-do-you-enable-has-selector-on-firefox。然而,这是一个兼容性问题,在 Firefox 上不像在其他浏览器中那样有效。

英文:

You can see here that :has() has no support on firefox, it is supported by nearly all the other browsers.
There are some work arounds like this one:
https://stackoverflow.com/questions/73936048/how-do-you-enable-has-selector-on-firefox. However, it is a compatibility issue and it never works as well as in other browsers.

huangapple
  • 本文由 发表于 2023年4月13日 15:55:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76002979.html
匿名

发表评论

匿名网友

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

确定