代码之家  ›  专栏  ›  技术社区  ›  Brandon Durham

是否可以让本地类继承从文件导入的所有类?

  •  11
  • Brandon Durham  · 技术社区  · 7 年前

    假设我有一个这样的CSS文件:

    /* Base styles */
    .content {
        background-color: var(--background);
        color: var(--text);
        font-family: "Helvetica Neue", Helvetica, sans-serif;
        font-size: 16px;
        line-height: 1.5;
        text-rendering: optimizeLegibility;
    }
    
    @media (min-width: 500px) {
        .content {
            font-size: 22px;
        }
    }
    
    /* Headers */
    h2 {
        font-family: "Helvetica Neue", Helvetica, sans-serif;
        font-size: 24px;
        font-weight: 700;
    }
    
    /* Classes */
    .small-caps {
        font-feature-settings: "tnum";
        letter-spacing: 0.05em;
    }
    

    使用Postss,您可以使用其他类属性,如:

    .another-class {
        composes: content from "other-file.css";
    }
    

    将编译为:

    .another-class {
        background-color: var(--background);
        color: var(--text);
        font-family: "Helvetica Neue", Helvetica, sans-serif;
        font-size: 16px;
        line-height: 1.5;
        text-rendering: optimizeLegibility;
    }
    

    是否可以让类继承 全部的 来自给定目标的样式,以便可以编写类似(伪代码)的内容:

    .another-class {
        composes: * from "other-file.css";
    }
    

    当它变成这样的时候?

    /* Base styles */
    .another-class .content {
        background-color: var(--background);
        color: var(--text);
        font-family: "Helvetica Neue", Helvetica, sans-serif;
        font-size: 16px;
        line-height: 1.5;
        text-rendering: optimizeLegibility;
    }
    
    @media (min-width: 500px) {
        .another-class .content {
            font-size: 22px;
        }
    }
    
    /* Headers */
    .another-class h2 {
        font-family: "Helvetica Neue", Helvetica, sans-serif;
        font-size: 24px;
        font-weight: 700;
    }
    
    /* Classes */
    .another-class .small-caps {
        font-feature-settings: "tnum";
        letter-spacing: 0.05em;
    }
    
    2 回复  |  直到 7 年前
        1
  •  4
  •   vicbyte    7 年前

    .elem {
      background: red;
      @import 'test2';
    }
    

    .inner {
      background: blue;
    }
    
    .outer {
      background: green;
    }
    
    @media (max-width: 500px){
      .something {
        color: black;
      }
    }
    

    .elem {
      background: red; }
      .elem .inner {
        background: blue; }
      .elem .outer {
        background: green; }
      @media (max-width: 500px) {
        .elem .something {
          color: black; } }
    
        2
  •  3
  •   Lookaji    7 年前

    .error {
      border: 1px #f00;
      background-color: #fdd;
    }
    .seriousError {
      border-width: 3px;
    }
    

    .error {
      border: 1px #f00;
      background-color: #fdd;
    }
    .seriousError {
      @extend .error;
      border-width: 3px;
    }
    

    SASS documentation

    推荐文章