"Learn how to position a div element below the header div and above the footer div while maintaining full height in

Recently delving into the world of reactjs, I find myself facing a challenge with a page that contains 3 distinct blocks formed by divs.

Have a look at the layout on my page: My Page

This is the code snippet I have worked on:

  return (
    <div>
     {/* First Block */}
        <div style={{ padding: "0.8%", backgroundColor:"red" }}>
            Block 1
        </div>
{/* Second Block */}
        <div style={{ height: "40%", overflowY: 'scroll', backgroundColor: "aqua", position: "fixed", width: "100%" }} >
       Block 2
        </div>

    {/* Third Block */}

      <div style={{ display: "flex", position: "fixed", bottom: 0, right: 0, left: 0, height: "40px", backgroundColor: "grey", }}>

           Block3
      </div>

    </div>
  );

Block 1 acts as the header block, while Block 2 serves as the content block and Block 3 functions as the footer block. I successfully managed to fix Block 1 at the top and Block 3 at the bottom. My current dilemma revolves around getting Block 2 to fit seamlessly in the middle with full-height responsiveness across various screen sizes. Although I tried setting manual heights for Block 2, adapting it to different screen sizes proved to be cumbersome. After seeking guidance from Stackoverflow without much success, I'm reaching out here for potential solutions.

The desired output appearance I am striving for can be viewed here: Output Screen

Answer №1

Check out this new layout suggestion. The header and footer are fixed, while only the center content is scrollable.

<div style={{ display:"flex", backgroundColor: "green", height: "100vh" , flexDirection:"column", flex:1}}>
      {/* First Block */}
      <div  style={{backgroundColor:"red",  width: "100%"}}>
        Block 1
      </div>
      {/* Second Block */}
      <div style= {{background: "pink",display:"block", overflow: "auto", flex:1}}>
        Block 2
        An essay is, generally, a piece of writing that gives the author's own argument, but the definition is vague, overlapping with those of a letter, a paper, an article, a pamphlet, and a short story. Essays have traditionally been sub-classified as formal and informal. Formal essays are characterized by "serious purpose, dignity, logical organization, length," whereas the informal essay is characterized by "the personal element (self-revelation, individual tastes and experiences, confidential manner), humor, graceful style, rambling structure, unconventionality or novelty of theme," etc.[...]
        
      </div>

      <div  style = {{marginTop:"auto", backgroundColor:"grey"}}>

        Block3
      </div>

    </div>

Answer №2

To establish a header footer body combination, follow these steps:

<!DOCTYPE html>
<html lang="en">
<head>
<title>CSS Template</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {
  box-sizing: border-box;
}

body {
margin:0;
  font-family: Arial, Helvetica, sans-serif;
}

/* Design the header */
header {
  background-color: #666;
  padding: 30px;
  text-align: center;
  font-size: 35px;
  color: white;
}

/* Establish two columns/boxes that float alongside each other */
nav {
  float: left;
  width: 30%;
  height: 300px; /* for display purposes only, consider removal */
  background: #ccc;
  padding: 20px;
}

/* Style the list within the menu */
nav ul {
  list-style-type: none;
  padding: 0;
}

article {
  float: left;
  padding: 20px;
  width: 70%;
  background-color: #f1f1f1;
  height: 300px; /* for display purposes only, consider removal */
}

/* Clear floating elements after the columns */
section::after {
  content: "";
  display: table;
  clear: both;
}

/* Customize the footer */
footer {
  background-color: #777;
  padding: 10px;
  text-align: center;
  color: white;
}

/* Responsive design - stacks two columns/boxes on top of each other instead of next to each other on smaller screens */
@media (max-width: 600px) {
  nav, article {
    width: 100%;
    height: auto;
  }
}
</style>
</head>
<body>

 
<header>
  <h2>Cities</h2>
</header>

<section>
  <nav>
    <ul>
      <li><a href="#">London</a></li>
      <li><a href="#">Paris</a></li>
      <li><a href="#">Tokyo</a></li>
    </ul>
  </nav>
  
  <article>
    <h1>London</h1>
    <p>London is the capital city of England. It is the most populous city in the  United Kingdom, with a metropolitan area of over 13 million inhabitants.</p>
    <p>Standing on the River Thames, London has been a major settlement for two millennia, its history going back to its founding by the Romans, who named it Londinium.</p>
  </article>
</section>

<footer>
  <p>Footer</p>
</footer>

</body>
</html>

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

Is there a way to resolve the issue of my localStorage getting overridden on page refresh?

I am currently working on a simple React app and encountering an issue with the localStorage. Whenever I refresh the page, the todo list stored in the localStorage disappears. How can I ensure that the todos remain visible even after a refresh? Any sugges ...

When using JQuery Validation on a small screen, error messages can cause the button to shift downwards

