Stop the entire page from scrolling in Next JS, while still enabling individual components to scroll

My ultimate goal is to have a fixed, sticky menu bar at the top of my app, followed by a div/component that houses the remaining content and does not scroll itself, while still allowing sub-components the ability to scroll when necessary. I plan to build this functionality in Next JS, but I am struggling to make it work even with plain HTML/CSS. I suspect that I need to apply styles to the outermost <body> tag, but my attempts so far have been unsuccessful. I also anticipate needing to override the default Document setup as outlined in the Next documentation in order to apply styles to the <body> element. But for now, I am focusing on getting it to work in plain HTML...

If I were to write this in bad, incorrect pseudocode, it would look something like this:

<html>
  <nav style="sticky to top">
    <!-- Menu markup goes here. -->
  </nav>
  <body style="don't scroll; take up remaining page space">
    <table style="overflow-y: auto">
      <!-- TONS OF TABLE ROWS. -->
    </table>
  </body>
</html>

I tried applying overflow: hidden to various elements in a brute force attempt, but still couldn't get it to work.

I came across a helpful post on Stack Overflow, as well as a related thread, but the solutions didn't quite fit my scenario.

Here is some simple HTML that I was experimenting with (ignoring the sticky nav bar part):

<html>
  <nav>
    <div style="background-color: purple;">
      <p>Navigation Bar</p>
    </div>
  </nav>
  <body style="overflow:hidden">
    <div style="overflow: scroll">
      <table style="color: red; overflow:scroll">
        <tr><td>Item 1</td></tr>
        <!-- Repeat the above rows multiple times. -->
      </table>
    </div>
  </body>
  <footer>
    <p>Footer Content Here</p>
  </footer>
</html>

Below is the base layout for the main page using Next/React and Tailwind CSS:

const AppLayout = ({ children }) => (
  <>
    <header className = "sticky top-0 z-50">
      <Menu />
    </header>
    <main id = "AppLayoutMain" className="relative bg-white antialiased overflow-hidden">
      {children}
    </main>
  </>
);

I have tried adding styles to random elements in a disorganized manner to try and make it work, but I haven't been successful. Any guidance or a simple working example would be greatly appreciated. Feel free to use Tailwind CSS if it helps demonstrate the solution.

Answer №1

To achieve this, utilize the flex-col class and apply overflow-hidden to your content div. Here is an example:

<div class="flex flex-col h-screen overflow-hidden">
  <div class="flex-shrink-0">Header content</div>
  <div class="overflow-hidden">
    <div class="w-40 max-h-full overflow-y-auto">
      Scrolling content
      ...
    </div>
  </div>
  <div class="flex-shrink-0">Footer Content</div>
</div>

View the demonstration here: https://play.tailwindcss.com/bxUwAuCs8R (resize the screen vertically to observe the scroll bar).

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue with Domsanitizer bypasssecuritytruststyle functionality on Internet Explorer 11 not resolving

CSS:- Implementing the style property from modalStyle in TypeScript <div class="modal" tabindex="1000" [style]="modalStyle" > Angular Component:- Using DomSanitizer to adjust height, display, and min-height properties. While this configuration work ...

Activate background functionality while modal is active in Bootstrap 4

I have come across similar questions addressing the need to change the following: .modal-backdrop { display: none; } However, my modal does not have this .modal-backdrop. I am unsure if this is a new feature in Bootstrap 4, but the mentioned solution ...

What is the best way to return to the homepage if the query parameter is not specified?

I am working on a search website using Next.js. The home page is located at / and the search page is at /search. Users can search by entering the query in the URL as /search?query=myterm. If a user tries to access the /search page without specifying a qu ...

The stacking order of a child element within a parent element, which has a transform

Hey there, I've encountered a problem and was wondering if you could assist me with it. In the code snippet provided below, you'll see that there are elements with: transform: translate(0,0); Within these elements, there is a "dropdown" elemen ...

Whenever there is a click event triggered within the map function, it will affect every element within the collection

