- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个用于按钮样式的混合,我将它应用于按钮样式的 a
标签和实际的 button
标签,但我使用的是 flexbox 垂直居中,这在某些版本的 Webkit 中被破坏了。
例如,我可能会在 Stylus 中这样做,但我需要在 Less 中完成同样的事情:
.container {
btn(width) {
display: flex;
flex-direction: column;
width: width;
height: 50px;
// How can I do this part, somehow, in Less?
../ button^[1..-1] {
display: inline-block;
}
}
.btn {
btn(100px);
}
}
以上是我在 Stylus 中的做法,但我希望找到另一种方法来绕过 Less 限制。我尝试附加到选择器,例如 :not(:not(button))
但这似乎不受浏览器支持。
最佳答案
Less(或任何预处理器)无法知道选择器代表的是什么类型的元素,因此他们无法在没有开发人员特别指示的情况下生成此类规则。此外,Less 不支持定位父选择器的特定部分并对其进行修改。因此,对于有问题的 Stylus 代码没有直接转换。
话虽如此,可以采用一些替代选项来生成类似于 Stylus 代码的输出。
选项 1:(使用 CSS :not
选择器)
我们可以使用 CSS :not
(否定)选择器将 flexbox 样式仅应用于不是 button
元素的 .btn
元素.
The support for :not
is available in latest versions of all browsers and IE supports it from 9+.
.container {
.btn-mixin(@width) {
display: inline-block; /* default display is inline block */
width: @width;
height: 50px;
&:not(button){ /* override for elements that are not buttons */
display: flex;
flex-direction: column;
}
}
.btn {
.btn-mixin(100px);
}
}
这不能用默认显示为 flex 来编写,因为否定选择器可以附加在末尾,而元素类型选择器不能。下面是编译后的 CSS 的演示。
.container .btn {
display: inline-block;
width: 100px;
height: 50px;
}
.container .btn:not(button) {
display: flex;
flex-direction: column;
}
<div class="container">
<a href='#' class='btn'>Hello!</a>
<a href='#' class='btn'>we</a>
<a href='#' class='btn'>are</a>
<a href='#' class='btn'>some</a>
<a href='#' class='btn'>links</a>
</div>
<div class="container">
<button class='btn'>Hello!</button>
<button class='btn'>we</button>
<button class='btn'>are</button>
<button class='btn'>some</button>
<button class='btn'>buttons</button>
</div>
选项 2:(使用较少的参数化混合)
这不是我推荐的选项。在我看来,选项 1 更直接、更简单。我将其放入答案中只是为了表明可以在 Less 中实现条件行为。它使用guards 功能来检查参数 (@elType
) 的值,然后应用样式。
.container {
.btn-mixin(@width; @elType: anchor) {
& when not (@elType = anchor){
display: inline-block;
}
& when (@elType = anchor){
display: flex;
flex-direction: column;
}
width: @width;
height: 50px;
}
.btn {
.btn-mixin(100px);
}
button.btn {
.btn-mixin(200px, button);
}
}
为什么不 :not(:not(button))
工作?
这个选择器不会工作(至少还没有)因为截至目前,negation :not
selector accepts only simple selectors as arguments . :not(button)
(外部否定的参数)不是一个简单的选择器。
关于css - Less mixin 可以对某些元素类型应用特殊规则吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37364186/
乐 mixins具有两个(或更多)性质,在同一个容器中组合多个值,或者值与角色一起。但是,据我所知,没有一种直接的方法可以检查不是由您创建的变量中的“混合性”。 这可能是个伎俩 my $foo = 3
我对 sass 比较陌生,但是当我学习它时,Sass 网站说要开始使用 @use 而不是 import,所以经过大量的反复试验,我终于弄清楚了如何使用它与导入相同。 注意:我使用 Prepros 进行
给定一个看起来像这样的代码片段,我如何编写一个函数来检查给定对象是否实现了某个混合?我尝试使用指针转换,但由于它们具有相同的基础,因此每个结果都是非空的,但我猜测有一个模板化的解决方案,但找不到我可以
我正在使用 Typescript 2.2 的 mixin,并且想使用另一个 mixin 的属性。 文档显示可以将 mixins 限制为仅混合到某些类中...... const WithLocation
我如何创建一个将嵌套的 mixin 属性用作参数的 mixin? 我用下一个例子来解释自己。 我有一个“过渡属性”mixin: .transition-property (@props){ -we
我浏览了language documentation而且 Google Dart 似乎不支持 mixins(接口(interface)中没有方法主体,没有多重继承,没有类似 Ruby 的模块)。我对此
我想编写返回混合的函数/混合。例如我有这个 mixin: @mixin generate-offsets-from-map($class-slug,$type,$from, $to, $step) {
所有 Less 文档和教程都使用 #namespace > .mixin()当它进入命名空间时的语法。但是我发现自己更习惯于 .namespace.mixin()语法,即: .namespace()
我正在努力实现以下目标: class A { def foo() { "foo" } } class B { def bar() { "bar" } } A.mixin B def a = n
出于本问题的目的,将“mixin”视为 https://www.typescriptlang.org/docs/handbook/mixins.html 中所述的函数。 .在这种情况下,mixin 扩
如何在 vue mixins 中组合两个函数? Vue.mixin({ methods: { functionOne: () => { console.log(1)
我需要重写 mixin 添加的一些成员函数来自第三方库。问题是:mixin 立即在多个第 3 方类定义中使用,在定义 mixin 的同一个脚本文件中。我只能在此脚本之前或之后插入自定义代码,而不能在两
我有一些基本的 mixin,它们使用媒体查询应用一些规则 .on-small(@rules) { @media (@minWidthSmall) { @rules(); } } .on-mediu
我尝试安装 npm 包。所有软件包都安装正确。 但是当我尝试使用 npm start 运行应用程序时当时发生以下错误: ERROR in ./node_modules/css-loader?{"sou
这里有两个mixin @mixin parent { .parent & { @content; } } @mixin child($child) { .#{$child} & {
我在另一个 mixins 中有一个 mixins .background(@url: @base-url , @repeat: repeat, @pos1: left, @pos2: center
我有这个: 如您所见,我目前有一个包含按钮样式混合宏的条件,无论如何我可以自动包含一个吗?例如: @mixin button($color) @include button-#{$color} 最
我有以下代码,可以很好地将各种 std::tuples 转发到我的“BaseSensor”主机类的各种 mixin。 #include // std::cout std::endl #include
我按照概述的方式组织我的 sass (scss) 文件 here ... stylesheets/ | |-- modules/ # Common modules | |
所以,这是我的第一个 mixin .3transitions (@value1,@value2,@value3,@duration){ @value: ~"@{value1},@{value2}
我是一名优秀的程序员,十分优秀!