← 中文资源

简体中文版本

最大内容绘制(LCP)

LCP 上报首屏最大内容元素(通常是主视觉图或标题块)可见的时刻,作为 Core Web Vitals 的加载体验指标,良好线为 75 分位 2.5 秒。

阅读英文原文

概念定义

用户以“主内容出现”感知加载完成,而不是网络空闲。LCP 捕捉的正是首屏最大元素绘制的时刻;若更大的元素随后绘制,候选会被替换,指标始终对“主导视口的内容”保持诚实。

工作机制

largest-contentful-paint 类型的 PerformanceObserver 收到带绘制时间的元素候选。LCP 子阶段把时间归因为:首字节、资源加载延迟、资源加载时长、元素渲染延迟——各自指向从服务端缓存到优先级提示的不同修法。

使用场景

主内容大而可见的页面:商品页、文章、落地页。预加载 LCP 图片、加 fetchpriority、绝不懒加载它,并把它从 CSS 链的发现路径里解放出来;标题交给服务端渲染,让文字先当早期 LCP、图片再流入。

常见误区

LCP 不是 load 事件:它常在 load 完成前或后很久。客户端渲染的页面可能先画骨架、最终 LCP 反复振荡;第三方字体晚到会把文本块换装成新的 LCP。实验室与现场在缓存热度与设备速度上分歧很大——实验室看方向,现场才是真相。

代码示例

<p>
  Search results for <mark>HTML</mark>:
  The <mark>HTML</mark> standard defines the structure of web pages.
  Learning <mark>HTML</mark> is the first step in web development.
</p>

<p>
  The <mark>critical threshold</mark> is reached when
  the temperature exceeds 100&deg;C.
</p>

<blockquote>
  <p>
    It was the best of times, it was the worst of times,
    it was the age of <mark>wisdom</mark>, it was the age
    of <mark>foolishness</mark>.
  </p>
</blockquote>

<p>
  Required fields are marked with <mark>*</mark>.
  Please review the <mark>terms and conditions</mark>
  before proceeding.
</p>

<style>
  mark {
    background: #ffeb3b;
    color: #000;
    padding: 0 2px;
    border-radius: 2px;
  }
  mark.search-result {
    background: #fff3cd;
    border-bottom: 2px solid #ffc107;
  }
  mark.important {
    background: #ffcdd2;
    font-weight: bold;
  }
  mark::before {
    content: "\1F50E";
    margin-right: 2px;
  }
</style>
public class StringBuilderManipulation {

    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder(64);

        sb.append("Hello");
        sb.append(" ");
        sb.append("World");
        System.out.println("Built: " + sb);

        sb.insert(5, " Java");
        System.out.println("After insert: " + sb);

        sb.delete(5, 10);
        System.out.println("After delete: " + sb);

        sb.replace(0, 5, "Hi");
        System.out.println("After replace: " + sb);

        sb.reverse();
        System.out.println("Reversed: " + sb);

        String csv = String.join(", ",
            "Apple", "Banana", "Cherry", "Date");
        System.out.println("CSV: " + csv);

        String formatted = String.format(
            "Name: %s, Age: %d, Score: %.2f",
            "Alice", 30, 95.5
        );
        System.out.println(formatted);

        String stripped = "  padded  ".strip();
        System.out.println("Stripped: '" + stripped + "'");
    }
}