After successfully creating an email form with jquery validation error style that appears when there's an error, I encountered a problem. When the screen width is reduced or viewed on a small screen, the error message pushes down the button. I&apos ...

Retrieve the Content-Type header response from the XHR request

My intention is to check the header content type and see if it is text/html or text/xml. If it is text/html, then it indicates an error that I need to address before moving forward. ...

JavaScript file encountering a Typescript issue with a property defined in a subclass

Currently, I am utilizing Typescript to validate my Javascript files. One issue I have encountered is that when I inherit from a class, Typescript does not recognize the types of the properties in the parent class. I am unsure if I am overlooking something ...

Effective Ways to Transfer Input Parameters to Karate File using npm

I am working on a test script that runs a series of queries. In order to execute the file, I need specific inputs such as URL and authorization header, which will vary depending on the environment. How can I prompt the user for these inputs in the command ...

Browsing the web with Internet Explorer and uploading images

I am facing an issue with a large form that contains over 100 submit buttons. The code I am currently using is: <input type='image' src='../images/add.png' name='FormDuplicate' value='" . $resInvoices['iProjectID ...

Achieving Vertical Alignment of ListItems in React Using Material UI

I am facing an issue with aligning ListItems (ListItemIcon and ListItemText) on a Material UI mini Drawer in vertical mode. The ListItemText is not aligning properly with the ListItemIcons. Below are my styles: link: { textDecoration: "none", " ...

The useEffect function continues to fire twice even when the dependency array is empty

I found a helpful example at https://github.com/vercel/next.js/blob/canary/examples/with-firebase-authentication/utils/auth/useUser.js Although the effect is working fine and firing once, the functions inside are being called twice for some reason. us ...

Rendering a ImageBitMap/Image on an OffScreenCanvas using a web-worker

In my vue.js application, I have implemented vue-workers/web-workers to handle image processing tasks such as scaling and cropping in the background after a user uploads images. Due to the lack of support for Canvas and Image Object in web workers, I opte ...

Load Bootstrap CSS file externally on a website dynamically

Although my knowledge of JavaScript is limited, I am the recipient of a small web application from a friend that is being utilized by multiple companies. As each company requires specific modifications in the appearance and CSS of certain webpages, it can ...

Filtering elements upon loading

I have successfully implemented data filtering on my website using data-filter. While everything is working great, I am interested in displaying items with specific attributes upon loading. For instance, I want to load only green objects by default inste ...

I need to optimize my function so that it is only invoked when a user clicks on a row in the datagrid, not for

After implementing this function: const DataEdit = ({id}) => { console.log(id) } I also added a column inside the DataGrid (imported from @miu/x-data-grid) with the following configuration: field:'Delete', headerName:'Delete& ...

Prevent the SVG mouseclick event with CSS styling

Is it possible to prevent clicking on an SVG element using CSS? .disabled{ pointer-events: none; } After selecting the SVG element with $(".dasvg"), the console confirms the selection: [ <svg width=​"500" height=​"500" class=​"dasvg">​ ...

Certain scripts fail to load properly when using Internet Explorer

The code snippet provided only seems to be functional in Firefox, where it displays all alerts and successfully executes the displayUserLanding function. However, in Internet Explorer, the browser appears to only execute the following alert: alert("Helper ...

Is it possible for an Android webview to access the device's geo location?

For the application I'm working on, it's necessary to access the user's geo location. We've created a HTML 5 based website and are using a webview to develop our mobile application by calling the website URL. With the latest geo tags av ...

The SyntaxError message indicates that there was an unexpected non-whitespace character found after the JSON data when parsing it

I received an Error message: SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data Here is the code snippet: <script> $(document).ready(function () { $('.edit1').on('change', function () { ...

Is the output returning before the AJAX call is completed?

Similar Question: How can I get the AJAX response text? When calling a JavaScript method that sends an Ajax request and receives a response in a callback function labeled "success," sometimes the JavaScript method returns a result as "undefined" inste ...

I am in the process of developing a React application, and when I try to run "npm start," I encounter this particular error

https://i.stack.imgur.com/UPQlu.jpg An issue has been encountered where npm start is not functioning properly, displaying an error message. Furthermore, the system also identifies vulnerabilities with a total of 82 moderate and 4 high vulnerabilities. ...

Generating dynamic variable names in JavaScript

Looking for a similar solution. var item_<?php echo $variable->n ?> = <?php echo '32' ?> Trying to achieve something like this: var item_342 = '32' ...

Leveraging Material-UI's customized theme in tandem with Redux

Currently, I have successfully integrated Material-ui and Redux into my project. However, I am faced with the challenge of using connect for higher order components in Redux: export default connect(mapStateToProps)(ComponentName); Similar to Redux, the M ...