【问题标题】:How to write SCSS for Element => ElementClass => Child Class如何为 Element => Element Class => Child Class 编写 CSS
【发布时间】:2020-06-06 02:54:17
【问题描述】:

您好,我正在使用在我的应用程序中全局使用的子组件。所以现在我只想在这个子组件特定于我的要求时更改它的几个 CSS 属性。我想在这里为 descriptionend 类应用不同的属性。如何使用 SCSS 实现这一点,我们是否有可能在没有重要标签的情况下实现它?

*****HTML*******

<my-parent class="parent"> 
//I have added myflag to identify this has to apply only in case of my scenario
 <global-child [class.myFlag]="myFlag===true">
  <div class="child">
    <div class="description">test</div>
    <div class="end">end</div>
  </div>
</global-child>
</my-parent>

这就是我尝试应用我的 css 的方式,它正在增加高度但不是颜色用于描述 *****SCSS******

    global-child.myflag{
      height: 100px !important
    &.description{
      color: blue !important
    }
}

编辑 1:肯尼的回答看起来不错,但它仍然对我不起作用。我想的原因如下。如果这是正确的,如何在我的场景中实现这一点。

“我在 my-parent.scss 中添加新的 CSS。全局子组件在 global-child component.scss 中有它的样式。我相信我的新 SCSS 代码(它是父级)在 globalchild 之前加载。会这样吗是它没有反映在页面上的原因吗?”

编辑 2: 更新了 HTML 上面和下面的一些变化是我的孩子和父母 css

****全局子css****

.child {
  display: flex;
  flex-direction: row;

  &-description {
    width: 100%;
    color: BLACK;
    position: relative;
    }
  }

****父级css*****

  .parent{
     global-child.myflag {
        height: 100px;

       .description {
          color: blue;
       }
     }
   }

【问题讨论】:

    标签: css sass


    【解决方案1】:

    这会起作用

    global-child.myflag {
       height: 100px;
    
      .description {
          color: blue;
      }
    }
    

    现在何时使用&amp;

    当你在同一个元素上上课时

    如果你有类似的元素

    <global-child class="myflag description">
    

    那么你应该使用&amp; 将属性应用到global-child 元素

    但在您的情况下,.descriptionglobal-child 元素的子元素。

    这样就可以了

    global-child {
       &.myflag {
          // css properties
    
          .description {
               // css properties for `.description` those are child of `global-child.myflag
          }
       }
    
       .description {
               // css properties for `.description` those are child of only `global-child
          }
    
    }
    

    【讨论】:

    • 这看起来不错,但它仍然不适合我。我在 my-parent.scss 中添加新的 CSS。全局子组件的样式在 global-child component.scss 中。我相信我的新 SCSS 代码(它是父代码)在 globalchild 之前加载。这会是它没有反映在页面上的原因吗?如果是,那么在我的情况下我怎么能做到这一点。
    • 首先加载 css 文件应该不会导致任何问题。我们必须以这样一种方式编写代码,即无论哪个文件首先加载或第二个加载,我们的代码都必须工作。现在,谈到您的问题,您能否用您的父 CSS 和子 CSS 更新您的问题。并指定在这种情况下您要应用哪个 css?
    • 感谢您的澄清。在问题中添加了我的 prrent 和 child css。请检查一下。
    • 我发现了问题。问题不在于我应用样式的方式。这是由于范围样式。在角度组件上将 viewEncapsulation 设置为 None 解决了我的问题。
    【解决方案2】:

    Kenny 的回答对于应用 CSS 样式是正确的,但对我来说,问题是由于角度的样式范围。在我的角度组件上将 viewEncapsulation 设置为 NONE 为我解决了这个问题。

    【讨论】:

    • 如果它确实解决了您的问题,那么您应该将其标记为已接受的答案,因为社区会知道,问题已解决,这将对来看这篇文章的用户有所帮助,我会获得代表点:P
    • 标记为已接受。我的问题不在于我如何应用样式。正如我所说,这是由于 viewencapsulation 配置。所以我在答案中更新了它并回答了我的问题。非常感谢您花时间回答我的所有问题。
    猜你喜欢
    • 2014-10-30
    • 2018-08-07
    • 1970-01-01
    • 2015-06-08
    • 2014-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多