Table of Contents
回复内容:
Home Web Front-end H5 Tutorial 如何删除多余无用的css?

如何删除多余无用的css?

Jun 07, 2016 am 08:42 AM
css

项目经过几个版本的迭代,几个css文件加起来都有快6k行了,目测一半都是没用的代码,有没有自动的工具识别并删除这些代码?
网上搜了一些,要么就是不好用,要么就是只能搜索出没用的css,不能自动删除的。
有没有人有更好的办法?

回复内容:

火狐的 CSS Usage 扩展是专门干这个的。用法是开启 AutoScan 选项,手动翻看尽量多的页面,手动触发显示尽量多的元素,以让尽量多的规则起作用,然后就可以导出干净的 CSS 了。
addons.mozilla.org/en-U

UnCSS giakki/uncss 路 GitHub
及对应 Grunt task grunt-uncss
  1. Open the page in Google Chrome
  2. Open the developer tools (Ctrl+Shift+I)
  3. Under Audits tab -> Select Audits to run - "Select All" & select "Audits Present State"
  4. click run
  5. expand the "Remove unused CSS rules" item to view a list of style sheets that contain unused style rules.
如果你用chrome的话,控制台有个audits tab,
选择web page performance(也可以用默认),run一下就能看的以下信息:

Put CSS in the document head (1)
CSS in the document body adversely impacts rendering performance.
Link node application.css should be moved to the document head in xxx.xxx.xxx.xxx/
Remove unused CSS rules (3637)
3637 rules (93%) of CSS not used by the current page.
application.css: 93% is not used by the current page.(这里还可以展开看列表)
Use normal CSS property names instead of vendor-prefixed ones (1)
application.css
.tooltip
"-webkit-box-shadow" is used, but "box-shadow" is supported. unused-css.com/ 这个全是英文怎么用啊 单页面的话 chrome 检测多余CSS的插件,具体每次忘记了。公用样式多的页面则没办法检测了 为什么要删? 注意规范和统一一下风格就好。

1. 删掉原来的很全局的 css ,如设置所有select 或者 input 的;
2. 规范 命名 和 风格,命名不要和控件属性的命名混淆就好。 select-list != SelectList

风格没有最佳,大家统一一个就好。 写好的css最好还是尽量少删啦,因为可能现在没起作用,在某一块没起作用,但是说不定以后能起作用,在别的地方能有用。在现在越来越强调代码重用的环境下,我们只能在编码初期就把代码整体架构给打好。比如说选择一个好的样式重置,科学的浮动清除方式等等都是至关重要的。所以我觉得与其想办法删除一些多余的css,倒不如把心思花在前期工作和css代码的锤炼上,毕竟事先解决比事后解决还是好一些的嘛。
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to achieve gap effect on the card and coupon layout with gradient background? How to achieve gap effect on the card and coupon layout with gradient background? Apr 05, 2025 am 07:48 AM

Realize the gap effect of card coupon layout. When designing card coupon layout, you often encounter the need to add gaps on card coupons, especially when the background is gradient...

Can Bootstrap directly implement horizontal waterfall flow layout? Can Bootstrap directly implement horizontal waterfall flow layout? Apr 05, 2025 am 07:15 AM

Using the Bootstrap framework to build horizontal waterfall flow layout Many developers hope to use the Bootstrap framework to quickly build web pages and achieve various complex layout effects...

The style remains the same after PC page zooms: What are the possible solutions? The style remains the same after PC page zooms: What are the possible solutions? Apr 05, 2025 am 07:51 AM

The challenge of keeping the style of the page zoomed and the same after the page is zoomed in. Many developers will encounter a difficult problem when making PC pages: when the user zooms in or out of the browsing...

Which websites are suitable for learning and enjoying excellent CSS effects display? Which websites are suitable for learning and enjoying excellent CSS effects display? Apr 05, 2025 am 07:09 AM

Explore excellent CSS effect display website If you are interested in learning and appreciating various exquisite CSS effects, it is very important to find the right resources. regardless...

CSS overflow:auto is invalid. Is the negative value of the parent element z-index the culprit? CSS overflow:auto is invalid. Is the negative value of the parent element z-index the culprit? Apr 05, 2025 am 07:12 AM

CSS scroll bar failure troubleshooting: overflow:auto failure cause analysis In using CSS to set up container overflow:auto...

2K resolution rendering design of corporate websites: How can we perfectly adapt to the customer's display environment? 2K resolution rendering design of corporate websites: How can we perfectly adapt to the customer's display environment? Apr 05, 2025 am 07:21 AM

Enterprise website rendering design: How to deal with the needs of 2K resolution. When designing a corporate website, customers often encounter special resolution requirements.

How to create blocks of irregular shapes with CSS code only? How to create blocks of irregular shapes with CSS code only? Apr 05, 2025 am 07:39 AM

Cleverly using CSS to implement irregular shape blocks This article will explain in detail how to use CSS to create irregular black shape blocks similar to those shown in the figure below: [There should be inserted here...

See all articles