sass - 我如何获得任何选择器的特定属性的值?

标签 sass

我有来自第三方库的以下 CSS。

.rce-citem {
    position: relative;
    background: white;
    display: flex;
    flex-direction: row;
    height: 72px;
    cursor: pointer;
    user-select: none;
    max-width: 100%;
    overflow: hidden;
    min-width: 240px;
}

我想在我的 SCSS 文件中使用这个片段中的一些值来使我的 block 的高度与 rce 的项目同步。那么,是否有一个函数可以在我的样式中获取高度属性(或任意值)的值?

如果有一个 get-value 函数,我可以在下一个示例的已编译 SCSS 中获取 grid: auto 72px/auto

@import 'third_party/rce/main.css';
.custom-layout {
    display: grid;
    grid: auto get-value(.rce-citem, height) / auto;
}

也许还有其他一些提取特定值的技术?

最佳答案

“功能” 已在Github 上讨论过并且不会添加到 Sass 的任何进一步版本中。

This is a good feature for CSS to add. Sass isn't going to add it because we can't take the document inheritance into account and so it will confuse more people than it helps.


但是,这里有一个替代方案,使用 maps , 一个 each directive和自定义 function .

$rce-citem: (
  position: relative,
  background: white,
  display: flex,
  flex-direction: row,
  height: 72px,
  cursor: pointer,
  user-select: none,
  max-width: 100%,
  overflow: hidden,
  min-width: 240px,
);

.rce-citem {
  @each $property, $value in $rce-citem {
    #{$property}: $value;
  }
}

@function rce-citem($key) {
  @return map-get($rce-citem, $key);
}


.foo {
  height: rce-citem(height); // 72px
  min-width: rce-citem(min-width); // 240px
}

关于sass - 我如何获得任何选择器的特定属性的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52834095/

相关文章:

html - SCSS - 为什么不允许规则 `&-classA.&-classB`?

html - 从 Bootstrap 类创建自定义 CSS Helper

css - 是否有使用 scss 全面覆盖 ion-view ion-title 字体颜色的方法?

css - 索环主题与组件上的自定义 CSS 混合

css - 我必须使用什么 webpack 加载器来组合媒体查询?

css - SASS - 为什么不包含此 mixin?

css - 如何使用 CSS 预处理器将扩展样式应用于不同类名的子选择器?

html - 如何在CSS中将元素保持在一行

node.js - node-sass-middleware 不使用 Express 渲染

twitter-bootstrap - 在 Bootstrap 4 中,如何根据断点更改内部 $grid-gutter-width?