使用较少或缩小的 CSS 的最佳实践

发布于 2024-12-27 06:10:58 字数 372 浏览 0 评论 0原文

我正在考虑在即将到来的项目中减少使用,但一直在尝试找出在开发过程中使用它的最佳方法。

通常,在开发网站时,我会编写 html 和 css,然后开始在浏览器中测试它,查看它的外观、优化、重新加载并重复整个过程,直到我对所有内容的外观感到满意为止。

该过程的一个关键部分是使用浏览器中的“检查元素”功能来识别我需要更改的 CSS 片段。通常只需查看行号,我就知道 CSS 文件中的位置。

但如果我使用 Less(或任何其他组合/压缩 CSS 的方法),就会使行号变得毫无用处。我知道我可以使用“查找”来搜索代码部分,但行号要快得多。

当处理涉及其他开发人员或大型 CSS 文件的项目时尤其如此。

我只是想知道其他人如何处理这个问题,或者也许有更好的过程?

I'm considering using less in an upcoming project but have been trying to figure out the best way to work with it while in development.

Usually when developing a site I'll write my html and css then start testing it in the browser, see how it looks, refine, reload, and repeat the whole process until I'm happy with how everything looks.

A crucial part of the process is using the Inspect Element feature in the browser to identify the piece of CSS I need to change. Usually just by looking at the line number I know exactly where to go in my CSS file.

But if I use Less (or any other method of combining/compressing my CSS) it makes the line numbers useless. I know I could use Find to search for the section of code but line numbers are much faster.

This is especially true when working on a project that involves other developers or large CSS files.

I just wondered how others deal with this, or maybe there's a better process?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

策马西风 2025-01-03 06:10:58

缩小的 CSS 确实应该只用于站点的生产版本。当您执行测试/更改等时,这一切都应该在辅助服务器区域中的站点的某些测试或开发版本中完成,您可以在其中使用可用的 css 行号版本。 JavaScript 也是如此。在生产可视副本中,您希望将其缩小。在开发过程中,你不需要。

无论如何,您应该始终拥有 2 个 css 副本。第一个副本是主要源副本,即您的开发副本。它具有所有格式正确且带有换行符的样式。另一个是已投入生产的最新 CSS 缩小版本。如果您需要解决某些问题,这将允许您在两个文件之间快速切换,假设您的站点使用某种模板化交付(布局页面、母版页或其他)。

缩小版本仅在最终交付中有用。所有其他需求都应使用主副本。在站点的服务器端部分放置一个配置来确定要使用的样式表可能不是一个坏主意。这样您就可以简单地更改配置设置并进入“调试”模式。

Minified CSS really should only be going out to the production version of your site. When you're performing tests/changes etc, this should all be done in some test or development version of your site in a secondary server area where you can have the line-numbers version of css available. The same would go for JavaScript. On the production viewable copy, you want it minified. In development, you don't.

In any event, you should always have 2 copies of your css. The first copy is the primary source copy that is your development copy. It has all of your properly formatted styles with line-breaks in it. The other is the very latest minified version of your css that went to production. This will allow you to switch between the 2 files rapidly in the event that you need to work something out, assuming your site uses some sort of templated delivery (layout pages, master pages, or whatever).

The minified version will only be useful in the final delivery. All other needs should use the master copy. It might not be a bad idea to put a configuration in server-side portion of your site that determines what style sheet to use. That way you can simply change a configuration setting and go into a "debug" mode.

东京女 2025-01-03 06:10:58

我同意乔尔的观点——这就是我的处理方式。在将每个版本通过 FTP 传输到生产环境之前,构建脚本会缩小 CSS(和 JS)。我只是在 PHP 中有一个开关,例如:

if ($config->prod()) {
    // incldue the minfied css
} else {
    // include all the original files
}

I agree with Joel - that's how I handle it. A build script minifies the CSS (and JS) before each release is FTP'ed to production. I just have a switch in PHP like:

if ($config->prod()) {
    // incldue the minfied css
} else {
    // include all the original files
}
_畞蕅 2025-01-03 06:10:58

就我个人而言,我使用 ant 构建脚本来制作生产版本:

  1. 它将多个 css 文件“压缩”为一个
  2. ,然后使用 YUI 压缩器缩小它们,
  3. 脚本相同
  4. (页面重新组合以指向新生成的文件),

这样您就可以划分 http我想说,请求这些文件,并获得 30% 到 70% 的带宽。也取决于 gzip。

就我而言,开发版本有:
18 CSS 权重 178ko
在生产版本中,在 96ko 时减少到 1 个 CSS

Personnaly, I use an ant build script to make a production version:

  1. it "condense" multiple css files in one
  2. then it minify them with YUI compressor
  3. same for scripts
  4. (page recomposition to point to the newly generated files)

this way you divide your http request for those files, and gain some bandwith from 30% to 70% i'd say. depends on gzip also.

in my case, the dev version have:
18 css weighting 178ko
reduced down to 1 css at 96ko in the production version

梦在夏天 2025-01-03 06:10:58

每次保存 Less 文件时,我个人都会使用工具来缩小 CSS 并将其注入浏览器。所以我立即看到每一个变化。这样一来,刚刚发生的事情就很清楚了。我不再需要太多的引用行号。

我最近开始使用源映射来查看正确的文件并再次在浏览器中检查 CSS 时(我的 less 文件的行号)。我想这就是你正在寻找的。我个人并不需要这种额外的花哨。

I personally use tools to minify and inject the CSS into the browser each time i save my Less file. So i see each change immediately. This way it's pretty clear what just happened. I don't need the referencing line numbers that much any more.

I recently started using source maps, to see the correct file and line numbers (of my less files) when inspecting CSS in the browser again. I think that is what you are looking for. I personally don't need this extra fanciness that much.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文