การปรับปรุงการโหลดฟอนต์
ใช้ `font-display` การโหลดล่วงหน้า และการทำชุดย่อย เพื่อให้ฟอนต์เว็บโหลดได้อย่างมีประสิทธิภาพโดยไม่ทำให้เลย์เอาต์เปลี่ยน
การปรับปรุงการโหลดฟอนต์ เป็นบทเรียน Web Performance Optimization & Lighthouse ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Web Performance Optimization & Lighthouse และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Web Performance Optimization & Lighthouse มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why Optimize Fonts?
Web fonts make websites beautiful and unique, but they can also be a major source of performance bottlenecks. Large font files mean longer download times, which directly impacts your site's overall speed and user experience.
Slow font loading can lead to issues like 'Flash of Unstyled Text' (FOUT) or 'Flash of Invisible Text' (FOIT). In this lesson, we'll learn practical techniques to load fonts efficiently.
Control Font Loading with `font-display`
The font-display CSS property gives you control over how web fonts load and display. It tells the browser what strategy to use while a font is still downloading, helping to prevent jarring visual shifts.
auto: The browser's default behavior.block: Short block period (invisible text), then infinite swap.swap: Very short block period (invisible text), then infinite swap.fallback: Very short block, then short swap, then permanent fallback.optional: Very short block, no swap, permanent fallback.
`font-display: swap` in Action
font-display: swap is a popular choice for balancing performance and aesthetics. It tells the browser to use a fallback font immediately. Once your custom web font is downloaded, it 'swaps' in, replacing the fallback.
This ensures text is always visible, prioritizing content readability over font styling during the initial load.
@font-face {
font-family: 'MyWebFont';
src: url('mywebfont.woff2') format('woff2');
font-display: swap;
}`optional` and `fallback` Strategies
Other font-display values offer different trade-offs:
fallback: Gives the custom font a very short block period (around 100ms) and a short swap period (around 3s). If the custom font isn't ready within these times, it uses a fallback font permanently.optional: Similar tofallback, but the browser decides if the custom font is downloaded at all, based on network conditions. If the font is not downloaded quickly, the fallback is used permanently, avoiding any layout shift.
Preloading Critical Fonts
Normally, browsers discover fonts when they parse your CSS. However, you can tell the browser to fetch a resource *earlier* than it would normally discover it using preloading.
For fonts, <link rel="preload"> means the browser can start downloading them even before the CSS that uses them is fully parsed. This significantly reduces the time until text is displayed with the correct font.
How to Preload a Font
You can preload fonts by adding a <link> tag in your HTML's <head> section. It's crucial to include as="font" and crossorigin to ensure the font is fetched correctly and to prevent double downloads.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Preload Font Example</title>
<link rel="preload" href="mywebfont.woff2" as="font" type="font/woff2" crossorigin>
<style>
@font-face {
font-family: 'MyWebFont';
src: url('mywebfont.woff2') format('woff2');
font-display: swap;
}
body {
font-family: 'MyWebFont', sans-serif;
}
</style>
</head>
<body>
<h1>Hello CoddyKit!</h1>
<p>This text uses a preloaded web font.</p>
</body>
</html>Reducing Font Size with Subsetting
Font files often contain hundreds, or even thousands, of characters. Many of these, like special symbols or characters for different languages, might not be used on your specific website.
Font subsetting is the process of creating a smaller version of a font file that only includes the specific characters (glyphs) your website actually needs. This drastically reduces the file size.
Why Subset Your Fonts?
Subsetting has a direct and significant impact on your website's performance:
- Faster Downloads: Smaller font files download much quicker, leading to improved page load times.
- Reduced Bandwidth: This saves data for your users, which is especially important for those on mobile plans.
- Better User Experience: Less waiting means users see your content, styled correctly, much faster.
Tools like Font Squirrel's Webfont Generator or Google Fonts can help you subset.
Test Your Font Knowledge
Which of the following are effective strategies for optimizing web font loading and performance?
Font Loading Recap
We've covered essential techniques for optimizing web font loading:
font-display: This CSS property controls how fonts render during download, preventing FOUT and FOIT.- Preloading: Using
<link rel="preload">in your HTML fetches critical fonts earlier, speeding up their availability. - Subsetting: Reduces font file size by including only the characters your website actually needs.
By applying these strategies, you can significantly improve your website's perceived performance and deliver a smoother user experience.
คำถามที่พบบ่อย
บทเรียน “การปรับปรุงการโหลดฟอนต์” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การปรับปรุงการโหลดฟอนต์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Web Performance Optimization & Lighthouse ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Web Performance Optimization & Lighthouse มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การปรับปรุงการโหลดฟอนต์”
ใช้ `font-display` การโหลดล่วงหน้า และการทำชุดย่อย เพื่อให้ฟอนต์เว็บโหลดได้อย่างมีประสิทธิภาพโดยไม่ทำให้เลย์เอาต์เปลี่ยน คุณปฏิบัติ Web Performance Optimization & Lighthouse ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Web Performance Optimization & Lighthouse หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Web Performance Optimization & Lighthouse บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “การปรับปรุงการโหลดฟอนต์” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Web Performance Optimization & Lighthouse นี้ได้ไหม
ได้ บทเรียน Web Performance Optimization & Lighthouse ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- CSS สำคัญและการส่งมอบ CSS
- การปรับปรุงการโหลดฟอนต์
- ผลกระทบด้านประสิทธิภาพของ CSS-in-JS
- การลด CSS ที่ไม่ได้ใช้งาน