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

本地与GitHub站点CSS样式不同步:导航栏重叠问题求助

问题描述

我的导航栏在GitHub站点上出现元素重叠,但本地文件预览显示正常。

以下是我的HTML代码:

<div class="heading">
  <span class="logo">
      <a href="#top"><img src="./images/logo2.png" alt="Logo"/></a>
      <p><a href="#top">Vu Nguyen</a></p>
  </span>
  <span id="menu">
    <a href="#top">Home</a>
    <a href="#about">About</a>
    <a href="#skills">Skills</a>
    <a href="#projects">Projects</a>
  </span>
</div>

CSS样式代码:

body,
html {
  height: 100%;
  font-family: "Arial", sans-serif;
  line-height: 1.6;
  color: #333;
  background-color: #000000;
  margin: 0px;
  padding: 0px;
  min-width: 100%;
  scroll-behavior: initial;
  scroll-snap-type: x mandatory;
}

.heading {
  position: fixed;
  width: 100%;
  top: 0;
  background-color: #000000;
  z-index: 1000;
  border-bottom: 2px solid #f4f4f4;
  padding: 20px;
  display: flex;
  flex: 1;
  flex-direction: row;
  align-items: center;
  padding: 20px;
}
.heading span {
  color: #ffffff;
  font-size: 1.5em;
  letter-spacing: 2px;
}
.logo {
  display: flex;
  align-items: center;
  flex-shrink: 0;
  cursor: pointer;
}
.logo img {
  width: 80px;
  flex-shrink: 0;
}
.logo a {
  text-decoration: none;
  color: #ffffff;
}
.logo p {
  color: #ffffff;
  font-size: 1.3em;
  letter-spacing: 2px;
  margin-left: 10px;
  font-weight: bold;
  text-transform: uppercase;
  text-decoration: none;
  transition: color 0.3s;
}

#menu {
  display: flex;
  justify-content: space-between;
  margin-left: 20%;
  width: 50%;
}

#menu a {
  text-decoration: none;
  font-size: large;
  color: #ffffff;
  padding: 20px;
  border-radius: 5px;
  align-items: center;
  transition: background-color 0.3s;
}

我希望得到CSS代码优化建议,同时想知道这个问题是代码本身的问题,还是GitHub部署导致的?


问题分析与优化方案

问题根源

这个问题是代码本身的布局缺陷,和GitHub部署无关。核心问题有两点:

  1. .heading设置了width:100%同时添加padding:20px,按照CSS盒模型默认规则,元素总宽度会变成100% + 40px,超出视口后挤压内部元素导致重叠。
  2. #menu使用固定比例的margin-left:20%和width:50%,在不同视口宽度下,这种布局容易超出剩余空间,引发元素堆叠。

优化后的CSS代码

body,
html {
  height: 100%;
  font-family: "Arial", sans-serif;
  line-height: 1.6;
  color: #333;
  background-color: #000000;
  margin: 0;
  padding: 0;
  scroll-behavior: initial;
  scroll-snap-type: x mandatory;
}

.heading {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  background-color: #000000;
  z-index: 1000;
  border-bottom: 2px solid #f4f4f4;
  padding: 20px;
  display: flex;
  flex-direction: row;
  align-items: center;
  box-sizing: border-box;
  justify-content: space-between;
}
.heading span {
  color: #ffffff;
  font-size: 1.5em;
  letter-spacing: 2px;
}
.logo {
  display: flex;
  align-items: center;
  flex-shrink: 0;
  cursor: pointer;
}
.logo img {
  width: 80px;
  flex-shrink: 0;
}
.logo a {
  text-decoration: none;
  color: #ffffff;
}
.logo p {
  color: #ffffff;
  font-size: 1.3em;
  letter-spacing: 2px;
  margin-left: 10px;
  font-weight: bold;
  text-transform: uppercase;
  text-decoration: none;
  transition: color 0.3s;
}

#menu {
  display: flex;
  gap: 20px;
  margin-left: auto;
}

#menu a {
  text-decoration: none;
  font-size: large;
  color: #ffffff;
  padding: 10px 20px;
  border-radius: 5px;
  transition: background-color 0.3s;
}

优化说明

  • 给.heading添加box-sizing:border-box,让padding包含在元素宽度内,避免超出视口。
  • 用left:0;right:0替代width:100%,配合box-sizing确保导航栏始终适配视口宽度。
  • 修改#menu布局:去掉固定比例的margin-left和width,用margin-left:auto让菜单自动靠右侧,用gap控制菜单项间距,解决比例布局导致的空间不足问题。
  • 调整#menu a的上下padding,避免导航栏高度过高。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 03:54:54