计算 css 文件中选择器的数量
是否有现有的插件/应用程序/程序/脚本/任何东西可以分析和计算文件的CSS选择器?我想检查我的 css 文件在 IE 中不起作用的原因是否是因为我的选择器计数超过 4095 (我很确定不是)
谢谢!
ps 如果有 haml/sass/compass 解决方案加分
is there an existing plugin/app/program/script/whatever that analyzes and counts the css selectors of a file? i want to check if the reason my css file is not working in IE is because my selector count is over 4095 (which im pretty sure is not)
thanks!
p.s. plus points if there's a haml/sass/compass solution
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
以下代码片段可以在 Firefox 的 Firebug 控制台中运行,以计算 CSS 选择器(不仅仅是 CSS 规则)的总数,并检查其是否达到 每个样式表限制 4095 个选择器:
The following snippet can be run in the Firebug console in Firefox to count the total number of CSS selectors (not just CSS rules) and check whether it reaches the limit of 4095 selectors per stylesheet:
有一个小书签可以告诉您在总 CSS 规则(您感兴趣的)中使用的 CSS 规则的数量。
CSS紧缩
There is this bookmarklet that tells you the number of used CSS rules out of the total CSS rules (which you are interested in).
CSS Crunch
我的项目 Bless CSS 可能就是您正在寻找的。它将分析文件并根据选择器限制在最佳点分割它们。
它还内置于 CodeKit 中。
My project, Bless CSS, could be what you're looking for. It will analyze files and split them at the optimum point based on the selector limit.
It's also built in to CodeKit.
这将执行内联 CSS...
jsFiddle。
This will do inline CSS...
jsFiddle.
搜索与在 CSS 文件中将“{”替换为“{”。大多数编辑会告诉你已经做了多少次替换......
Search & replace "{" by "{" in your CSS file. Most editors wil tell how many replacements you’ve done…
如果您想在构建过程中执行此操作或者只是不想在 JS 中执行此操作,则可以使用简单的算法来计算选择器:
将“{”和“}”之间的文本替换为“,”,然后计算“,”的数量。这将为您提供文件中选择器的数量。
Simple algorithm for counting the selectors if you want to do it as part of the build process or simply don't want to do it in JS:
Replace the texts between "{" and "}" with a "," and then calculate the number of ",". This will give you the number of selectors on the file.
有点晚了,但对于任何人来说如何寻找 css 选择器计数器:
http://snippet.bevey.com/css/selectorCount.php
它非常简单,并且可以使用多个样式表,它甚至会在您达到 4096 限制时告诉您
a bit to late, but for anyone how's looking for a css selector counter:
http://snippet.bevey.com/css/selectorCount.php
it's very simple, and can work with more than one stylesheet, it even tells you when you hit the 4096 limit
基于 10 年后 jetli13 的答案,没有 IE 选择器限制,但未来 JS 生成器中会出现样式组件和大型 CSS ,我添加了以下
style
标记或external
样式表添加的样式脚本- 在控制台中复制并运行
您可以在 SPA 应用程序中更改路由后运行它,以查看添加了多少规则。
跨域样式表规则又名您的第 3 方在做什么
如果您需要 Safari Web 检查器中的第 3 方样式,请执行以下操作:
输出
Building on jetli13's answer 10 years later with no IE selector limits to be seen but styledcomponents and mega CSS in JS generator's in the future, I added the following
style
tag orexternal
stylesheetsScript - Copy and run in console
You can run it after a route change in a SPA application to see how many rules got added.
Cross Domain Stylesheet Rules a.k.a What is your 3rd party doing
If you need 3rd party styles in Safari web inspector do this:
Output