个人作品集网站加载时出现字体及样式闪烁问题求助
问题:网站加载时字体及样式出现短暂闪烁
我制作的个人作品集网站,在不同浏览器中打开时,字体及其他样式会先显示为非预期状态,1-2秒后才刷新为预设样式。当前使用DreamHost作为主机服务商,不确定该问题是否与服务商有关。
相关代码
HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>...</title> <link href="https://fonts.googleapis.com/css2?family=Caveat:wght@400;700&display=swap" rel="stylesheet"> <link rel="stylesheet" href="style.css"> </head> <body> <nav> <ul> <li><a href="#home">Home</a></li> <li><a href="#projects">OS Projects</a></li> <li><a href="#education">Education</a></li> <li><a href="#experience">Industry</a></li> <li><a href="#awards">Awards</a></li> </ul> </nav> <div class="main-container"> <aside class="static-left"> <img src="assets/AI-me2-transparent.jpg" alt="Levent Özbek" class="profile-picture"> <div class="contact-info"> <p>...</p> </div> </aside> <div class="scrollable-right"> <section id="home" class="section"> <h1>...</h1> <p>...</p> </section> <section id="projects" class="section"> <h1>Open Source Projects</h1> <div id="project-list"> <p>Projects will be dynamically loaded here...</p> </div> </section> <section id="education" class="section"> <h1>Education</h1> <div class="education-list"> <div class="education-item">...</div> <div class="education-item">...</div> </div> </section> <section id="experience" class="section"> <h1>Industry</h1> <div class="experience-list"> <div class="experience-item">...</div> <div class="experience-item">...</div> <div class="experience-item">...</div> <div class="experience-item">...</div> <div class="experience-item">...</div> <div class="experience-item">...</div> <div class="experience-item">...</div> <div class="experience-item">...</div> <div class="experience-item">...</div> <div class="experience-item">...</div> </div> </section> <section id="awards" class="section"> <h1>Awards</h1> <div class="awards-list"> <div class="award-item">...</div> <div class="award-item">...</div> </div> </section> </div> </div> <script src="script.js"></script> </body> </html>
style.css
/* General Reset */ * { margin: 0; padding: 0; box-sizing: border-box; } /* Global Body Styles */ body { font-family: 'Caveat', cursive; /* Apply the Caveat font globally */ background-color: white; color: black; line-height: 1.6; scroll-behavior: smooth; } /* Navigation Bar */ nav { position: fixed; top: 0; right: 20px; background-color: white; z-index: 1000; } nav ul { list-style: none; display: flex; flex-direction: row; gap: 10px; } nav ul li a { text-decoration: none; font-weight: bold; color: black; font-size: 1.5rem; } nav ul li a:hover { text-decoration: underline; } /* Main Layout */ .main-container { display: flex; height: 100vh; } /* Static Left Section */ .static-left { position: fixed; left: 0; top: 0; width: 30%; height: 100vh; background-color: white; padding: 20px; overflow: hidden; border-right: 1px solid #ddd; } .profile-picture { width: 200px; height: 250px; object-fit: cover; /* Ensures the image scales properly within the dimensions */ border: 3px solid black; /* Keeps the border */ margin-bottom: 20px; } .contact-info { font-size: 1.2rem; line-height: 1.8; } .contact-info a { color: black; text-decoration: none; font-weight: bold; } .contact-info a:hover { text-decoration: underline; } /* Scrollable Right Content */ .scrollable-right { margin-left: 30%; width: 70%; padding: 20px; overflow-y: auto; } h1, h2, h3 { font-family: 'Caveat', cursive; } .section { padding: 50px 20px; border-bottom: 1px solid #ddd; } .section h1 { font-size: 3rem; margin-bottom: 20px; } .section p { font-size: 1.5rem; font-family: 'Caveat', cursive; } /* Projects Section */ #projects h1 { font-size: 3rem; margin-bottom: 20px; } #project-list { display: flex; flex-direction: column; gap: 20px; /* Add spacing between projects */ } .project-item { padding: 20px; border: 1px solid #ddd; border-radius: 10px; background-color: #f9f9f9; } .project-item h3 { font-size: 2rem; margin-bottom: 10px; } .project-item p { font-size: 1.4rem; margin-bottom: 10px; } .project-item a { text-decoration: none; font-size: 1.2rem; font-weight: bold; color: black; } .project-item a:hover { text-decoration: underline; } /* Education Section */ .education-list { display: flex; flex-direction: column; gap: 20px; /* Add spacing between education entries */ } .education-item { padding: 20px; border: 1px solid #ddd; border-radius: 10px; background-color: #f9f9f9; } .education-item h2 { font-size: 2rem; font-weight: bold; margin-bottom: 10px; } .education-item p { font-size: 1.5rem; } /* Experience Section */ .experience-list { display: flex; flex-direction: column; gap: 20px; /* Add spacing between experience entries */ } .experience-item { padding: 20px; border: 1px solid #ddd; border-radius: 10px; background-color: #f9f9f9; } .experience-item h2 { font-size: 2rem; font-weight: bold; margin-bottom: 10px; } .experience-item p { font-size: 1.5rem; } .experience-item ul { list-style: disc; padding-left: 20px; margin-top: 10px; /* Add spacing above the list */ } .experience-item ul li { margin-bottom: 5px; font-size: 1.4rem; } /* Awards Section */ .awards-list { display: flex; flex-direction: column; gap: 20px; /* Add spacing between award entries */ } .award-item { padding: 20px; border: 1px solid #ddd; border-radius: 10px; background-color: #f9f9f9; } .award-item h2 { font-size: 2rem; font-weight: bold; margin-bottom: 10px; } .award-item p { font-size: 1.5rem; } /* Scrollbar Styling (Optional) */ .scrollable-right::-webkit-scrollbar { width: 8px; } .scrollable-right::-webkit-scrollbar-thumb { background-color: #888; border-radius: 4px; } .scrollable-right::-webkit-scrollbar-thumb:hover { background-color: #555; } @media (max-width: 768px) { /* General layout stacking for mobile */ .main-container { flex-direction: column; /* Stack items vertically */ align-items: center; /* Center-align content for a clean look */ } /* Static-left adjustments */ .static-left { position: relative; /* Remove fixed positioning */ width: 100%; /* Full width for mobile */ text-align: center; /* Center-align content */ padding: 20px 10px; border: none; /* Remove border on mobile */ overflow: visible; /* Ensure content is not clipped */ } /* Profile picture adjustments */ .static-left .profile-picture { width: 150px; /* Reduce image size for smaller screens */ height: auto; /* Maintain aspect ratio */ margin: 0 auto; /* Center the image */ display: block; /* Ensures it's treated as a block-level element */ z-index: 1; /* Bring it to the front */ } /* Ensure contact info is visible */ .static-left .contact-info { margin-top: 20px; /* Add spacing below the profile picture */ font-size: 1.2rem; /* Adjust for readability */ line-height: 1.8; } /* Scrollable-right adjustments */ .scrollable-right { margin-left: 0; /* Remove margin for stacked layout */ width: 100%; /* Full width for mobile */ padding: 20px 10px; overflow: visible; /* Allow content to flow naturally */ } /* Navigation */ nav { position: static; /* Remove fixed positioning */ width: 100%; text-align: center; padding: 10px 0; border-bottom: 1px solid black; } nav ul { flex-direction: row; justify-content: center; gap: 15px; } nav ul li a { font-size: 1.2rem; } /* Section padding adjustments for mobile */ .section { padding: 20px 10px; /* Adjust padding for smaller screens */ } .section h1 { font-size: 2.5rem; /* Slightly smaller headings for mobile */ } .section p { font-size: 1.2rem; /* Smaller font for better readability */ } }
script.js
document.addEventListener("DOMContentLoaded", () => { // Fetch and display projects from projects.json fetch("projects.json") .then((response) => response.json()) .then((data) => populateProjects(data.projects)) .catch((error) => console.error("Error loading projects.json:", error)); function populateProjects(projects) { const projectList = document.getElementById("project-list"); if (!projectList) return; projectList.innerHTML = ""; // Clear placeholder projects.forEach((project) => { const projectItem = document.createElement("div"); projectItem.className = "project-item"; projectItem.innerHTML = ` <h3>${project.name}</h3> <p>${project.description}</p> ${project.link ? `<a href="${project.link}" target="_blank">View Project</a>` : ""} `; projectList.appendChild(projectItem); }); } });
解决方案
1. 优化字体加载,消除字体闪烁
- 确保Google Fonts链接启用
font-display: swap,让浏览器先显示系统默认字体,自定义字体加载完成后再替换,避免空白或样式突变。你的链接已包含display=swap,若仍有问题可显式指定:<link href="https://fonts.googleapis.com/css2?family=Caveat:wght@400;700&display=swap&font-display=swap" rel="stylesheet"> - 添加字体加载监听,在字体就绪后切换样式:
先给body设置降级字体:
再在JS中添加监听:body { font-family: sans-serif, 'Caveat', cursive; }document.fonts.ready.then(() => { document.body.style.fontFamily = "'Caveat', cursive"; });
2. 加速CSS加载,避免样式延迟
- 将关键CSS(如body、nav、静态左侧栏的基础样式)内联到HTML头部,减少外部CSS加载等待时间:
<head> <!-- 其他meta和link --> <style> * { margin:0; padding:0; box-sizing:border-box; } body { font-family: sans-serif, 'Caveat', cursive; background:white; color:black; } nav { position:fixed; top:0; right:20px; background:white; z-index:1000; } .static-left { position:fixed; left:0; top:0; width:30%; height:100vh; background:white; padding:20px; border-right:1px solid #ddd; } </style> </head> - 用
preload预加载关键资源:<link rel="preload" href="style.css" as="style"> <link rel="preload" href="https://fonts.googleapis.com/css2?family=Caveat:wght@400;700&display=swap" as="style">
3. 优化动态内容的布局稳定性
- 给项目列表添加占位样式,避免加载完成后布局跳动:
修改HTML中的占位文本:#project-list .placeholder { padding:20px; border:1px solid #ddd; border-radius:10px; background:#f9f9f9; font-size:1.4rem; color:#666; }<div id="project-list"> <div class="placeholder">Projects will be dynamically loaded here...</div> </div>
4. 静态资源缓存配置(针对DreamHost)
DreamHost本身不会导致这类问题,但可通过.htaccess添加缓存头加速静态资源重复加载:
<IfModule mod_expires.c> ExpiresActive On ExpiresByType text/css "access plus 1 month" ExpiresByType application/font-woff2 "access plus 1 year" ExpiresByType image/jpeg "access plus 1 month" ExpiresByType image/png "access plus 1 month" </IfModule>
内容的提问来源于stack exchange,提问作者mchd
相关产品推荐
相关产品推荐

