Core points:
(The following is excerpted from the book "The Master of CSS" written by Tiffany Brown. The book is available worldwide, and you can also purchase the e-book version here.)
Developer tools can help you find and fix rendering issues, but how efficient is it? Have our file size been minimized? To do this, we need compression tools.
In the context of CSS, compression simply means removing excess characters. For example, consider the following code block:
<code>h1 { font: 16px / 1.5 'Helvetica Neue', arial, sans-serif; width: 80%; margin: 10px auto 0px; }</code>
This code includes line breaks and spaces, and is 98 bytes in length. Let's look at the compressed example:
<code>h1{font:16px/1.5 'Helvetica Neue',arial,sans-serif;width:80%;margin:10px auto 0}</code>
Now our CSS is only 80 bytes long, a decrease of 18%. Of course, the fewer bytes, the faster the download speed, and the less data you and your users will transfer.
In this section, we will introduce CSS Optimizer (CSSO), a Node.js-based compression tool. To install CSSO, you must first install Node.js and NPM. NPM is installed as part of the Node.js installation process, so you only need to install one package.
Using CSSO requires you to be familiar with the command line interface. Linux and OS X users can use terminal applications (for OS X, Applications > Terminal Applications). If you are using Windows, use the command prompt. Go to the Start or Windows menu and type cmd in the search box.
After setting up Node.js and NPM, you can install CSSO. At the command line prompt, type:
npm install -g csso
-g
Flags to install CSSO globally so that we can use it in the command line. OS X and Linux users may need to use sudo (sudo npm install -g csso
). When NPM prints its installation path to the command line window and the command line prompt reappears, you know it has been installed successfully, as shown in Figure 3.25.
Now we are ready to compress CSS.
To compress the CSS file, run the csso command and pass the file name as a parameter:
csso style.css
This will perform basic compression. CSSO removes unnecessary spaces, extra semicolons, and comments in CSS input files.
After completion, CSSO prints the optimized CSS to standard output, i.e. the current terminal or command prompt window. However, in most cases, we want to save that output to a file. To do this, pass the second parameter to csso, the name of the compressed file. For example, if we want to save the compressed version of style.css as style.min.css, we will use the following command:
csso style.css style.min.css
By default, CSSO rearranges parts of CSS. For example, it merges declaration blocks with duplicate selectors and removes some overwritten attributes. Consider the following CSS:
body { margin: 20px 30px; padding: 100px; margin-left: 0px; }
h1 { font: 200 36px / 1.5 sans-serif; }
h1 { color: #ff6600; }
In this code segment, margin-left
overwrites the previous margin
declaration. We also reused h1
as a selector for continuous declaration blocks. After optimization and compression, we get:
<code>h1 { font: 16px / 1.5 'Helvetica Neue', arial, sans-serif; width: 80%; margin: 10px auto 0px; }</code>
CSSO deletes unnecessary spaces, newlines, and semicolons and shortens #ff6600
to #f60
. CSSO also merges the margin
and margin-left
properties into a declaration (margin: 20px 30px 20px 0
) and combines our separate h1
selector blocks into one. Now, if you doubt how CSSO will rewrite your CSS, you can disable its refactoring feature. Just use the --restructure-off
or -off
logo. For example, running csso style.css style.min.css -off
will get the following results:
<code>h1{font:16px/1.5 'Helvetica Neue',arial,sans-serif;width:80%;margin:10px auto 0}</code>
Now our CSS is compressed, but not optimized. Disabling refactoring will prevent your CSS file from reaching the minimum size. Avoid disabling refactoring unless you encounter problems. The preprocessor introduced in Chapter 9 provides compression capabilities in its tool set; however, using CSSO can further reduce file size.
Frequently Asked Questions about Using CSS Optimizer to Compress CSS
Compressing CSS is an important aspect of web development because it helps reduce the size of CSS files. Reduced file size will result in faster website loading, thus providing a better user experience. The compressed CSS file removes unnecessary characters such as spaces, line breaks and comments, which do not affect the functionality of the code. This process can significantly improve the performance of the website, especially for users with slower internet connections.
CSS Optimizer works by analyzing your CSS code and removing unnecessary characters and redundancy. It compresses CSS files by eliminating spaces, line breaks and comments, and simplifying the code structure. This will result in smaller file sizes and faster downloads from the web browser, thereby improving the overall performance of the website.
While compressing CSS can significantly improve the performance of your website, it should be noted that it may also make the code harder to read and debug. This is because the process removes all formats and comments that make the code easy to read. Therefore, it is recommended to keep the original uncompressed CSS files for debugging and development.
Yes, you can manually compress CSS by removing all unnecessary characters and simplifying the code. However, this process can be very time-consuming and error-prone, especially for larger CSS files. This process can be automated using tools like CSS Optimizer to ensure that your CSS is compressed accurately and efficiently.
To use CSS Optimizer, you simply paste the CSS code into the input field on the CSS Optimizer website and click the "Optimizer" button. The tool will then analyze your code and give you a compressed version that you can copy and use in your website.
No, compressing CSS will not affect the functionality of your website. The process removes only unnecessary characters and simplifies the code, but does not change the way the browser interprets CSS rules. Therefore, the appearance and functionality of your website will remain the same, but it will load faster due to the smaller file size.
Yes, CSS Optimizer can handle large CSS files. However, the larger the file, the longer it takes for the tool to analyze and compress the code. Nevertheless, with large CSS files, using CSS Optimizer is still more efficient and accurate than trying to manually compress the code.
Yes, CSS Optimizer is a free tool that you can use to compress CSS code. It is a web-based tool so you don't need to download or install anything to use it. You just need to visit the CSS Optimizer website, paste your CSS code and you will get a compressed version.
Yes, you can reverse this process using the original uncompressed CSS file. This is why it is recommended to keep a copy of the original CSS file before compressing it. If you need to change or debug the code, you can use the original file and then compress it again when you are done.
In addition to compressing CSS, you can also compress HTML and JavaScript files. This can further reduce the loading time of the website. Other practices include optimizing images, using content distribution networks (CDNs), enabling compression, and implementing browser caching. These practices combined with CSS compression can significantly improve the performance and user experience of your website.
The above is the detailed content of Minifying CSS With CSS Optimizer. For more information, please follow other related articles on the PHP Chinese website!