简体   繁体   中英

Header div stays at top, vertical scrolling div below with scrollbar only attached body

Here I'm using bootstrap class for the sticky top header.

header.js

<header className="header sticky-top">HEADER</header>

stylesheet

header {
  overflow: hidden;
  padding: 20px 10px;
  display: inline-flex;
  width: 100%;
  height: fit-content;
  background-color: blue;
}

body {
  margin: 0;
  height: 100vh;
  overflow:auto;
  scroll-behavior: smooth;
}

This is what I wanted to achive.

______________________
|_______header_______|
|                  |*|
|   Container Div  |*|
|                  |*|
|                  |*|
|                  |*|
|                  |*|
|                  |*|
----------------------

* = scrollbar

This is the example project to work on demo code sandbox

You need to import the header component into your app like:

import Header from "./pages/Header";

and actually use it in the JSX:

function App() {
  return (
    <div className="App">
      <Header></Header>

Also to see this in actin the content needs scroll:

编辑 React 引导演示(分叉)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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