henrydoe
henrydoe

Reputation: 397

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);
}

Upvotes: 6

Views: 47469

Answers (5)

Suprabhat Kumar
Suprabhat Kumar

Reputation: 685

This worked for me -

position: fixed;

top: 0;

Upvotes: -1

JulioTrujilloH
JulioTrujilloH

Reputation: 11

This works for React components too:

position: sticky;
top: 0

Upvotes: 0

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

Pure CSS will be faster

Upvotes: 23

Prakash Karena
Prakash Karena

Reputation: 2605

make these changes in your CSS

.navbar {
      position: fixed;
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 0.7rem 2rem;
      overflow: hidden;
      z-index: 1;
      width: 100%; 
      margin: auto;
      top: 0;
      border-bottom: solid 1px var(--primary-color);
      opacity: 0.9;

      position: fixed;
      top: 0;

      /* box-shadow: 0 2px 4px -1px rgba(0, 0, 0, 0.25); */
      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);
    }

.conMain {
  height: 130vh;
  margin-top: 100px; //you need to add margin for your layout main 
  container
  width: 100%;
  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);
}

I hope this will help you.

Upvotes: 2

Oleg Levin
Oleg Levin

Reputation: 3621

<Navbar sticky="top" />

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

Upvotes: 4

Related Questions