You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:freeCodeCamp作品集项目Navbar样式未生效问题排查

问题描述

我是正在完成freeCodeCamp作业的前端新手,正在制作个人作品集。我期望navbar元素水平显示,并应用指定字体与样式,但该部分CSS完全未生效,其余页面CSS正常。当前navbar始终使用默认字体,元素垂直排列。我已尝试在HTML中调整navbar元素的位置,但无任何效果。附上相关代码,恳请技术指导!

相关代码

HTML代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My Portfolio</title>
    <link rel="stylesheet" href="./styles.css">
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif:ital,wght@0,100..900;1,100..900&display=swap" rel="stylesheet">
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif:ital,wght@0,100..900;1,100..900&family=Poppins:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap" rel="stylesheet">
    <link
    href="https://cdn.jsdelivr.net/npm/remixicon@4.2.0/fonts/remixicon.css"
    rel="stylesheet"
/>
</head>
<body>
    <header>
    <nav id='navbar'>
        <ul class=""nav-links'>
            <li><a href="#welcome-section" class="nav-link">Welcome</a></li>
            <li><a href="#projects" class="nav-link">Projects</a></li>
            <li><a href="#contact" class="nav-link">Contact</a></li>
        </ul>
    </nav>
    </header>
</body>
</html>

CSS代码

*{
    margin: 0;
}

body {
    font-family: "Noto Serif", sans-serif;
    text-align: center;
    background-color: var(--neutral);
}

h1,
h2,
h3,
h4,
h5,
h6 {
    font-family: "Poppins", sans-serif;
}

h1{
    font-size: 60px;
}

#navbar {
    display: flex;
    justify-content: center;
    position: fixed;
    top: 0;
    width: 100%;
    background-color: var(--neutral);
    padding: 20px 0;
    font-size: 20px;
    border-bottom: 1px solid var(--neutral-focus);
}

.nav-links {
    display: flex;
    list-style: none;
}

.nav-links a{
    text-decoration: none;
    padding: 0 10px;
    color: var(--base-100);
}

.nav-links a:hover {
    text-decoration: underline;
}
解决方案

1. 修复HTML中ul的class语法错误

你的HTML里<ul>标签的class属性写法错误:class=""nav-links',混合使用双引号和单引号导致浏览器无法识别正确的class名称,这是CSS未生效的核心原因——.nav-links选择器找不到对应元素,flex布局等样式自然无法应用,列表项只能默认垂直排列。将其修改为:

<ul class="nav-links">

2. 定义CSS变量

你的CSS中使用了var(--neutral)、var(--neutral-focus)、var(--base-100)这些变量,但未提前定义,会导致背景色、文字颜色等样式失效。可以在CSS开头添加:root伪类定义变量,示例:

:root {
    --neutral: #f0f0f0;
    --neutral-focus: #cccccc;
    --base-100: #333333;
}

你可以根据需求自行调整颜色值。

3. 验证字体应用

修复上述问题后,navbar会继承body的Noto Serif字体,若想单独给navbar指定字体,可在#navbar的CSS规则中添加font-family属性,比如:

#navbar {
    /* 原有样式保留 */
    font-family: "Poppins", sans-serif;
}

内容的提问来源于stack exchange,提问作者Youssef

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 07:37:05