0Pricing
Web Performance Optimization & Lighthouse · บทเรียน

เจาะลึก LCP, FID และ CLS

วิเคราะห์ Largest Contentful Paint (LCP), First Input Delay (FID) และ Cumulative Layout Shift (CLS) อย่างละเอียด พร้อมเทคนิคการปรับปรุง

เจาะลึก LCP, FID และ CLS เป็นบทเรียน Web Performance Optimization & Lighthouse ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Web Performance Optimization & Lighthouse และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Web Performance Optimization & Lighthouse มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Core Web Vitals Deep Dive

Welcome to a deeper look into the Core Web Vitals! These metrics are crucial for understanding and improving your website's user experience.

In this lesson, we'll analyze Largest Contentful Paint (LCP), First Input Delay (FID), and Cumulative Layout Shift (CLS). You'll learn what each measures, why they matter, and practical techniques to optimize them.

Largest Contentful Paint (LCP)

Largest Contentful Paint (LCP) measures the time it takes for the largest content element visible within the viewport to render. Think of it as how quickly a user sees the main content of your page.

  • A good LCP score is 2.5 seconds or less.
  • Anything above 4 seconds is considered poor.

LCP is a key indicator of your page's perceived loading speed.

What Counts for LCP?

Not all elements contribute to LCP. Typically, the largest image or text block that's visible when the page first loads is the LCP element. Common LCP elements include:

  • <img> elements
  • <video> elements (using their poster image)
  • Elements with a background image loaded via url()
  • Block-level text elements containing text nodes (e.g., <h1>, <p>)

Knowing your LCP element is the first step to optimizing it!

Optimizing LCP: Speeding Up Resources

A major factor for LCP is how quickly your browser can fetch and render critical resources, especially images and fonts. Here's a common strategy:

  • Preload critical images: Use <link rel="preload"> to tell the browser to fetch high-priority resources sooner.
  • Optimize images: Compress, use modern formats (WebP, AVIF), and responsive images.
  • Minimize render-blocking resources: Reduce or defer CSS and JavaScript that prevent the page from rendering quickly.

Let's see an example of preloading a hero image:

<!DOCTYPE html>
<html>
<head>
  <title>LCP Preload Demo</title>
  <!-- Preload the hero image to fetch it early -->
  <link rel="preload" href="https://via.placeholder.com/800x450.webp" as="image">
  <style>
    body { margin: 0; font-family: sans-serif; }
    img { max-width: 100%; height: auto; display: block; }
    h1 { padding: 10px; }
  </style>
</head>
<body>
  <h1>Welcome to Our Site!</h1>
  <!-- The actual image will render faster due to preload -->
  <img src="https://via.placeholder.com/800x450.webp" alt="Important Hero Image" width="800" height="450">
  <p>This image is likely the LCP element. Preloading helps it appear faster.</p>
</body>
</html>

First Input Delay (FID)

First Input Delay (FID) measures the time from when a user first interacts with a page (e.g., clicks a button, taps a link) to when the browser is actually able to begin processing that interaction.

It's about responsiveness and how quickly your page reacts to user input. It doesn't measure the event handler execution time, only the delay before it can start.

  • A good FID score is 100 milliseconds or less.
  • Anything above 300 milliseconds is considered poor.

Why FID is High: Busy Main Thread

A high FID often means the browser's main thread is busy doing other work, typically executing JavaScript, and can't respond to user input immediately.

Common causes include:

  • Long JavaScript tasks: Heavy scripts that run for an extended period, blocking the main thread.
  • Large JavaScript bundles: More code means more time to parse, compile, and execute.
  • Third-party scripts: Ads, analytics, or other external scripts can consume significant main thread time.

Optimizing FID: Freeing the Main Thread

To improve FID, you need to reduce the amount of time the main thread is blocked. Here's how:

  • Break up long tasks: Divide large JavaScript operations into smaller, asynchronous chunks.
  • Defer or async non-critical JS: Use defer or async attributes for scripts that aren't essential for initial rendering.
  • Reduce JavaScript payload: Minify, tree-shake, and code-split your JavaScript bundles.
  • Use Web Workers: Offload computationally intensive tasks to a background thread, keeping the main thread free.

