From the author: on the internet today, page load time is one of the most important metrics of a website. Even milliseconds can have a huge impact on the bottom line, and slow page loading can easily ruin your conversion rates. There are many tools and techniques you can use to speed up your website. In this article, we’ll take a look at the best CSS optimization tips that you can use to improve the performance of your interface.
1. Find performance bottlenecks
The most important thing in all types of optimization is a thorough audit. Fortunately, there are many CSS diagnostic tools that can help you find bottlenecks. First of all, you can use your web browser’s DevTools to check how fast assets are loading. In most browsers, you can open DevTools by pressing the F12 key.
For example, in Firefox DevTools, you can check the size and load times of all CSS files loaded by a page using the Network tab. You can also check how fast CSS loads with and without caching. Since DevTools also exposes external CSS such as Google Font files and CSS assets pulled from third-party CDNs, you can find many sources that you didn’t even know about before.
Google’s Pingdom Tools and Lighthouse are two other free tools that developers often use to analyze website speed and interface performance. For example, Pingdom Tools will provide you with some helpful CSS optimization tips if you are running a simple site speed test.
2. Minify and compress CSS files
Most websites use multiple CSS files. While modular CSS is considered best practice in most cases, it can take a while for all files to load. But this is exactly the reason why CSS minification and compression tools exist. If you use them wisely, you can significantly improve your page load times.
There are online tools like CSS Minify that allow you to minify a CSS file by copying it into a simple form. This type of tool can be suitable for small projects. However, their use can become cumbersome and time-consuming in the case of large projects that come with multiple CSS files. In such cases, it is better to opt for an automated solution.
Most build tools these days allow you to automatically compress your code. For example, Webpack returns all files as a minified package by default. PostCSS also has smart plugins like CSS Nano that not only minify files but also run them through many targeted optimizations.
3. Use Flexbox and CSS Grid
If you are still relying only on the traditional box model when writing CSS and aligning elements on the screen using margins, padding, and floats, you should consider using more modern layout modules, namely flexbox and CSS Grid. These new models allow you to implement complex layouts with much less code.
With the old methods, you have to apply a lot of tweaks and tweaks, even for things as simple as centering elements vertically. However, with flexbox and CSS Grid, things are easier. While it may take a while to get to grips with the new layout modules, it is well worth it as your CSS files will be much smaller. This is especially true of flexbox, which currently has pretty good browser support (currently 98.3% globally).
Although browser support for CSS Grid is lower (currently 92.03% globally), you can still use it if you don’t need support for older browsers or want to provide a fallback.
4. Use link tag instead of @import rules
There are two main methods you can use to load CSS files into a web page:
adding them to the section
HTML pages using the link tag,
import them from other stylesheets using the @import CSS rule.
You need to add an @import rule to the top of your main CSS file. In most cases, it is used to download small resources such as fonts and other design elements. This may seem like a good solution at first, but it takes a lot longer for the browser to load additional stylesheets than when the HTML page loads them directly using link tags.
When adding multiple HTML files to an HTML page, always pay attention to the specificity of the CSS. Add the most general style sheet first, then move on to more specific ones. You need to do it this way because the stylesheets you add later override the rules of the previous CSS files. Here’s an example of how the CSS files are added in the correct order:
<link rel=“stylesheet” href=“https://webformyself.com/7-sovetov-optimizacii-css/main.css”>
<link rel=“stylesheet” href=“page.css”>
<link rel=“stylesheet” href=“component.css”>
5. Use gradients and SVGs instead of images
It can take a long time to load all images on a web page. Developers use many techniques to optimize images, such as loading images from an external CDN, or using image compression tools like TinyJPG. These solutions can help a lot, however you can often replace resource-intensive JPG and PNG images with native CSS effects.
For example, you can use gradients instead of huge background images, which can slow down your user’s browser. You can use the CSS gradient features to create linear, radial, and repeating gradients. With these built-in CSS functions, you can define not only colors, but also slope.
For example, the following rule creates a nice gradient background that loads much faster than any images:
background: linear-gradient(45deg, lightgreen, royalblue);
For more complex gradients and textures, you can also use generators such as CSSmatic (pictured below) and ColorZilla.
Besides gradients, you can also replace traditional JPG and PNG images with scalable vector graphics (SVG). SVGs not only load faster, but you also only need to include one version of the image. Because SVG can be scaled to any size without loss of quality due to its vector nature. Alternatively, you can also style the SVG with CSS as if it were a regular HTML file.
6. Avoid the! Important rule
While the! Important rule may be an appropriate solution in some cases, you should only use it as a last resort. This rule creates an exception from the cascade. Hence, when you add! Important to a CSS declaration, it overrides any other declarations, even those with higher specificity. This is what its syntax looks like:
margin-bottom: 20px ! important;
If there are too many! Important rules in the CSS, the user’s browser will have to perform additional code checks, which can significantly slow down the page. As a rule of thumb, never use! Important for your site’s CSS or when creating a theme or plugin. If possible, only use it if you want to override CSS from a third party library.
7. Consider CSS Refactoring
While refactoring CSS is rarely an easy task, there are many cases where it can significantly improve site performance. For example, when your CSS files are too large, or you have inherited your codebase, or you have slow page load times that seriously hurt your conversion rates. The goal of CSS refactoring is to make your code cleaner, easier to maintain, and faster to load.
Refactoring CSS is a multi-step process in which you need to analyze every aspect of your CSS code base. There are several things you need to check, such as:
do you have any unused or duplicate CSS rules or resources,
can you use more modern techniques like flexbox and CSS grid,
are you using too much specificity (you can calculate this with the visual specificity calculator),
is the structure of your CSS files reasonable (e.g. it is easier to maintain smaller files than large ones),
is it worth using an automated build tool,
and many others.
Before you start refactoring, also set measurable goals and criteria you will use, such as page load time or time to first display, so that you can compare before and after.
Also remember to use a version control tool like Git. That way, if something goes wrong, you can revert to a previous version of your code.
There are many CSS optimization tips you can use to improve the performance of your site. Most are easy to implement, but can have a significant impact on page load times. Faster page loading not only improves the user experience, but also helps you get higher rankings on Google and other search engines.
Besides CSS optimization guidelines, there are many other techniques you can use to improve loading speed, such as caching, Google AMP, and HTTPS.
Editorial staff: Webformyself team.
PSD to HTML
Practice of site layout on CSS Grid from scratch