英文:
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)
&__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;
}
}
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.
<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>
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
我的建议是:
&__logo {
颜色:$white;
显示:块;
&-small {
最大宽度:3rem;
}
&-big {
最大宽度:6rem;
}
&--link {
显示:弹性;
弹性方向:行;
.logo-small & {
高度:3rem;
宽度:3rem;
}
.logo-big & {
高度:6rem;
宽度:6rem;
}
}
}
英文:
My suggestion is:
&__logo {
color: $white;
display: block;
&-small {
max-width: 3rem;
}
&-big {
max-width: 6rem;
}
&--link {
display: flex;
flex-direction: row;
.logo-small & {
height: 3rem;
width: 3rem;
}
.logo-big & {
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 -->
&__logo {
color: $white;
display: block;
max-width: 3rem;
&-link {
display: flex;
flex-direction: row;
height: 3rem;
width: 3rem;
&--big {
height: 6rem;
width: 6rem;
}
}
&--big {
max-width: 6rem;
}
}
<!-- language: lang-html -->
a normal logo would look like this:
<a href="class__logo-link"><img class="class__logo"></a>
a big logo would look like this:
<a href="class__logo-link class__logo-link--big"><img class="class__logo class__logo--big"></a>
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 -->
&__logo {
color: $white;
display: block;
max-width: 3rem;
&-link {
display: flex;
flex-direction: row;
height: 3rem;
width: 3rem;
&--big {
height: 6rem;
width: 6rem;
.class__logo {
width: 6rem;
}
}
}
}
<!-- language: lang-html -->
a normal logo would look like this:
<a href="class__logo-link"><img class="class__logo"></a>
a big logo would look like this:
<a href="class__logo-link class__logo-link--big"><img class="class__logo"></a>
答案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 > .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!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论