I am struggling to make changes to a specific item in the map function when clicked. Can someone assist me with achieving this functionality? const Product = ({categories}) => { const [active,setActive] = useState(true) function activeCat ...

Implementing a dual-column design for MailChimp using HTML and CSS

I'm feeling overwhelmed. I have a MailChimp layout that I exported from MailChimp. Currently, the layout consists of 3 columns, but on mobile, it converts to 1 column. Here is the link to the codepen: goo.gl/Fj8Ct7 Is there anyone who can assist me ...

How does conditional rending of function components in react affect the positioning of my css elements?

I'm in the process of creating a website as part of a project, where I have set up a vertical navigation bar next to the main content area. Utilizing bootstrap, I divided the layout into two main columns - one for the navigation bar and the other for ...

Trouble arises with the background of the HTML body

There seems to be a mysterious white strip appearing when I use the (google custom sesrch) search results rendering tag gcse:searchresults-only If I remove cse id from var cx or delete the above tag, everything works perfectly and the white stripe disappe ...

Styling nested lists with CSS

Looking to implement CSS to style a nested list like the one below: 1. item1 subitem subitem 2. item2 subitem subitem I am trying to customize the appearance of the numbers (either in bold or red color). Despite searching online, the solutio ...

Utilize Bootstrap and Flat UI to enhance the design of a div, or duplicate the styles to apply to the navbar

My navigation bar was created using Bootstrap and Flat UI on top of it. However, when I try to incorporate bootstrap+flat ui into another HTML+CSS file that I have, the hexagons end up looking distorted. It seems like my navigation bar only works with flat ...

Adding margin to an HTML element causes the entire page to shift downward

Despite successfully applying margin to other elements, I encountered an issue with #searchbarcontainer where it causes the entire page to shift downward. My goal is to center it slightly below the middle of the page, but I'm struggling to find a solu ...

What is the best way to navigate from a button in NextJS to another page that I have built within the same application?

As I work on developing a website for a pet rescue charity using Next.js, I am facing an issue with getting my button or link to function correctly. Every time I try to navigate to another page within my Next.js app, I encounter a 404 error stating "This p ...

Responsive CSS - reordering div elements

#div1 { position: relative; max-width: 100%; height: 100px; background-color: green; color: white; } #div2 { position: relative; max-width: 100%; height: 100px; background- ...

Encountering a build issue with aws-sdk on Next.js platform

I encountered a build error while working on my project. As a beginner, I am not entirely sure how to resolve this issue. This is my first attempt at creating a build. > next build ▲ Next.js 14.2.3 - Environments: .env Creating an optimized p ...

Customize the appearance of the search box in Serverside Datatables

Is there a way to adjust the search and show inputs for datatables so that I can customize their width and make them fit my dynamic column sizing? The issue I ran into is that these inputs are enclosed within <label> tags in the source JavaScript, an ...

Image not showing correctly after closing the bootstrap modal

I am currently working on creating a gallery with hover effects for each image, similar to the one found here. My goal is to have a Bootstrap modal open when I click on a link within the hovered image, displaying additional content. Everything seems to be ...

Error encountered: The JSONP request to https://admin.typeform.com/app/embed/ID?jsoncallback=? was unsuccessful

I encountered an issue with the following error message: Error: JSONP request to https://admin.typeform.com/app/embed/id?jsoncallback=? Failed while trying to integrate a typeform into my next.js application. To embed the form, I am utilizing the react-ty ...

Integrating node.js into my HTML page

Forgive me for sounding like a newbie, but is there a simple way to integrate node.js into my HTML or perhaps include a Google API library? For example: <script>google.load(xxxx)</script> **or** <script src="xxxx"></script> ...

Mapping variables to attributes in polymers

I'm working with a polymer element that looks like this: <polymer-element name="foo" attributes="bar"> <template> <core-ajax url="url.php" params="{{ {last: bar} }}"></core-ajax> {{bar}} </template> </polymer-element& ...

Changing the color of placeholder text in MUI 5 TextField

Looking to customize the text color and placeholder text color in my MUI TextField component to be green https://i.sstatic.net/NZmsi.png The documentation doesn't provide clear instructions, so I attempted a solution that didn't work: <TextF ...