How to Optimize Your CSS

How to Optimize Your CSS

CSS expressions are evaluated very frequently, so can degrade page performance after it has loaded,Instead use one-time expressions or better yet use event handlers

CSS(Cascading Style Sheets)

These sheets make websites much more efficient because they allow the browsers to cache style-related information from the .css file directly, removing the need to read that information every time a single page is loaded. Even if Style Sheets are naturally more efficient than HTML tables you can still optimize the CSS code to make your website cleaner and faster.

First of all try to locate dispersed code and aggregate it together. For instance instead of:

margin-top: 10px;

margin-right: 20px;

margin-bottom: 10px;

margin-left: 20px;

you should use:

margin: 10px 20px 10px 20px;

Instead of:

<p>A paragraph of decorated text</p>

<p>Second paragraph</p>

<p>Third paragraph</p>

<p>Forth paragraph</p>

you should use:

<div>

<p>A paragraph of decorated text</p>

<p>Second paragraph</p>

<p>Third paragraph</p>

<p>Forth paragraph</p>

</div>

Secondly you can also try a CSS optimizer tool like CleanCSS. Those tools are supposed to merge similar selectors, remove useless properties, remove whitespace and so on.

The following two tabs change content below.

Hamza Basharat

Hi. I'm Hamza Basharat, I love to find solutions and help people. I'm a senior writer at TheHelptimes and AboutEngg, and currently studying computer engineering.