基于元素数量的CSS条件样式化

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

css conditional styling based on number of elements

问题

在我的React.js项目中,有一个块可能会在运行时具有一个元素或两个元素,具体取决于运行时情况。

例如:

情景1:(存在两个组)

<div class='menu-list'>
    <div class='group'>
        <div class="option">....</div>
    </div>
    
    <div class='group'>
        <div class="option">....</div>
    </div>
</div>

情景2:(仅有一个组)

<div class="menu-list">
    <div class='group'>
        <div class="option">....</div>
    </div>
</div>

原本我已经准备好了CSS样式。然而,我注意到在运行时有时会出现情景2,我不想在这种情况下应用任何样式。

我想只在情景1下为元素设置样式。

.menu-list .group:first-child .option:last-child { .....}

这是否可能?

英文:

In my react js project

There is a block which may have one element or two elements, depending on run time.

For example:

Scenario 1: (two groups are existed )

&lt;div class=&#39;menu-list&#39;&gt;
	&lt;div class=&#39;group&#39;&gt;
		&lt;div class=&quot;option&quot;&gt;....&lt;/div&gt;
	&lt;/div&gt;
	
	&lt;div class=&#39;group&#39;&gt;
		&lt;div class=&quot;option&quot;&gt;....&lt;/div&gt;
	&lt;/div&gt;
&lt;/div&gt;

Scenario 2: (only 1 group)

&lt;div class=&quot;menu-list&quot;&gt;
	&lt;div class=&#39;group&#39;&gt;
		&lt;div class=&quot;option&quot;&gt;....&lt;/div&gt;
	&lt;/div&gt;
&lt;/div&gt;

Original I have my css styling ready. However, I noticed there exist scenario 2 sometimes in run time and I dont want to apply any styling in this situation.

I would like to style the element only when it is in scenario 1.

.menu-list .group:first-child .option:last-child { .....}

If it is possible to do so?

答案1

得分: 1

如果您只有这两种情况:当它是一个元素时,应用一种特定的样式,当它是多个元素时,应用另一种样式,那么这对您来说应该是适用的。

.group {
    color: green;
}
.group:first-child:last-child {
    color: red;
}

或者,如果您只需要为第一种情况(多个元素)设置样式:

.group:not(:first-child:last-child) {
    color: green;
}

正如@AmauryHanser所建议的,使用 :only-child 更为正确:

.group:not(:only-child) {
    color: green;
}

以上是您提供的CSS样式代码的翻译。

英文:

If you only have these two scenarios: when it's one element you apply one specific style, when it's multiple elements you apply another style, this should be for you.

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

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

.group {
	color:green;
}
.group:first-child:last-child {
	color:red;
}

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

&lt;div class=&#39;menu-list&#39;&gt;
    &lt;div class=&#39;group&#39;&gt;
        &lt;div class=&quot;option&quot;&gt;If the element is not alone, it&#39;s green&lt;/div&gt;
    &lt;/div&gt;
    
    &lt;div class=&#39;group&#39;&gt;
        &lt;div class=&quot;option&quot;&gt;If the element is not alone, it&#39;s green&lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

---

&lt;div class=&quot;menu-list&quot;&gt;
    &lt;div class=&#39;group&#39;&gt;
        &lt;div class=&quot;option&quot;&gt;If it&#39;s alone it&#39;s red&lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

Or, if you only need to style for the first scenario (multiple elements)

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

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

.group:not(:first-child:last-child) {
color:green;
}

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

&lt;div class=&#39;menu-list&#39;&gt;
    &lt;div class=&#39;group&#39;&gt;
        &lt;div class=&quot;option&quot;&gt;If the element is not alone, it&#39;s green&lt;/div&gt;
    &lt;/div&gt;
    
    &lt;div class=&#39;group&#39;&gt;
        &lt;div class=&quot;option&quot;&gt;If the element is not alone, it&#39;s green&lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

---

&lt;div class=&quot;menu-list&quot;&gt;
    &lt;div class=&#39;group&#39;&gt;
        &lt;div class=&quot;option&quot;&gt;If it&#39;s the only element, I don&#39;t apply the style&lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

As rightly suggested by @AmauryHanser, using :only-child is more correct

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

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

.group:not(:only-child) {
   color:green;
}

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

&lt;div class=&#39;menu-list&#39;&gt;
    &lt;div class=&#39;group&#39;&gt;
        &lt;div class=&quot;option&quot;&gt;If the element is not alone, it&#39;s green&lt;/div&gt;
    &lt;/div&gt;
    
    &lt;div class=&#39;group&#39;&gt;
        &lt;div class=&quot;option&quot;&gt;If the element is not alone, it&#39;s green&lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

---

&lt;div class=&quot;menu-list&quot;&gt;
    &lt;div class=&#39;group&#39;&gt;
        &lt;div class=&quot;option&quot;&gt;If it&#39;s the only element, I don&#39;t apply the style&lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 0

根据答案和调整,

这个逻辑达到了目标。

.group:not(:only-child):first-child .option:last-child{

 颜色:绿色;

}

谢谢大家。

英文:

Based on the answers and adjustment,

this logic gets to the target.

.group:not(:only-child):first-child .option:last-child{

     color:green;
  
}

Thank you all

huangapple
  • 本文由 发表于 2023年5月24日 20:44:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76323684.html
匿名

发表评论

匿名网友

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

确定