Introduction
Good performance makes websites load faster and feel smooth. This keeps visitors happy and helps your site work well on all devices.
Jump into concepts and practice - no test required
Good performance makes websites load faster and feel smooth. This keeps visitors happy and helps your site work well on all devices.
/* Example of efficient CSS selector */ .container > .item { color: blue; }
/* Good: simple class selector */ .button { background-color: green; }
/* Avoid: very deep selector */ html body div.container ul li a.button { color: red; }
/* Use shorthand properties */ margin: 10px 5px;
/* Avoid using @import for CSS files */ @import url('styles.css');
This example uses simple class selectors and CSS transitions for smooth hover effects. The boxes are easy to style and fast to render. The tabindex and aria-label improve accessibility.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Performance Example</title> <style> /* Efficient selector */ .box { width: 10rem; height: 10rem; background-color: #4caf50; margin: 1rem; transition: background-color 0.3s ease; } .box:hover { background-color: #388e3c; } </style> </head> <body> <main> <section> <div class="box" tabindex="0" aria-label="Green box"></div> <div class="box" tabindex="0" aria-label="Green box"></div> <div class="box" tabindex="0" aria-label="Green box"></div> </section> </main> </body> </html>
Use simple selectors like classes or IDs for better speed.
Minimize the use of universal selectors (*) and deep descendant selectors.
Combine CSS files and use shorthand properties to reduce file size.
Simple CSS selectors help browsers apply styles faster.
Avoid complex or deep selectors to improve performance.
Use shorthand and avoid @import to speed up loading.
.class instead of complex selectors like div ul li a:hover?.class match elements directly, making style application faster.@import, which slows loading. * { box-sizing: border-box; } div p span { font-size: 1rem; } uses universal selector * and deep selectors, which are slower.<link> tags by a single large CSS file?body { font-family: Arial; }
div p span { color: blue; }div p span is deep and requires multiple element checks..highlight reduces matching steps and speeds rendering.div p span is slow; use a class selector instead. -> Option B