SASS CSS:子级的目标父类
我正在使用SASS,发现有一个不便之处。这是我想做的一个例子:
.message-error {
background-color: red;
p& {
background-color: yellow
}
}
预期的 CSS:
.message-error {
background-color: red;
}
p.message-error {
background-color: yellow ;
}
想法:所有带有 .message-error 的元素都将是红色的,除非它是 p.message-error。这不是现实生活中的情况,只是为了展示一个例子。
SASS 无法编译这个,我什至尝试过字符串连接。有没有一些插件可以做完全相同的事情?
笔记: 我知道我可以在下面放置另一个 CSS 定义,例如:
p.message-error{....}
...,但我想避免这种情况,并为所有 .message-error 定义使用一个位置。
谢谢。
I am using SASS and found an inconvenience. This is an example of what I am trying to do:
.message-error {
background-color: red;
p& {
background-color: yellow
}
}
Expected CSS:
.message-error {
background-color: red;
}
p.message-error {
background-color: yellow ;
}
The idea: all elements with .message-error will be red, except if it is p.message-error. This is not real-life situation, just to show an example.
SASS is not able to compile this, I even tried string concatenation. Is there some plugin that will do exactly the same?
NOTE:
I know I can put another CSS definition like:
p.message-error{....}
...under, but I would like to avoid that and use one place for all .message-error definitions.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
从 Sass 3.4 开始,现在支持这一点。语法如下所示:
请注意
@at-root
指令和 & 符号上的插值语法。未能包含@at-root
指令将导致选择器类似于.message-error p.message-error
而不是p.message-error
代码>.As of Sass 3.4, this is now supported. The syntax looks like this:
Note the
@at-root
directive and the interpolation syntax on the ampersand. Failure to include the@at-root
directive will result in a selector like.message-error p.message-error
rather thanp.message-error
.您可以将当前选择器分配给一个变量,然后在任意深度使用它:
You can assign the current selector to a variable and then use it at any depth:
Natalie Weizenbaum(Sass 的首席设计师和开发人员)表示永远不会支持它:
来源:# 282 – Element.parent 选择器
据我所知,没有可能的解决方法。
Natalie Weizenbaum (the lead designer and developer of Sass) says it will never be supported:
Source: #282 – Element.parent selector
To my knowledge, there is no possible workaround.
最好的做法可能是这样(假设您的 .message-error 类中除了背景颜色之外还有更多内容)。
这种方法不提供紧密的分组,但您可以让它们彼此靠近。
The best thing to do would be probably this (assuming you have a little more in your .message-error class than just background color.
This approach doesn't offer that close grouping, but you can just keep them close to each other.
我遇到了同样的问题,所以我为此做了一个 mixin。
为了使其工作,您还需要一个字符串替换函数(来自 Hugo Giraudel):
它是如何工作的:
SCSS
输出
I had the same problem so I made a mixin for that.
To make it work, you will need a string replacement function as well (from Hugo Giraudel):
How it works:
SCSS
Output
@Zeljko不可能通过SASS做你想做的事。
请参阅 Nex3 评论:https://github.com/nex3/sass/issues/286 #issuecomment-7496412
关键是“&”之前的空格:
而不是:
@Zeljko It is no possible to do what you want via SASS.
See Nex3 comment: https://github.com/nex3/sass/issues/286#issuecomment-7496412
The key is the space before the '&':
instead of:
我认为如果你想让它们按父选择器分组,你可能需要添加一个公共父元素:
当然,
body
可以替换为其他一些公共父元素,例如#Content< /code> 或另一个包含所有错误消息的
div
名称。更新(另一个想法)
如果您利用@for 和 列表 然后看起来这应该可行(我不确定列表是否允许
.
(句点)。应该编译为类似以下内容:
I think if you want to keep them grouped by parent selector, you might need to add a common parent:
Of course,
body
could be replaced with some other common parent, such as#Content
or anotherdiv
name that will contain all the error messages.UPDATE (Another Idea)
If you leverage @for and lists then it seems like this should work (what I don't know for sure is if the list will allow the
.
(period).Should compile to something like:
我做了一个 mixin 来解决这个问题。
Github: https://github.com/imkremen/sass-parent-append
示例: https://codepen.io/imkremen/pen/RMVBvq
用法 (scss):
结果 (css):
I made a mixin that solves this problem.
Github: https://github.com/imkremen/sass-parent-append
Example: https://codepen.io/imkremen/pen/RMVBvq
Usage (scss):
Result (css):
我使用类似的解决方案创建了 package/mixin :)(也许它会对你有所帮助)
https:// github.com/Darex1991/BEM-parent-selector
这样写:
这个 mixin 只会为最后一个父级添加选择器:
有关存储库的更多信息。
I created package/mixin with a similar solution :) (Maybe it will help U)
https://github.com/Darex1991/BEM-parent-selector
so writing:
This mixin will add selector only for the last parent:
More info on the repo.
我以前也遇到过这个问题。 Bootstrap 3 使用父选择器 hack 来处理这个问题。我为了自己的目的稍微调整了它......
wheresrhys 使用上面类似的方法,但有一些 sass 错误。上面的代码允许您将其作为一个块进行管理并在编辑器中折叠它。嵌套变量也使其成为本地变量,因此您可以在需要应用此 hack 的所有实例中重用 $class 。请参阅下面的工作示例...
http://sassmeister.com/gist/318dce458a9eb3991b13
I have ran into this before as well. Bootstrap 3 handles this using a parent selector hack. I've tweaked it slightly for my own purposes...
wheresrhys uses a similar approach above, but with some sass errors. The code above allows you to manage it as one block and collapse it in your editor. Nesting the variable also makes it local so you can reuse $class for all instances where you need to apply this hack. See below for a working sample...
http://sassmeister.com/gist/318dce458a9eb3991b13
当我需要更改中间的某些元素时,我使用这样的 @mixin 函数
一棵萨斯大树的。
第一个参数是父元素、目标,第二个参数是应该具有的类。
SASS
示例,
就像我需要改进强标记中的字体大小一样,当 .txt-target 也有 .txt-strong
HTML
SASS
字体:
https://sass-lang.com/documentation/at-rules/at -root
这里可以看到一个名为 @mixin unify-parent($child) 的函数,如下所示
I use an @mixin function like this, when i need change some element in middle
of a sass big tree.
The first parameters is the parent element, the target, and the second the class that should have.
SASS
Sample,
like i need to improve font size in a strong tag, when .txt-target had .txt-strong too
HTML
SASS
Font:
https://sass-lang.com/documentation/at-rules/at-root
Here you can see a function called @mixin unify-parent($child) that looks like this
这个作弊可能会起作用,
您甚至可以使用
$&
作为变量名称,但我不能 100% 确定它不会抛出错误。SASS 具有内置的作用域,这样就不必担心
$and
的值泄漏到样式表的其余部分This cheat might work
You may even be able to use
$&
as your variable name but I'm not 100% sure it won't throw an error.And SASS has inbuilt scoping, which removes having to worry about the value of
$and
leaking out to the rest of your stylesheet在当前版本:Selective Steve (3.4.14) 中,这现在是可能的,只需要更新一点您的代码:
这仅在您是一层嵌套时才有效,例如,如果您有这样的内容,则它不起作用:
In the Current Release: Selective Steve (3.4.14) this is now possible, just need to update a little bit your code:
this only works if you are one level nested, for instance it does not work if you have something like this: