使用 sass,如何设置“#div1 img, #div2 img { height: 80px }”的样式?
使用 sass,你如何设计样式
#box1 img, #anotherbox img { height: 80px }
?使用 mixins 是唯一的方法吗?
Using sass, how do you style
#box1 img, #anotherbox img { height: 80px }
? Is using mixins
the only way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种方法是像在 CSS 中所做的那样列出选择器:但这不是 Sassy 的方法,因为这些元素不一定彼此相关,那么为什么它们应该属于在一起呢?他们只是碰巧有一些共同的风格。像这样将它们列出在一起效果很好,但是当您添加更多内容时,保持组织起来会稍微困难一些:
如果您想概括它,您当然可以使用 mixin :
但这是同一件事:#box1 和 #anotherbox 想要成为在文件的不同部分。您需要它们共享一些常见的样式,但更有意义的是让它们按照它们所属的框规则组进行组织,以便将它们分开:
因为这会调用 mixin 两次,所以它会在输出中创建重复:
这是当 mixin 只有一些规则或者在有限数量的地方使用时,这是完全可以接受的。 (当 mixin 根据参数生成不同的样式规则时,Mixin 才真正发挥作用。)
好的,这就是 mixin 的整个块之旅。
以下是使用 @extend 避免重复的方法:
@extend 的想法最初由 Nicole Sullivan 在她的 CSS 愿望清单 博客文章。这似乎是一个好主意,因此设计了当前纯 CSS 中的实现,并随后不久将其添加到 Sass 中。
如果您的样式规则描述了一个旨在用作通用类或许多其他项目的基本样式的模式,您应该考虑@extend。
要理解其中的区别,我们知道 mixin 会每次调用时都会输出样式,但扩展会创建一个使用该样式的所有选择器的列表,然后将它们附加到一个样式块:
生成:
它将打印基本样式类(.thumb-size)输出,即使您根本不需要它做其他任何事情。但是,我认为无论如何这都是更好的选择,因为它定义了选择器列表位于选择器前面时的用途。
One way is to list your selectors just as you have done in CSS: but that's not the Sassy way to do it since these elements don't necessarily relate to each other, so why should they belong together? They just happen to share some common styles. Listing them together like this works fine, but is slightly harder to keep organized as you add more:
You could of course use a mixin for that if you want to generalize it:
But it's the same thing: #box1 and #anotherbox want to be in different parts of the file. You need them to share some common styles, but it makes more sense to keep them organized with the group of box rules to which they belong so you split them up:
Since that calls the mixin twice, it creates duplication in the output:
This is perfectly acceptable when the mixin just has a few rules in it, or when it's used in a limited number of places. (Mixins really come into their own when the mixin generates different style rules based on parameters.)
Ok, so that's the trip around the block with mixins.
Here's how you avoid that duplication using @extend:
The idea of @extend was originally proposed by Nicole Sullivan in her CSS Wish List blog post. This seemed like such a good idea that an implementation in current plain CSS was devised, and it was added to Sass shortly thereafter.
If your style rules describe a pattern that's intended to be used as a general class or a base style for a number of other items, you should consider @extend.
To understand the difference, we know a mixin will output the styles each time it's called, but extend will instead create a list of all the selectors where that style is used, and then attach them to one style block:
Produces:
It will print the base style class (.thumb-size) in the output, even if you strictly don't need it for anything else. But, I think this is preferable anyway since it defines the purpose of that list of selectors when it's sitting in front of them.