重用CSS类,更改一些属性。

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

Reuse CSS class changing some properties

问题

我需要重新使用一个CSS类,但根据条件更改其中的一些属性。

例如,我想将这两个类合并为一个,以避免重复的代码行(在实际情况中有更多重复的属性):

&__logo-small {
    color: $white;
    display: block;
    max-width: 3rem;

    &--link {
        display: flex;
        flex-direction: row;
        height: 3rem;
        width: 3rem;  
    }
}

&__logo-big {
    color: $white;
    display: block;
    max-width: 6rem;

    &--link {
        display: flex;
        flex-direction: row;
        height: 6rem;
        width: 6rem;  
    }
}

我觉得这有点棘手,因为更改发生在不同的级别。如果是单一级别的话,我可以使用继承,但在这种情况下我陷入了困境。

这样的效果是否可行?

编辑:我添加了一小段HTML代码以提供更好的上下文。

我要做的是为两种不同类型的标志提供支持。

旧格式称为'logo',新格式称为'desktopLogo'。它们以不同的方式呈现。

<img *ngIf="logo" class="navbar__logo" [src]="logo" alt="logo">
<a 
  *ngIf="desktopLogo"
  class="navbar__logo"
  (click)="onItemClicked(desktopLogo)"
  [attr.id]="desktopLogo.id"
>
  <img class="navbar__logo--link" [src]="desktopLogo.icon" alt="logo">
</a>

请注意,类navbar__logo--link 仅适用于新版本。

感谢大家的热心回答,我已经成功实现了Pete的建议!

英文:

I need to reuse a css class, but change some of its properties conditionally.

For example, i'd like to merge these two classes into one to avoid having duplicated lines (my real world scenario has many more duplicated properties)

&amp;__logo-small {
    color: $white;
    display: block;
    max-width: 3rem;

    &amp;--link {
        display: flex;
        flex-direction: row;
        height: 3rem;
        width: 3rem;  
    }
}

&amp;__logo-big {
    color: $white;
    display: block;
    max-width: 6rem;

    &amp;--link {
        display: flex;
        flex-direction: row;
        height: 6rem;
        width: 6rem;  
    }
}

I find this tricky because the changes happen in different levels. If it was a sinlge level i could use inheritance, but in this case I'm stuck.

Is something like this achievable?

Edit: I'm adding a snippet of HTML for better context.

What I'm trying to do is provide support to two kind of logos.

The old format is called 'logo', the new format is 'desktopLogo'. They are rendered differently.

&lt;img *ngIf=&quot;logo&quot; class=&quot;navbar__logo&quot; [src]=&quot;logo&quot; alt=&quot;logo&quot;&gt;
&lt;a 
  *ngIf=&quot;desktopLogo&quot;
  class=&quot;navbar__logo&quot;
  (click)=&quot;onItemClicked(desktopLogo)&quot;
  [attr.id]=&quot;desktopLogo.id&quot;
&gt;
  &lt;img class=&quot;navbar__logo--link&quot; [src]=&quot;desktopLogo.icon&quot; alt=&quot;logo&quot;&gt;
&lt;/a&gt;

Notice that the class navbar__logo--link only applies to the new version.

Thanks everyone for your kind answers, I made it work with Pete's suggestion !

答案1

得分: 3

我的建议是:

&amp;__logo {
  颜色:$white;
  显示:块;
  
  &amp;-small {
    最大宽度:3rem;
  }
  
  &amp;-big {
    最大宽度:6rem;
  }
  
  &amp;--link {
    显示:弹性;
    弹性方向:行;    
    
    .logo-small &amp; {
        高度:3rem;
        宽度:3rem;        
    }
    
    .logo-big &amp; {
        高度:6rem;
        宽度:6rem;        
    }
  }
}
英文:

My suggestion is:

&amp;__logo {
  color: $white;
  display: block;
  
  &amp;-small {
    max-width: 3rem;
  }
  
  &amp;-big {
    max-width: 6rem;
  }
  
  &amp;--link {
    display: flex;
    flex-direction: row;    
    
    .logo-small &amp; {
        height: 3rem;
        width: 3rem;        
    }
    
    .logo-big &amp; {
        height: 6rem;
        width: 6rem;        
    }
  }
}

答案2

得分: 3

I think you are using your modifier wrong - the modifier in this case would be small and big.

我认为你在使用修饰符时有误 - 在这种情况下,修饰符应该是small和big。

I would start off with a base class and then just change the bits with the modifiers.

我会从一个基本类开始,然后只需使用修饰符来更改部分内容。

If your html is structured like the above, you may want to only put the modifier on the link, then you can do something like this.

如果你的HTML结构如上所示,你可能只想将修饰符放在链接上,然后你可以这样做。

英文:

I think you are using your modifier wrong - the modifier in this case would be small and big.

I would start off with a base class and then just change the bits with the modifiers

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

&amp;__logo {
  color: $white;
  display: block;
  max-width: 3rem;
  
  &amp;-link {
    display: flex;
    flex-direction: row;
    height: 3rem;
    width: 3rem;
    
    &amp;--big {
      height: 6rem;
      width: 6rem;
    }
  }
  
  &amp;--big {
    max-width: 6rem;
  }
}

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

a normal logo would look like this:
&lt;a href=&quot;class__logo-link&quot;&gt;&lt;img class=&quot;class__logo&quot;&gt;&lt;/a&gt;

a big logo would look like this:
&lt;a href=&quot;class__logo-link class__logo-link--big&quot;&gt;&lt;img class=&quot;class__logo class__logo--big&quot;&gt;&lt;/a&gt;

If your html is structured like the above, you may want to only put the modifier on the link, then you can do something like this

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

&amp;__logo {
  color: $white;
  display: block;
  max-width: 3rem;
  
  &amp;-link {
    display: flex;
    flex-direction: row;
    height: 3rem;
    width: 3rem;
    
    &amp;--big {
      height: 6rem;
      width: 6rem;
      
      .class__logo {
         width: 6rem;
      }
    }
  }
}

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

a normal logo would look like this:
&lt;a href=&quot;class__logo-link&quot;&gt;&lt;img class=&quot;class__logo&quot;&gt;&lt;/a&gt;

a big logo would look like this:
&lt;a href=&quot;class__logo-link class__logo-link--big&quot;&gt;&lt;img class=&quot;class__logo&quot;&gt;&lt;/a&gt;

答案3

得分: -1

只要代码部分,不要翻译。

:root { --clr-light: #FFFFFF; }

.logo:has(.logo-small) { --size-logo: 3rem; }
.logo:has(.logo-big) { --size-logo: 6rem; }

.logo {
    color: var(--clr-light);
    display: block;
    max-width: var(--size-logo);
}

.logo .link {
    display: flex;
    flex-direction: row;
    height: var(--size-logo);
    width: var(--size-logo);
}
英文:

I may be wrong, you may have tried this already, and this may not quite be what your are asking but I hope I am on the right track. To remove some redundancy and increase reusability, you could abstract the similarities between the two classes into one logo class, and then use CSS variables to write the differences elsewhere. I am writing this in normal CSS instead due to personal preferences but feel free to adapt to SCSS:

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

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

:root { --clr-light: #FFFFFF; }

.logo:has(.logo-small) { --size-logo: 3rem; }
.logo:has(.logo-big) { --size-logo: 6rem; }

.logo {
    color: var(--clr-light);
    display: block;
    max-width: var(--size-logo);
}

.logo .link {
    display: flex;
    flex-direction: row;
    height: var(--size-logo);
    width: var(--size-logo);
}

<!-- end snippet -->

A few other tips:

  • Setting heights is generally bad practice (according to Kevin Powell) but understandable here as it is an image!
  • You may want to change the .logo .link to .logo &gt; .link if it is only wanted for a direct child.
  • I believe flex-direction: row is the default so that probably is not needed but I may be wrong!
  • .has() is not supported in some browsers still so may not be the best way to go. You can use .logo-big and .logo-small.
  • This approach might increase the number of classes you may need to add into the HTML but hopefully for the better to aid styling.

I hope this helps!

huangapple
  • 本文由 发表于 2023年3月9日 19:34:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75684047.html
匿名

发表评论

匿名网友

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

确定