Alright, you're aiming for top-notch Core Web Vitals — crisp, smooth, fast-loading webpages that make Lighthouse go “WOW” and Google rank you like royalty 👑. Let’s break down the best practices to write HTML, CSS, JS, fonts, etc. that give you green scores across all Web Vitals:
HTML: Keep it Clean, Semantic & Lightweight
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Your Awesome Page</title>
<!-- Preconnects for performance -->
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<!-- Inline Critical CSS -->
<style>
/* Only critical styles here (for above-the-fold content) */
body {
font-family: 'Inter', sans-serif;
margin: 0;
padding: 0;
background: #fff;
color: #333;
}
h1 {
font-size: 2rem;
text-align: center;
margin-top: 2rem;
}
</style>
<!-- Load fonts properly -->
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap" rel="stylesheet" />
<!-- Defer non-critical CSS -->
<link rel="stylesheet" href="styles.css" media="print" onload="this.media='all'" />
<noscript><link rel="stylesheet" href="styles.css" /></noscript>
<!-- Important: Avoid large JS in <head> -->
</head>
<body>
<h1>Hello, Web Vitals!</h1>
<button id="btn">Click Me</button>
<!-- Minimal Critical JS (inlined) -->
<script>
document.addEventListener('DOMContentLoaded', () => {
document.getElementById('btn').addEventListener('click', () => {
alert('Hi there!');
});
});
</script>
<!-- Async/defer all non-critical JS -->
<script src="main.js" defer></script>
</body>
</html>
CSS: Optimize, Minify & Use Only What’s Needed
- Extract Critical CSS and inline it (manually or with tools like critical)
- Load the rest asynchronously (media="print" trick)
- Avoid unused styles (Lighthouse can show you these)
- Use
:where()
or:is()
instead of long selector chains (faster parsing)
JavaScript: Split, Defer, and Delay
- Inline only critical JS
- Use defer for everything else
- Use code splitting (in frameworks like React, Vue, etc.)
- Avoid large JS bundles — aim for <150KB (gzipped) for initial load
- Lazy-load expensive features like charts, sliders, chat widgets
Fonts: Load Fast and Avoid Layout Shifts
- Use
font-display: swap
(default with Google Fonts) - Preconnect to
fonts.googleapis.com
andfonts.gstatic.com
- Prefer system fonts for super-fast load (optional)
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap');
OR use:
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
Images: Compress, Lazy Load, Responsive
<img src="banner.jpg" loading="lazy" alt="Banner" width="600" height="400" />
- Use
loading="lazy"
- Add width and height attributes to prevent CLS (layout shift)
- Prefer
WebP
orAVIF
- Use
srcset
for responsive images if needed
Core Web Vitals Targets (What You’re Chasing)
Metric Goal
LCP (Largest Contentful Paint) < 2.5s
FID (First Input Delay) < 100ms
CLS (Cumulative Layout Shift) < 0.1
TTI (Time to Interactive) As fast as possible
Read More
Comprehensive Guide to Documentation Comments in C#
Understanding Strong Password Regex in C#: From Basic to Advanced
Thank You,
Keep coding
Top comments (0)