如何删除多余无用的css?
项目经过几个版本的迭代,几个css文件加起来都有快6k行了,目测一半都是没用的代码,有没有自动的工具识别并删除这些代码?
网上搜了一些,要么就是不好用,要么就是只能搜索出没用的css,不能自动删除的。
有没有人有更好的办法?
回复内容:
火狐的 CSS Usage 扩展是专门干这个的。用法是开启 AutoScan 选项,手动翻看尽量多的页面,手动触发显示尽量多的元素,以让尽量多的规则起作用,然后就可以导出干净的 CSS 了。https://addons.mozilla.org/en-US/firefox/addon/css-usage/
UnCSS giakki/uncss 路 GitHub
及对应 Grunt task grunt-uncss
- Open the page in Google Chrome
- Open the developer tools (Ctrl+Shift+I)
- Under Audits tab -> Select Audits to run - "Select All" & select "Audits Present State"
- click run
- expand the "Remove unused CSS rules" item to view a list of style sheets that contain unused style rules.
选择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 http://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代码的锤炼上,毕竟事先解决比事后解决还是好一些的嘛。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text
