如何在反应中将项目居中

发布于 2025-01-16 17:15:33 字数 3305 浏览 0 评论 0原文

我为引用创建了一个导航栏,目前正在格式化页面,我对反应还很陌生,所以我尝试从基础知识开始。我被困在如何将我的 NavBarLinks 居中,我已经使用文本中心和位置来使链接位于导航栏的中心,但如果我使网络浏览器变小,它就不会停留在中心。我的问题是在反应中将项目居中的权利是什么。

import React, { useState } from "react";
import {
  NavbarContainer,
  TopContainer,
  BottomContainer,
  NavbarExtendedContainer,
  NavbarInnerContainer,
  NavbarLinkContainer,
  NavbarLink,
  Logo,
  OpenLinksButton,
  NavbarLinkExtended,
} from "../styles/Navbar.style";
import LogoImg from "../assets/logo.png";

function Navbar() {
  const [extendNavbar, setExtendNavbar] = useState(false);

  return (
    <NavbarContainer extendNavbar={extendNavbar}>
      <NavbarInnerContainer>
        <TopContainer>
          <NavbarLinkContainer>
            <OpenLinksButton
              onClick={() => {
                setExtendNavbar((curr) => !curr);
              }}
            >
              {extendNavbar ? <>&#10005;</> : <> &#8801;</>}
            </OpenLinksButton>
            <Logo src={LogoImg}></Logo>
          </NavbarLinkContainer>
        </TopContainer>
        <BottomContainer>
        <NavbarLinkContainer>
        <NavbarLink to="/"> Home</NavbarLink>
            <NavbarLink to="/products"> Products</NavbarLink>
            <NavbarLink to="/contact"> Contact Us</NavbarLink>
            <NavbarLink to="/about"> About Us</NavbarLink>
        </NavbarLinkContainer>
        </BottomContainer>
      </NavbarInnerContainer>
      {extendNavbar && (
        <NavbarExtendedContainer>
          <NavbarLinkExtended to="/"> Home</NavbarLinkExtended>
          <NavbarLinkExtended to="/products"> Products</NavbarLinkExtended>
          <NavbarLinkExtended to="/contact"> Contact Us</NavbarLinkExtended>
          <NavbarLinkExtended to="/about"> About Us</NavbarLinkExtended>
        </NavbarExtendedContainer>
      )}
    </NavbarContainer>
  );
}

export default Navbar;

样式页

import styled from "styled-components";
import { Link } from "react-router-dom";

export const NavbarContainer = styled.nav`
  width: 100%;
  background-color: black;
  @media (min-width: 700px) {
    height: 80px;
  }
`;

export const TopContainer = styled.div`
  padding-left: 5%;
`;

export const BottomContainer = styled.div`
  padding-right: 50px;
  background-color:salmon;
`;

export const NavbarInnerContainer = styled.div`
  width: 100%;
  height: 80px;
`;

export const NavbarLinkContainer = styled.div`
  display: flex;
`;

export const NavbarLink = styled(Link)`
  color: white;
  font-size: x-large;
  font-family: Arial, Helvetica, sans-serif;
  position: relative;
  left: 43%;
  top:10%;
  text-decoration: none;
  margin: 10px;
  height: 30px;
  text-align: center;
  @media (max-width: 700px) {
    display: none;
  }
`;

export const NavbarLinkExtended = styled(Link)`
  color: white;
  font-size: x-large;
  font-family: Arial, Helvetica, sans-serif;
  text-decoration: none;
  margin: 10px;
`;

export const Logo = styled.img`
  @media (min-width: 700px) {
    margin: auto;
  }
  margin: 10px;
  max-width: 180px;
  height: auto;
`;

Im crating a navbar for a cite and currently formatting the page Im pretty new to react so im trying to start with the basics. I am stuck on how to center my NavBarLinks, I have used text-center and position to make the links be in the center of the navbar but if I make the web browser smaller it wont stay in the center. My question is what is the right to center items in react.

import React, { useState } from "react";
import {
  NavbarContainer,
  TopContainer,
  BottomContainer,
  NavbarExtendedContainer,
  NavbarInnerContainer,
  NavbarLinkContainer,
  NavbarLink,
  Logo,
  OpenLinksButton,
  NavbarLinkExtended,
} from "../styles/Navbar.style";
import LogoImg from "../assets/logo.png";

function Navbar() {
  const [extendNavbar, setExtendNavbar] = useState(false);

  return (
    <NavbarContainer extendNavbar={extendNavbar}>
      <NavbarInnerContainer>
        <TopContainer>
          <NavbarLinkContainer>
            <OpenLinksButton
              onClick={() => {
                setExtendNavbar((curr) => !curr);
              }}
            >
              {extendNavbar ? <>✕</> : <> ≡</>}
            </OpenLinksButton>
            <Logo src={LogoImg}></Logo>
          </NavbarLinkContainer>
        </TopContainer>
        <BottomContainer>
        <NavbarLinkContainer>
        <NavbarLink to="/"> Home</NavbarLink>
            <NavbarLink to="/products"> Products</NavbarLink>
            <NavbarLink to="/contact"> Contact Us</NavbarLink>
            <NavbarLink to="/about"> About Us</NavbarLink>
        </NavbarLinkContainer>
        </BottomContainer>
      </NavbarInnerContainer>
      {extendNavbar && (
        <NavbarExtendedContainer>
          <NavbarLinkExtended to="/"> Home</NavbarLinkExtended>
          <NavbarLinkExtended to="/products"> Products</NavbarLinkExtended>
          <NavbarLinkExtended to="/contact"> Contact Us</NavbarLinkExtended>
          <NavbarLinkExtended to="/about"> About Us</NavbarLinkExtended>
        </NavbarExtendedContainer>
      )}
    </NavbarContainer>
  );
}

export default Navbar;

Style page

import styled from "styled-components";
import { Link } from "react-router-dom";

export const NavbarContainer = styled.nav`
  width: 100%;
  background-color: black;
  @media (min-width: 700px) {
    height: 80px;
  }
`;

export const TopContainer = styled.div`
  padding-left: 5%;
`;

export const BottomContainer = styled.div`
  padding-right: 50px;
  background-color:salmon;
`;

export const NavbarInnerContainer = styled.div`
  width: 100%;
  height: 80px;
`;

export const NavbarLinkContainer = styled.div`
  display: flex;
`;

export const NavbarLink = styled(Link)`
  color: white;
  font-size: x-large;
  font-family: Arial, Helvetica, sans-serif;
  position: relative;
  left: 43%;
  top:10%;
  text-decoration: none;
  margin: 10px;
  height: 30px;
  text-align: center;
  @media (max-width: 700px) {
    display: none;
  }
`;

export const NavbarLinkExtended = styled(Link)`
  color: white;
  font-size: x-large;
  font-family: Arial, Helvetica, sans-serif;
  text-decoration: none;
  margin: 10px;
`;

export const Logo = styled.img`
  @media (min-width: 700px) {
    margin: auto;
  }
  margin: 10px;
  max-width: 180px;
  height: auto;
`;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尐籹人 2025-01-23 17:15:33

我真的不建议为 css 使用完全不同的页面,而只需将代码添加到 index.css,从那里您可以访问您的 css > 来自 react 应用程序中的任何组件

为了使 NavbarLink 居中,您可以使用 flexbox

index.css 中创建一些 css 代码:

.navbar__link {
  width: 100%
  display: flex;
  justify-content: center;
}

并将该 class 添加到您的 component 元素 中:

<NavbarLinkContainer className='navbar__link'>
  <NavbarLink to="/"> Home</NavbarLink>
  <NavbarLink to="/products"> Products</NavbarLink>
  <NavbarLink to="/contact"> Contact Us</NavbarLink>
  <NavbarLink to="/about"> About Us</NavbarLink>
</NavbarLinkContainer>

I really wouldn't recommend using a whole different page for css instead just add your code to index.css, from there you can access your css from any component in your react app.

And to center the NavbarLink you can use flexbox.

Create some css code in index.css:

.navbar__link {
  width: 100%
  display: flex;
  justify-content: center;
}

And add that class in your component element:

<NavbarLinkContainer className='navbar__link'>
  <NavbarLink to="/"> Home</NavbarLink>
  <NavbarLink to="/products"> Products</NavbarLink>
  <NavbarLink to="/contact"> Contact Us</NavbarLink>
  <NavbarLink to="/about"> About Us</NavbarLink>
</NavbarLinkContainer>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文