Clearfix 的 Sass / SCSS Mixin - 最佳方法?
我想从 HTML 中删除 clearfix
类,并在我的 SCSS(Rails 3.1 应用程序)中包含一个clearfix mixin。对此最好的方法是什么?
我正在考虑仅采用 HTML 5 Boilerplateclearfix 并将其转换为 mixin,然后将其@包含在 CSS 中以获取需要clearfixing 的元素。
复制自 HTML5 Boilerplate:
/* The Magnificent Clearfix: Updated to prevent margin-collapsing on child elements. http://j.mp/bestclearfix */
.clearfix:before, .clearfix:after { content: "\0020"; display: block; height: 0; overflow: hidden; }
.clearfix:after { clear: both; }
/* Fix clearfix: blueprintcss.lighthouseapp.com/projects/15318/tickets/5-extra-margin padding-bottom-of-page */
.clearfix { zoom: 1; }
这有缺点吗?有更好的办法吗?我可以通过这种方式安全地从 HTML 标记中删除 clearfix 吗?
I want to remove the clearfix
class from my HTML and include a clearfix mixin in my SCSS (Rails 3.1 application). What is the best approach to this?
I am thinking of just taking the HTML 5 Boilerplate clearfix and turning it into a mixin, then @including it within the CSS for elements that need clearfixing.
Copied from HTML5 Boilerplate:
/* The Magnificent Clearfix: Updated to prevent margin-collapsing on child elements. http://j.mp/bestclearfix */
.clearfix:before, .clearfix:after { content: "\0020"; display: block; height: 0; overflow: hidden; }
.clearfix:after { clear: both; }
/* Fix clearfix: blueprintcss.lighthouseapp.com/projects/15318/tickets/5-extra-margin padding-bottom-of-page */
.clearfix { zoom: 1; }
Is there a drawback to this? Is there a better way? Can I safely remove clearfix from my HTML markup this way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我可能应该添加 - 这是我想出的解决方案。我对这一切还很陌生,所以我不知道这是否真的能完成与将元素的类设置为clearfix并使用上面的HTML5样板代码相同的工作。
编辑:
最好使用
@extend
而不是 mixin,因为它会产生更少的 CSS 代码。此外,在使用
@extend
时使用静默类(用%
表示)也很有用。这可以防止意外的CSS规则并消除您正在使用的规则如果未直接使用,则进行扩展。I should probably have added - this is the solution I came up with. I am still pretty new to all this so I don't know if this will actually do the same job as setting an element's class to clearfix and using the HTML5 boilerplate code above.
Edit:
It's best to use
@extend
instead of a mixin as it will produce much less CSS code.Also, it's useful to use a silent class (denoted by
%
) when using@extend
. This prevents unexpected CSS rules and eliminates the rule you're extending if it's not being used directly.要使用
@extend
实现更少的代码输出,请将clearfix 定义为 占位符(此处仅适用于没有 IE 6+7 的现代浏览器):Sass 代码:
CSS 输出将是:
To achieve less code output by using
@extend
, define clearfix as placeholder (here just for modern browsers w/o IE 6+7):Sass code:
CSS output will be:
为什么不使用 Compass 框架?它已经在许多其他有用的 mixin 中提供了用于clearfix的mixins公用事业。寻找现有的工具总是比自己维护额外的代码更好。
Why not use the Compass framework? It already provides mixins for clearfix among a host of other useful mixins and utilities. It's always better to look for existing tools rather than to have to maintain additional code yourself.