我正在用react-router-dom
制作一个多页面的网站,并试图在顶部添加一个固定的导航栏.
我的导航栏组件和css文件:
import { NavLink } from "react-router-dom";
import styles from "./NavBar.module.css";
const NavBar = () => {
return (
<nav className={styles.nav}>
<ul>
<li>
<NavLink
className={({ isActive }) => (isActive ? styles.active : undefined)}
to="/"
>
Home
</NavLink>
</li>
<li>
<NavLink
className={({ isActive }) => (isActive ? styles.active : undefined)}
to="/Menu"
>
Menu
</NavLink>
</li>
<li>
<NavLink
className={({ isActive }) => (isActive ? styles.active : undefined)}
to="/Contact"
>
Contact
</NavLink>
</li>
</ul>
</nav>
);
};
export default NavBar;
.nav {
position: fixed;
width: 100%;
z-index: 10;
background-color: white;
display: flex;
justify-content: center;
}
.nav ul {
list-style-type: none;
display: flex;
gap: 1rem;
}
.nav a {
color: grey;
font-weight: bold;
text-decoration: none;
padding-bottom: 10px;
transition: color 0.15s border-bottom 0.15s;
font-size: 1.15rem;
}
.nav a.active {
color: black;
border-bottom: 2px solid black;
}
.nav a:hover {
color: black;
}
我将导航栏添加到我的网站上,如下所示:
import { Outlet } from "react-router-dom";
import NavBar from "../components/NavBar/NavBar";
const Root = () => {
return (
<div>
<NavBar />
<Outlet />
</div>
);
};
export default Root;
基本上,Outlet的内容正在改变我导航栏的大小.例如,在主页上,导航栏的宽度为690px,但在联系人页面上,导航栏的宽度为710px.问题似乎出在Outlet中元素的大小上,因为当我降低高度时,大小变化会变得不那么严重.但导航吧是固定的,所以我不确定为什么插座会影响它.
以下是我的主页组件和css文件:
import styles from "./Home.module.css";
import img from "../../img/spread.jpg";
import { Link } from "react-router-dom";
import Map from "../../components/Map/Map";
import HomeContact from "./HomeContact/HomeContact";
const Home = () => {
return (
<div>
<div className={styles.home}>
<div>
<h5>Welcome to</h5>
<h1>Spring Garden</h1>
<Link to="/Menu">View Menu</Link>
</div>
<img src={img} alt="A table full of Chinese food."></img>
</div>
<HomeContact />
<Map />
</div>
);
};
export default Home;
.home {
display: flex;
height: 60vh;
align-items: center;
}
.home div {
width: 50%;
padding-left: 2rem;
padding-right: 2rem;
}
.home img {
width: 50%;
height: 100%;
object-fit: cover;
}
.home h1 {
font-size: 4rem;
line-height: 5rem;
margin-top: 0rem;
}
.home h5 {
font-size: 1.25rem;
margin-bottom: 1rem;
}
.home a {
text-decoration: none;
font-weight: bold;
background-color: rgb(205, 7, 30);
color: white;
padding: 1rem;
transition: background-color 0.15s;
}
.home a:hover {
background-color: rgb(231, 9, 35);
}
我正在try 添加一个固定的导航栏,它不会在我的网站的每一页上改变.然而,通过判断元素,我看到每一页上的NAV大小都有轻微的变化.这会导致我的导航栏略有偏移.我不明白为什么会发生这种情况,因为导航栏不应该是文档流的一部分.
整个项目都在GitHub:https://github.com/iamgeeyuh/spring-garden-website上
这是一个running codesandbox美元的演示.