简体   繁体   English

React header 无法填满屏幕

[英]React header doesn't fill screen

I'm new to React and don't really understand why my code will not fill the screen horizontally.我是 React 新手,并不真正理解为什么我的代码不会水平填充屏幕。

If I open the page then this it what is shown for the header:如果我打开该页面,那么这就是 header 显示的内容: 在此处输入图像描述

However for some reason it is also possible to scroll to the side so it will look like:但是由于某种原因,它也可以滚动到一边,所以它看起来像: 在此处输入图像描述

Which doesn't look that great.这看起来不太好。 What have is wrong and how can it be fixed?有什么问题,如何解决?

Header.jsx Header.jsx

function Header(){ 

  return (
  <Navbar className="navbar-container" bg="dark" variant="dark" expand="lg">
    <Navbar.Brand href="https://github.com/">
      <img src={logo} alt="logo" style={{ width: 100, height: 100 }} />
      </Navbar.Brand>
    <Navbar.Toggle aria-controls="basic-navbar-nav" />
    <Navbar.Collapse id="basic-navbar-nav" className="navbar-collapse">
      <Nav.Link href="home">Home</Nav.Link>
      <Nav.Link href="projects" >Projects</Nav.Link>
      <Nav.Link href="settings" >Settings</Nav.Link>
    </Navbar.Collapse>
    <Navbar.Text lg="6" className="d-none d-lg-block">Daily progress:</Navbar.Text>
    <ProgressBar bgcolor="#6a1b9a" completed="60"/>
  </Navbar>
  );
}

export default Header;

Header.css Header.css

.navbar-container {
  width: 100%;
}

.navbar-collapse {
  text-align: center;
}

I thought adding "width:100%" to the navbar-container would fix it, but it didn't do anything.我认为向导航栏容器添加“宽度:100%”可以解决它,但它没有做任何事情。

It seems that you are scrolling the page, not the header itself.似乎您正在滚动页面,而不是 header 本身。 You better check the other elements, since they might overflow.您最好检查其他元素,因为它们可能会溢出。

If you checked other elements: Really depends on what other styles are added to the element, but you can try:如果你检查了其他元素:真的取决于元素中添加了哪些其他styles,但你可以尝试:

max-width: 100%; as Ollie said, but keep in mind the box model:正如奥利所说,但请记住盒子 model:

.navbar-container {
  max-width: 100%;
  box-sizing: border-box;
}

Or you can hide the overflow:或者你可以隐藏溢出:

.navbar-container {
  width: 100%;
  overflow-y: hidden;
  overflow-x: hidden; // if necessary
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM