Guide to creating a scrollable container that occupies the remaining height of the screen

Looking to create a fixed container that is scrollable, while keeping the rest of the page fixed? Check out this Stackblitz example. Here's a component called "PageDefault" that includes a Header and wraps each page in the application (the content of the page is passed as the prop "children" inside PageDefault's component).

Desired layout:

Minimal Example (without react):

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .header {
        top: 0px;
        background-color: green;
        height: 100px;
      }

      .page-title {
        height: 300px;
        background-color: blue;
      }

      .content {
        position: fixed;
        height: 100%;
        overflow-y: scroll;
      }
    </style>
  </head>
  <body>
    <div class="header">header goes here</div>
    <div class="page-title">page title goes here</div>

    <div class="content">
      blablabla blablabla blabla bla blabla blablabla bla blabla bla blablabla
      blabla bla blabla blablabla blabla bla blabla bla bla blablabla blablabla
      blabla bla blabla bla blabla bla blabla bla blablabla blablabla blabla bla
      blabla blablabla bla blabla bla blablabla blabla bla blabla blablabla
      blabla bla blabla bla bla blablabla blablabla blabla bla blabla bla blabla
      bla blabla bla blablabla blablabla blabla bla blabla blablabla bla blabla
    </div>
  </body>
</html>

Answer №1

Utilizing Flexbox for layout:

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

::before,
::after {
  box-sizing: inherit;
}

.wrap {
  width: 90%;
  margin: auto;
  height: 100vh;
  max-height: 100vh;
  display: flex;
  flex-direction: column;
}

header {
  background: orange;
  padding: 1em;
}

h1 {
  background: lightblue;
  margin: 0;
}

main {
  flex: 1;
  background: pink;
  overflow: auto;
}

p {
  height: 1000px;
}
<div class="wrap">
  <header>HEADER</header>
  <h1>TITLE</h1>
  <main>
    <p></p>
  </main>
</div>

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

Trouble with Title Updating in Next.js and Tailwind CSS Project

I am currently facing an issue with the title of my website not updating, even though I am using next/Head and have included the title tag. "use client"; import Head from 'next/head'; import { BsFillMoonStarsFill } from 'react-ico ...

The div inside an iframe is not displaying the desired background color as intended from the parent page

The code snippet below is included in the <head> section of the main page: <script type="text/javascript"> $(document).ready(function () { $('#innerframe').load(function () { $(this).contents().find($(".TitleB ...

Troubleshooting the issue with generateStaticParams() in NextJs/TypeScript

My NextJs app has a products page that should render dynamic routes statically using generateStaticParams(). However, this functionality does not work as expected. When I run "npm run build," it only generates 3 static pages instead of the expected number. ...

Incorporating a YouTube video

Having trouble integrating a YouTube video that won't play automatically on your website? The video is visible, but just doesn't start playing on its own. What could be causing this issue? Appreciate any help you can provide! ...

Is it possible to have multiple ReactDOM.render calls in a single project

Currently, I am in the process of learning react and encountering an issue when trying to call multiple ReactDOM.render functions: Here is my code using React: class Header extends React.Component{ render(){ return( <div> <p&g ...

How can lazy loading of tabs be disabled in React Material UI?

I am currently working on implementing react tabs using the example provided on Material UI's website: https://material-ui.com/components/tabs/. However, I am facing an issue where my tabs are lazily loading the linegraph components instead of execut ...

Tips on positioning the image to the left of your content instead of above it

Currently, I am following a tutorial on Flask and attempting to include the profile picture in each article displayed on the website. Here is the code snippet used to display an article: <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a ...

How to Set a Button as Inline Text in a React Native Component

Below is the code snippet I am working with utilizing the Text and Button components provided by react-native-paper: <Text>See also </Text> <Button mode="text" compact onPress={this.nav( name )}>Compass</Button> <Text&g ...

Using ReactJs Component to interact with Laravel Api and retrieve uploaded images

I need assistance retrieving my list from the Laravel API. I have successfully fetched the list details, but I am encountering difficulties fetching details with images. My card.js component import React, {useState, useEffect} from 'react'; imp ...

Implementing CSS and HTML in Email Communication

I'm noticing a difference between how this appears in a browser versus an email. The text on the image looks perfect when viewed on Mozilla/Chrome, but when I use the same code in an email, the text ends up displaced below the image (right beneath it) ...

Is there a way to modify the variable in order to switch the font of the heading every second using CSS and JavaScript?

I'm trying to create a heading that changes fonts every second, but I'm having trouble changing the variable to set it to another font. Check out my code here. Despite watching tutorials, everything seemed too confusing. var root = document.q ...

space allocated beneath a table cell

I am currently utilizing the foundation framework for emails and have encountered a formatting issue on my Test Page. There seems to be an unwanted whitespace just below the racoon image that I am struggling to remove. Despite setting the height to 250px, ...

The styles applied to the Angular 5 Component host element are not being reflected correctly in jsPlumb

As a beginner in Angular >2 development, I am excited to build my first application from scratch. One of the features I'm trying to implement is drawing flow charts using jsPlumb. However, I have encountered an issue where the connectors are not being ...

Can anyone provide guidance on locating the parent of a pseudo element with Selenium WebDriver?

Is there a way to retrieve the parent web element of a pseudo element (if we can call it the parent) using JavaScript? I know that Selenium's methods for searching for web elements are not suitable for pseudo elements, but JavaScript does allow manipu ...

Using webpack 4 and create-react-app to implement Content Security Policy and new Function functionality

It seems that webpack 4 is injecting code like the following into the bundle: try { n = n || new Function("return this")(); } catch (e) { "object" == typeof window && (n = window); } Due to our strict se ...

Troubleshooting issue with jquery.backgroundSize.js on Internet Explorer 8

After trying out the jquery.backgroundSize.js plugin, I ran into issues getting it to work in IE8. I followed the steps from the official website and tested on various browsers like IE9 and Firefox, but nothing seemed to happen on IE8. css: #main{ b ...

Error: The type 'boolean | (() => void)' cannot be assigned to type 'MouseEventHandler<HTMLButtonElement> | undefined'

Playing audio in a NextJS app while writing code in TypeScript has been an interesting challenge. The onClick() function performs well in the development environment, triggered by npm run dev. <button onClick ={toggle}> {playing ? "Pause" : ...

Error encountered while deploying NodeJS application on Heroku platform

I encountered some errors while trying to deploy my project to herokuapp: 2018-07-27T08:27:30.827410+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/robots.txt" host=ihealth99.herokuapp.com request_id=54687736-9467-4484-93d6 ...

"Customizing the color of the arrow in Bootstrap tooltips

Trying to customize tooltips in twitter-bootstrap, I set the template option of the tooltip with inline styles to achieve red tooltips instead of the default black color: $(document).ready(function(){ options = { container: 'body&ap ...

Choosing a single item from multiple elements in React using React and typescript

In this particular project, React, TypeScript, and ant design have been utilized. Within a specific section of the project, only one box out of three options should be selected. Despite implementing useState and toggle functionalities, all boxes end up bei ...