Tips to ensure the div remains 100vh in height, even when empty

As I work on a simple webpage with a header, body, and footer, it's essential for me to ensure that the content takes up the entire height of the page using 100vh. However, if I only have plain text in the body section without any child tags, it ends up looking like this:

https://i.sstatic.net/WMwkf.png

This presents an issue as I would need to manually add height: 100%; width: 100%; to the styles of each child element to utilize the full height of its parent. For instance, if I pass a

<div className=...>...</div>
as the child.

Shown below is the code snippet for page.tsx:

import Footer from '@/components/Page/Footer'
import Header from '@/components/Page/Header'

import styles from './index.module.css'

interface PageProps {
  children: any
}

function Page({ children }: PageProps) {
  return (
    <div className={styles.page}>
      <Header />
      {children}
      <Footer />
    </div>
  )
}

export default Page

I've already defined the style for .page as follows:

.page {
  display: flex;
  flex-direction: column;
  height: 100vh;
  width: 100vw;
}

Answer №1

If you're looking to expand the children content within its parent container using display: flex, then it's a great opportunity to utilize the flex-grow property.

Give this a shot:

import Footer from '@/components/Page/Footer'
import Header from '@/components/Page/Header'

import styles from './index.module.css'

interface PageProps {
  children: any
}

function Page({ children }: PageProps) {
  return (
    <div className={styles.page}>
      <Header />
        <div className={styles.childrenContainer}>
          {children}
        </div>
      <Footer />
    </div>
  )
}

export default Page
.page {
  display: flex;
  flex-direction: column;
  height: 100vh;
  width: 100vw;
}

.childrenContainer{
  flex-grow: 1;
}

Answer №2

To fix this issue, try adding justify-content: space-between; to the main CSS class and use margin-bottom:auto to make sure your content stays aligned with the header.

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

Prevent Bootstrap 5 input fields from automatically adjusting to the height of a CSS grid cell

Below is some Bootstrap 5 markup that I need help with. The first example is incorrect. I don't want the Bootstrap input to be the same height as the grid cell. The second example is right, but I want to achieve the same result without using a wrapp ...

I am attempting to create a form in NodeJs to search for a user in MongoDB by their telephone number using query routing. However, I am puzzled as to why this is not functioning properly

Can you identify the issue in this code? I am able to retrieve the correct mobile number on the console, but it is not being passed to the query routing on /search_user?mob. <input type="tel" name="message" id="mobile_input&qu ...

Choose the label by utilizing the CSS selector

I am currently using CSS to create tabs with radio buttons. However, I am struggling to figure out how to select the corresponding <label> for the radio button. To keep the labels separate from the content and display them as tabs, I have structured ...

Storing information in Firebase using React.js

When storing an object in Firebase, I expected the structure to be as shown in the image below. However, what I received was a generated running number as a key. This is the code I used to store the object in Firebase: var location = []; location.push({ ...

Generating HTML table rows dynamically in Angular based on the number of items stored in a $scope variable

In my current project, I am utilizing Angular to dynamically populate data in an HTML table. Instead of manually coding each row for display, I am in need of a solution that allows me to programmatically define each HTML row. The Angular controller snippet ...

Error: The context does not have a definition for React

import './App.css'; import ComponentC from './components/ComponentC'; export const UserContext = React.createContext() function App() { return ( <div className="App"> <UserContext.Provider value={& ...

What is causing the continuous appearance of null in the console log?

As part of my JavaScript code, I am creating an input element that will be inserted into a div with the id "scripts" in the HTML. Initially, I add a value to this input field using JavaScript, and later I try to retrieve this value in another JavaScript fu ...

Creating a stationary div element solely with Javascript, excluding the use of CSS, jQuery, and HTML

I've been searching all day for a solution to this issue without any success. I apologize if I overlooked any relevant posts on the topic. Currently, I am working with Qualtrics and attempting to implement a textbox with scrolling instructions that fo ...

The integration of AsyncStorage with the web application is facing challenges

We are in the process of developing an app using react-native, where we retrieve data such as login/signup through our custom X-sdk. The X-sdk acts as a middleware/api fetcher that interacts with the backend api to obtain necessary information. In order to ...

Unable to modify the border-radius property of Material UI DatePicker

I'm having difficulties setting rounded borders for my DatePicker component from @mui/x-date-pickers and Material UI V5. Here is the intended look I am aiming for: https://i.stack.imgur.com/c1T8b.png I've tried using styled components from Mat ...

Upon making the initial post request, axios yielded a 404 error code

function checkStudentGrades(e) { e.preventDefault() setSubject(String(responseProfessor[1])) axios .post(`http://localhost:3001/getNotasAluno/${subject}`, { studentSearch }) .then((response) => { ...

When the navbar is expanded, a right-aligned image is visible; however, it becomes invisible when the

My website has a navigation bar with two logos - one in the left corner (the coat of arms) and one in the right corner (the scout lily), along with text links in between. The coat of arms uses the "navbar-brand" class. To see how it looks, click here: htt ...

"Encountering a runtime permission issue with the React Native SDK

When using React Native on an Android device, you may encounter an error stating that the new target SDK 22 does not support runtime permissions. However, the old target SDK 24 does allow for this feature. How can you resolve this issue? Error Image ...

Using jQuery to apply a CSS class to a chosen radio button

How can I dynamically add a CSS class to a radio button based on its selection? $("input[type=radio][name=align]").on('change', function() { var radVal = $("input[type=radio][name=align]").val(); if (radVal == 'center') { $(" ...

Personalized Bootstrap 4 grid system

Could someone guide me on how to create this grid layout with Bootstrap 4? I am trying to achieve a design where one column stretches from the edge of the window to the inside, while the other column is half the width of a standard .container element. ht ...

Using jquery to refresh several backgrounds at once

While I have some knowledge of CSS and various JavaScript libraries, I've encountered difficulty in integrating multiple backgrounds into the code that I found on this link: http://codepen.io/quasimondo/pen/lDdrF. My goal was to overlay a transparent ...

Tips for effectively displaying elements on a page

After making changes to my css/html code, all the elements within a div are now displayed in a single line instead of appearing as separate <p>contents</p> tags with each on a new line. An example of this issue can be seen here: Dealing with C ...

Retrieve the current row by clicking the button

I've been struggling to retrieve a specific value from a row displayed on my PHP webpage. My objective is to obtain the current value from a particular cell in the row, but using getElementsByTagName has not been successful as it only returns an HTMLC ...

The SVG image does not display in browsers other than Internet Explorer (IE)

I am encountering an issue with adding a menu toggle image in SVG format to my HTML. Unfortunately, the image is not displaying as expected. Here are screenshots for comparison between Explorer and Chrome: https://i.stack.imgur.com/74sqh.png https://i. ...

Adding space between the heading and the text underneath by placing a margin between h2

I am dealing with a situation where I have an <h2 class="landing-panel-title> containing a <span class="landing-panel-icon-right ion-ios-search-strong>. The second class on the span is from an icon font called ionicons. Despite this, it may not ...