简体   繁体   English

SCSS 目标类和伪选择器同时进行

[英]SCSS target classes and pseudo selectors simultaneously

I'm writing a library and I'd like to style all buttons.我正在编写一个库,我想为所有按钮设置样式。

HTML HTML

<div>
  <p>Buttons</p>
  <button>Button</button>
  <button class="r1">Button</button>
</div>

<div>
  <p>File inputs</p>
  <input type="file" />
  <input type="file" class="r1" />
</div>

SCSS SCSS

button,
input[type=file]::file-selector-button {
  background: #81ecec;
  border: 2px solid #00cec9;
  &.r1{
    background: red;
  }
}

This code processes to:此代码处理为:

button.r1,
input[type=file]::file-selector-button.r1 {
  background: red;
}

[This is invalid and does not work] [这是无效的,不起作用]

Is there a mixin or method I can use so that I can place the classes on only the parent selector, without this getting out of hand?是否有我可以使用的 mixin 或方法,以便我可以将类仅放在父选择器上,而不会失控? I intend to have multiple classes ( primary , secondary , large , small ) and I don't want to write :我打算有多个类( primarysecondarylargesmall ),我不想写

button.r1,
input[type=file].r1::file-selector-button{
  ...
}

button.large,
input[type=file].large::file-selector-button{
   ...
}

button.small,
input[type=file].small::file-selector-button{
   ...
}

I can't figure out a good way of targeting the parent input[type="file"]我想不出针对父input[type="file"]

This codepen has the first example in it, and as it isn't valid CSS the background: red doesn't take effect:这个codepen里面有第一个例子,因为它不是有效的 CSS background: red没有生效:

https://codepen.io/EightArmsHQ/pen/VwxwPGM/139933ae274200149b84afdb726478c5?editors=1100 https://codepen.io/EightArmsHQ/pen/VwxwPGM/139933ae274200149b84afdb726478c5?editors=1100

Attempt 1尝试 1

At the moment I am using a mixin like so:目前我正在使用这样的mixin:

@mixin button{
  background: var(--button-primary);
  color: #fff;
  text-decoration: none;
  border: none;
  display: inline-block;
  padding: 4px 8px;
}

@mixin button-r1{
  border-radius: 3px;
}

button,
.button,
input[type="submit"],
input[type="reset"]{
  @include button;
  &.r1{
    @include button-r1;
  }
}

input[type=file]{
  &::file-selector-button{
    @include button;
  }
  &.r1::file-selector-button{
    @include button-r1;
  }
}

The benefit is that I don't need to repeat the same styles over and over, however I feel like there must be a better way of creating a mixin that interpolates a class somehow.好处是我不需要一遍又一遍地重复相同的 styles,但是我觉得必须有更好的方法来创建一个以某种方式插入 class 的 mixin。

Attempt 2尝试 2

Using the classname as an argument works well, however I lose the ability to nest the rules, which is a shame and one of my favourite parts of SCSS.使用类名作为参数效果很好,但是我失去了嵌套规则的能力,这是一种耻辱,也是我最喜欢的 SCSS 部分之一。

@mixin buttonAndFileInputs($classname: "") {
  button#{$classname},
  .button#{$classname},
  input[type="submit"]#{$classname},
  input[type="reset"]#{$classname}, 
  input[type="file"]#{$classname}::file-selector-button {
    @content;
  }
}


@include buttonAndFileInputs {
  background: var(--button-primary);
  color: #fff;
}

@include buttonAndFileInputs(".r1") {
  border-radius: 3px;
}

I'm not 100% clear on what you're trying to do我不是 100% 清楚你想要做什么

But I think if you edit your codepen to this scss但我认为如果你将你的 codepen 编辑到这个 scss

button,
input[type=file] {
  background: #81ecec;
  border: 2px solid #00cec9;
  &.r1{
    background: red;
  }
}

::file-selector-button {
  background: inherit;
  border: inherit;
}

that will get what you're looking for这将得到你正在寻找的东西

Edit to add explanation : This will make the file-selector-button follow the background and border properties of the input[type=file] .编辑以添加说明:这将使file-selector-button遵循input[type=file]backgroundborder属性。
This means that the file-selector-button will match the rest of the input background.这意味着file-selector-button将匹配输入背景的 rest。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM