简体   繁体   English

如何简单地 CSS class 除了一个属性外具有相同的属性

[英]How to simply CSS class with same properties except one property

I am working on a react web app, which has 2 columns as leftCol and rightCol className.我正在开发一个反应 web 应用程序,它有 2 列作为 leftCol 和 rightCol 类名。 Both class have almost same property with z-index difference. class 两者具有几乎相同的属性,但 z-index 不同。 following is the styling以下是样式

&__rightCol {
​ position : relative;
 ​box-sizing: border-box;
 ​z-index: 0;

 ​&_box {
  ​display : none;
  ​}
}

&__leftCol {
  position : relative;
  box-sizing: border-box;
  z-index: 1;

  &_box {
   display : none;
   }
}

how to simply these both classes, such that we use common class except z-index different如何简化这两个类,以便我们使用常见的 class 除了 z-index 不同

looks like you're using SASS/SCSS?看起来你正在使用 SASS/SCSS?

you can use @mixin你可以使用@mixin

@mixin col {
  position : relative;
  box-sizing: border-box;

  &_box {
    display : none;
  }
}

.rightCol {
  @include col;

  z-index: 0;
}

.leftCol {
  @include col;

  z-index: 1;
}

or if you're using css-in-js framework like styled-components or emotion, you can write something like this:或者,如果您使用 CSS-in-js 框架,例如 styled-components 或 Emotion,您可以编写如下内容:

const colStyles = (zIndex = 0) => css`
  position : relative;
  box-sizing: border-box;
  z-index: ${zIndex};

  &_box {
    display : none;
  }
`;


const StyledDiv = styled.div`
  &__rightCol {
    ${colStyles(0)};
  }

  &__leftCol {
    ${colStyles(1)};
  }
`

Update: Less更新:少

.col {
  position : relative;
  box-sizing: border-box;

  &_box {
    display : none;
  }
}

.rightCol {
  .col();
  z-index: 0;
}

.leftCol {
  .col();
  z-index: 1;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何禁用外部CSS类的属性并在HTML中使用具有相同名称但不同属性的内部CSS类 - How to disable properties of external css class and use internal css class with same name but different property in html CSS如何在div中选择具有特定类的元素,除了第一个类之外的类相同 - CSS how to select element with specific class inside div with same the class except the first one 如何将 class 的一个属性更改为唯一,但使其他属性在 CSS 中相同 - How to change one property of a class to be unique but have others be the same in CSS 一个类中的相同属性 - Same properties in one class 如何删除除一个元素以外的所有元素,并保持相同的CSS路径? - How to remove all elements except for one, maintaing the same CSS path to it? css-div除了单个属性外都一样 - css - divs all the same except single property 如何为除一个类之外的所有元素创建css规则? - How to create a css rule for all elements except one class? 如何从一个 CSS 类中删除特定的 CSS 属性,以便应用另一个在其上放置另一个属性的类? - How to remove specific css property from one CSS class in order to apply another class that puts another property on it? SASS-如何为两个类别(除了一个属性)分配相同的样式? - SASS - How can I assign two classes the same style except one property? 除一个属性外,所有在Sencha Touch 2中工作的CSS列表 - All CSS working in Sencha Touch 2 list except for one property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM