Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have a sticky navbar in reactjs

My navbar is hiding the contents below the navbar. Also, it's sticked to the left side instead of the middle part. I hope someone could show me the right way to make the navbar right way to make a sticky navbar that stays in its place even when we are scrolling.

code sanbox https://codesandbox.io/s/clever-voice-24udr

Navbar.js

import React, { Fragment } from "react";
import { Link } from "react-router-dom";
import { Button } from "react-bootstrap";

const NavbarPage = () => {
  const guestLinks = (
    <ul>
      <li>
        <Link to="/">
          <Button className="btn btn-primary createNew">Create New</Button>
        </Link>
      </li>
      <li>
        <Link to="/about">
          <Button className="btn btn-danger signOut">Sign out</Button>
        </Link>
      </li>
    </ul>
  );
  return (
    <nav className="navbar bg-white">
      <h3 className="titleName">
        <Link to="/">
          <i className="fas fa-pen" /> demo
        </Link>
      </h3>
      {guestLinks}
    </nav>
  );
};

export default NavbarPage;
.navbar {
  position: fixed;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.7rem 2rem;
  overflow: hidden;
  z-index: 1;
  width: 88.15%;
  margin: auto;
  top: 0;
  border-bottom: solid 1px var(--primary-color);
  opacity: 0.9;
  top: 0;
  box-shadow: 12px 0 15px -4px rgba(31, 73, 125, 0.8),
    -12px 0 8px -4px rgba(31, 73, 125, 0.8);
  box-shadow: 0 9px 0px 0px white, 0 -9px 0px 0px white,
    12px 0 15px -4px rgba(31, 73, 125, 0.8),
    -12px 0 15px -4px rgba(31, 73, 125, 0.8);
}

like image 576
henrydoe Avatar asked Dec 18 '22 15:12

henrydoe


2 Answers

just wondering you can use position: sticky; top: 0 to do the same thing.

Pure CSS will be faster

like image 188
Pablo Domínguez Durán Avatar answered Dec 31 '22 03:12

Pablo Domínguez Durán


<Navbar sticky="top" />

Look at https://react-bootstrap.github.io/components/navbar/

like image 29
Oleg Avatar answered Dec 31 '22 02:12

Oleg