Cumulative Layout Shift (CLS)

Cumulative Layout Shift (CLS) measures the visual stability of a page. It quantifies how much unexpected layout shifts occur during the page's lifespan.

An unexpected shift happens when a visible element changes its start position from one rendered frame to the next. This can be very frustrating for users!

  • A good CLS score is 0.1 or less.
  • Anything above 0.25 is considered poor.

Common Causes of CLS

Layout shifts often happen when content loads or changes dynamically without reserving space. Key culprits include:

  • Images or videos without dimensions: The browser doesn't know how much space to reserve until the media loads.
  • Dynamically injected content: Ads, banners, or widgets that appear after the page has started rendering.
  • Web Fonts causing FOIT/FOUT: Fonts loading late can cause text to reflow or disappear/reappear.
  • Actions waiting for a network response: Content that shifts after an API call completes.

Optimizing CLS: Stable Layouts

Preventing CLS is all about reserving space and ensuring elements don't unexpectedly move. Here are some techniques:

  • Specify image/video dimensions: Always use width and height attributes, or CSS aspect-ratio.
  • Reserve space for ads/embeds: Use CSS min-height or a placeholder element.
  • Avoid inserting content above existing content: Especially after initial page render.
  • Preload fonts & use font-display: Use font-display: optional or swap to manage font loading behavior.

Here's an example of how setting image dimensions prevents CLS:

<!DOCTYPE html>
<html>
<head>
  <title>CLS Prevention Demo</title>
  <style>
    body { font-family: sans-serif; }
    .container { width: 300px; margin: 20px auto; border: 1px solid #ccc; padding: 10px; }
    img { max-width: 100%; height: auto; display: block; margin-bottom: 10px; }
  </style>
</head>
<body>
  <div class="container">
    <p>This content is stable.</p>
    <!-- Image with specified width and height prevents layout shift -->
    <img src="https://via.placeholder.com/300x200" alt="Placeholder" width="300" height="200">
    <p>The content below the image does not jump around.</p>
  </div>
</body>
</html>

Core Web Vitals Check

Let's test your understanding of Core Web Vitals and their optimization techniques.

Recap: LCP, FID, CLS

You've successfully dived deep into the Core Web Vitals!

  • LCP (Largest Contentful Paint): Measures perceived load speed, focusing on the largest content element. Optimize by preloading, optimizing images, and reducing server response time.
  • FID (First Input Delay): Measures interactivity, focusing on the delay before the browser responds to user input. Optimize by minimizing and breaking up JavaScript tasks.
  • CLS (Cumulative Layout Shift): Measures visual stability. Optimize by reserving space for dynamic content, specifying image dimensions, and managing font loading.

By understanding and improving these metrics, you contribute to a much better user experience!

คำถามที่พบบ่อย

บทเรียน “เจาะลึก LCP, FID และ CLS” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “เจาะลึก LCP, FID และ CLS” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Web Performance Optimization & Lighthouse ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Web Performance Optimization & Lighthouse มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “เจาะลึก LCP, FID และ CLS”

วิเคราะห์ Largest Contentful Paint (LCP), First Input Delay (FID) และ Cumulative Layout Shift (CLS) อย่างละเอียด พร้อมเทคนิคการปรับปรุง คุณปฏิบัติ Web Performance Optimization & Lighthouse ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Web Performance Optimization & Lighthouse หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Web Performance Optimization & Lighthouse บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “เจาะลึก LCP, FID และ CLS” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Web Performance Optimization & Lighthouse นี้ได้ไหม

ได้ บทเรียน Web Performance Optimization & Lighthouse ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. รู้จัก Core Web Vitals
  2. เจาะลึก LCP, FID และ CLS
  3. การปรับปรุงตัวชี้วัดการโต้ตอบของผู้ใช้
  4. การโต้ตอบจนถึงการแสดงผลถัดไป (INP)
← กลับไปที่ Web Performance Optimization & Lighthouse