Tips for maximizing the full height of your webpage:1. Utilize

I'm having trouble utilizing the full height of my webpage. It seems to only be using about 40% of the page's height. I tried setting the HTML and body heights to 100%, but that didn't work. I then attempted to allocate 20% to the header, 70% to the section, and 10% to the footer. However, I realized that this would cause my text to not be vertically centered. How can I achieve the desired layout?

<style>
    * {
        box-sizing: border-box;
        border: 0;
    }
    
    html, body {
        height: 100%;
    }

    header{
        background-color: blue;
        padding: 30px 0;
        text-align: center;
        font-size: 30px;
    }
     
    section {
        display: flex;
    }
   
    nav, article {
        border: solid 1px black;
        clear: both;
        background-color: aqua;
    }

    nav {
        flex: 1;
        padding: 20px;
        background-color: darkslateblue;
    }

    article {
        text-align: center;
        padding: 10px;
        flex: 4;
    }

    footer {
        border: solid 1px black;
        background-color: crimson;
        padding: 10px;
        text-align: center;
    }
</style>
</head>
<body>

<header>hello!</header>
<section>
    <nav>
        <ul>
            <li><a href="">1</a></li>
            <li><a href="">2</a></li>
            <li><a href="">3</a></li>
            <li><a href="">4</a></li>
        </ul>
    </nav>
    <article>
        <h2>hello</h2>
        <p>hello how are you?</p>
    </article>
</section>
<footer>this is footer</footer>

Answer №1

To optimize your layout, consider utilizing the vh relative unit, which represents Viewport Height and can be implemented as shown below:

html,
body {
  height: 100vh;
}

By setting the height to 100% of the viewport height, you are instructing the browser to utilize the entire available vertical space. Additionally, there is also a similar unit called vw that controls width in relation to the viewport size.

For more information on relative units, refer to the informative resources provided on the MDN page.

A recommended approach for constructing your layout is to leverage CSS Grid Layout. Integrate this with 100vh to establish a full-height layout and then segment elements by creating a grid structure.

Examining the sample below, pay close attention to the CSS section. Here, you will observe the utilization of grid-template-rows to specify three rows with heights of 20%, 70%, and 10% relative to the total 100vh.

Using grid-template-areas, you can assign names to rows and columns within the grid. Subsequently, the children elements must be positioned within the grid according to these defined areas by referring to their respective cell names (refer to grid-area in the CSS).

body {
  margin: 0;
}

.grid {
  display: grid;
  grid-template-rows: 20% 70% 10%;
  grid-template-columns: 1fr;
  grid-template-areas:
    "header"
    "main"
    "footer";
  height: 100vh;
}

.grid-header {
  grid-area: header;
  background-color: blue;
}

.grid-main {
  grid-area: main;
  background-color: aqua;
}

.grid-footer {
  grid-area: footer;
  background-color: crimson;
}
<body class="grid">
  <header class="grid-header"></header>
  <main class="grid-main"></main>
  <footer class="grid-footer"></footer>
</body>

Alternatively, you have the option to partition your primary sections into segments of 20vh, 70vh, and 10vh respectively.

Answer №3

It seems like using a wrapper could be the solution! Check out the updated code below:

<style>
  * {
      box-sizing: border-box;
      border: 0;
  }
  
  .wrapper {
      display: flex;
      height:100%;
      flex-direction: column;
  }

.wrapper > *{
  display: flex;
  flex:1;
}
  header{
      background-color: blue;
      padding: 30px 0;
      text-align: center;
      font-size: 30px;
  }
   
  nav, article {
      border: solid 1px black;
      clear: both;
      background-color: aqua;
  }

  nav {
      flex: 1;
      padding: 20px;
      background-color: darkslateblue;
  }

  article {
      text-align: center;
      padding: 10px;
      flex: 4;
  }

  footer {
      border: solid 1px black;
      background-color: crimson;
      padding: 10px;
      
      text-align: center;
  }
</style>
</head>
<body>
<div class="wrapper">
<header>hello!</header>
<section>
  <nav>
      <ul>
          <li><a href="">1</a></li>
          <li><a href="">2</a></li>
          <li><a href="">3</a></li>
          <li><a href="">4</a></li>
      </ul>
  </nav>
  <article>
      <h2>hello</h2>
      <p>hello how are you?</p>
  </article>
</section>
<footer>this is footer</footer>
</div>
</body>

Answer №4

Specify the heights of the header, section, and footer using height: ...vh.

The abbreviation "vh" stands for viewport height.

...
header {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 20vh;
    background-color: blue;
    padding: 30px 0;
    font-size: 30px;
}

section {
    height: 70vh;
    display: flex;  
 }

footer {
    height: 10vh;
    border: solid 1px black;
    background-color: crimson;
    padding: 10px;       
    text-align: center;
 }
...

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

Error: Compilation was unsuccessful due to module not found. Unable to resolve in ReactJS

