在.module.scss文件中有多个相同的类,但只返回一个类名。

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

There are several identical classes in the .module.scss file, but only one classname is returned

问题

如何使用相同的类名进行分离? :S
Next.js 生成了只有一个 column1 结尾的 cell1、cell2 等类...
也许可以解决??: className={styles.left.column1}, className={styles.left.column1.cell1}, className={styles.right.column1}, className={styles.right.column1.cell1}

xy.module.scss

.logo {
  container {
    .left {
      .column1 {
        .cell1 {}
        .cell2 {}
        .cell3 {}
        等等..
      }
      .column2 {
        .cell1 {}
        .cell2 {}
        .cell3 {}
        等等..
      }
      等等..
    }
    .right {
      .column1 {
        .cell1 {}
        .cell2 {}
        .cell3 {}
        等等..
      }
      .column2 {
        .cell1 {}
        .cell2 {}
        .cell3 {}
        等等..
      }
      等等..
    }
  }
}

logo.tsx

import styles from './logo.module.scss';

<div className={styles.logo}>
  <div className={styles.container}>
    <div className={styles.left}>
      <div className={styles.column1}>
        <div className={styles.cell1}></div>
        <div className={styles.cell2}></div>
      </div>
    </div>
    <div className={styles.right}>
      <div className={styles.????????}>
        <div className={styles.???????}></div>
        <div className={styles.???????}></div>
      </div>
    </div>
  </div>
</div>

注意:在 logo.tsx 中,你需要替换 "????????" 部分为正确的类名,以便分离相同的类名。

英文:

how to separate with the same class name? :S
nextjs generated only one cloumn1 end cell1, cell2 class etc..
maybe it could be solved??: className={styles.left.column1}, className={styles.left.column1.cell1}, className={styles.right.column1}, className={styles.right.column1.cell1}

xy.module.scss

logo {
  conatiner {
    .left {
      .column1 {
        .cell1 {}
        .cell2 {}
        .cell3 {}
        etc..
      }
      .column2 {
        .cell1 {}
        .cell2 {}
        .cell3 {}
        etc..
      }
      etc..
    }
    .right {
      .column1 {
        .cell1 {}
        .cell2 {}
        .cell3 {}
        etc..
      }
      .column2 {
        .cell1 {}
        .cell2 {}
        .cell3 {}
        etc..
      }
      etc..
    }
  }
}

logo.tsx

import styles from &#39;./logo.module.scss&#39;


&lt;div className={styles.logo}&gt;
  &lt;div className={styles.container}&gt;
    &lt;div className={styles.left}&gt;
      &lt;div className={styles.column1}&gt;
        &lt;div className={styles.cell1}&gt;&lt;/div&gt;
        &lt;div className={styles.cell2}&gt;&lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div className={styles.right}&gt;
      &lt;div className={styles.????????}&gt;
        &lt;div className={styles.???????}&gt;&lt;/div&gt;
        &lt;div className={styles.???????}&gt;&lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

答案1

得分: 1

看起来你想在相同的类名中的不同上下文中应用不同的样式。为此,你需要像这样重写 logo.module.scss

.left {
  & .column1 {
    & .cell1 {}
    & .cell2 {}
    // ...
  }
}

.right {
  & .column1 {
    & .cell1 {}
    & .cell2 {}
    // ...
  }
}

嵌套本身不会产生任何效果。你需要使用 & 选择器,SCSS 会将其替换为当前选择器,所以上面的代码编译为:

.left {}
.left .column1 {}
.left .column1 .cell1 {}
.left .column1 .cell2 {}
// ...
.right {}
.right .column1 {}
.right .column1 .cell1 {}
.right .column1 .cell2 {}
// ...

因此,在 logo.tsx 中,你可以简单地编写:

<div className={styles.right}>
  <div className={styles.column1}>
    <div className={styles.cell1}></div>
    <div className={styles.cell2}></div>
  </div>
</div>

CSS 后代选择器会起作用。

希望这个回答对你有帮助!

英文:

It looks like you want to apply different styles in different contexts to the same classname. For that, you need to rewrite logo.module.scss like this:

.left {
  &amp; .column1 {
    &amp; .cell1 {}
    &amp; .cell2 {}
…
.right {
  &amp; .column1 {
    &amp; .cell1 {}
    &amp; .cell2 {}
…

Nesting by itself does nothing. You need to use the &amp; selector, which SCSS substitutes with the current selector, so the code above transpiles to:

.left {}
.left .column1 {}
.left .column1 .cell1 {}
.left .column1 .cell2 {}
…
.right {}
.right .column1 {}
.right .column1 .cell1 {}
.right .column1 .cell2 {}
…

So in logo.tsx you can simply write:

    &lt;div className={styles.right}&gt;
      &lt;div className={styles.column1}&gt;
        &lt;div className={styles.cell1}&gt;&lt;/div&gt;
        &lt;div className={styles.cell2}&gt;&lt;/div&gt;

And CSS descendant selector will do its job.

Hopefully this answer is what you need!

huangapple
  • 本文由 发表于 2023年7月24日 19:37:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76754096.html
匿名

发表评论

匿名网友

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

确定