As I was wrapping up this task, an unexpected error popped up: Module not found: Can't resolve './components/Post' in ./src/pages/index.js I've tried everything to troubleshoot it but no luck. Here's a rundown of my code snippets ...

Minimum and Maximum Price Input Field

Hello everyone, I'm currently facing an issue with a code that is supposed to filter with minimum and maximum price values, but it's not working as expected. Below is the HTML form snippet: <form> <select class="custom-select my ...

Placing a piece of code within a table without any empty spaces

When including a preformatted code fragment with multiple lines and indentation, the HTML element to use is <pre>. However, I've noticed that when I place this within a table, the browser automatically adds a blank line before and after the code ...

Creating a legitimate svg element using javascript

While working with SVG, I had an issue where I added a <rect> element directly into the svg using html, and then created a new element (without namespace) <circle> with javascript. However, the <circle> element did not display in the svg ...

Exploring the capabilities of SVG and audio integration in web browsers

I am curious if it's possible to incorporate audio into an SVG file in a web browser. I came across this thread here, but unfortunately, I can't leave a comment =/ I tried implementing the code from this website, but it doesn't seem to work ...

Only one instance of a div with a table cell will function properly

I am facing an issue where I created a div with display:table-cell; and it appears as shown in the first picture, which is what I want. However, when I add another div, it changes to look like the second picture. All I want is for the text to be in the mid ...

Sleek Navigation in CSS and HTML

Hello, as I work on my website with multiple pages, I am looking to implement smooth scrolling on a specific page without using the html tag for the entire site. Below is the code snippet I am working with: {% if section.settings.display_product_detail_des ...

Overriding the w-4xl with sm:text-2xl in Tailwind CSS

Struggling to achieve responsive design on my Pages, especially with changing text size when the screen is small. I've been following all the correct steps and maintaining the right order. However, whenever I set 'sm', it seems to override a ...

Reorder List Items in Collapsible Accordion in Bootstrap 4

I previously attempted to implement the solution provided atHow do I change Bootstrap 3 column order on mobile layout?, however, it only works with bootstrap3. I am looking to rearrange the order of the navbar when it is collapsed on a small screen. Here ...

The sorting icon cannot be substituted with jQuery, AJAX, or PHP

Currently, I am working on implementing "sort tables" using ajax, jquery, and PHP. The sorting function is functioning correctly; however, I need to show/hide the "sorting images". At the moment, only one-sided (descending) sorting is operational because I ...

When hovering over the alternating rows of a table, the shadow effect does not appear

I'm encountering an issue with CSS. I have a table and when hovering on the table's tr, there's a CSS property for box-shadow. The problem is that this box-shadow isn't fully visible for the even rows. https://i.sstatic.net/9uSjO.png ...

The link background color is malfunctioning

I'm having trouble getting the background color to show for the active link class. The border changes to dashed, but the color remains the same. Can anyone help me figure out what's going on? I'm new to CSS and feeling a bit frustrated. Also ...

Online platform showcasing delectable cookies

I am interested in creating a webpage that displays cookies from external websites. I believe PHP would be the most suitable option. How can I retrieve a cookie value from a site such as Facebook? My understanding of PHP is quite limited. When examining ...

Utilizing CSS grid in a repetitive manner without the need to specify additional grid areas

I'm experimenting with CSS grid to organize content within certain limitations. My goal is to have three divs, each 50% wide, with div two and three stacking next to div one. To accomplish this dynamically using PHP, I utilized grid-template-areas. ...

Discover the child of the delegated event div using jQuery

Can you assist me in resolving this issue? My HTML structure appears as follows: <div id="main-randompart"> <!--Inserted into the DOM during runtime--> <div class="close"> X </div> <img src="somesrc..."> ...

Display a unique button and apply a strike-through effect specifically for that individual list item

I've encountered an issue with my code that is causing problems specifically with nested LI elements under the targeted li element: $('#comment-section .comment-box a#un-do').hide(); $('#comment-section ul li[data-is-archived="true"]& ...

Rock Paper Scissors Debugged

Question I have been working on a rock paper scissors game. The functionality I have implemented so far includes: Allowing the player to select a choice The computer randomly selects a choice Changing the images representing the hands based on the choic ...

Implementing HTML rendering in a PyQt5 window

As a newcomer to PyQt5 for GUI creation, I could use some assistance. I'm attempting to create a basic window that displays a simple HTML file. However, despite using the code provided below, the window shows up empty. Is there a step I missed in ord ...

Creating a horizontal scrollbar in columns using Bootstrap can be accomplished by adjusting the CSS properties

Having a container created with boostrap: https://i.sstatic.net/bqAuf.png The question at hand is how to implement horizontal scrolling in order to maintain the aspect ratio of the initial image within the container on phone resolutions. Here is the con ...

Can the layout of my page be modified for mobile in a specific manner using the Bootstrap 4 grid system?

At the moment, I have my bootstrap grid structured like this <div class="container"> <div class="row"> <div class="col-4"> <div class="A"> A </div> </div ...