Attempting to scale the div element to a portion of the total page's width and height

Is there a way to make the height of div elements a percentage of the body height? I am currently using Bootstrap version 5.3 and have managed to set the body element to be 100% of the viewport. However, all other div elements are staying at a fixed size of 24px. My goal is to have these divs cover the entire height of the page. Despite trying various solutions found in different posts, none seem to work for me. Can you help point out what I might be missing? Below is the complete code snippet.

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ed8f8282999e999f8c9dadd8c3dfc3de">[email protected]</a>/dist/css/bootstrap.min.css" />
</head>
<style>
  html {
    height: 100%;
  }
  
  body {
    min-height: 100%;
    margin: 0;
    padding: 0;
  }
</style>

<body>
  <div class="container-fluid">
    <div class="row">
      <div class="col-12" style="height: 5%; background-color: blueviolet">
        header
      </div>
    </div>
    <div class="row" style="height: 86.5%; background-color: aliceblue">
      <div class="col-12">main</div>
    </div>
    <div class="row" style="height: 1.9%; background-color: green">
      <div class="col-12">thin banner</div>
    </div>
    <div class="row" style="height: 6.56%; background-color: darkslategray">
      <div class="col-12">footer</div>
    </div>
  </div>
</body>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b6d4d9d9c2c5c2c4d7c6f68398849885">[email protected]</a>/dist/js/bootstrap.bundle.min.js"></script>

</html>

Answer №1

To achieve full page coverage with divs, using flexbox is a simple solution.

I adjusted the heights to be evenly rounded for better visual appeal and easier understanding. Removing inline styles simplifies maintenance. Each .row now has a unique class for easier identification.

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="395b56564d4a4d4b5849790c170b170a">[email protected]</a>/dist/css/bootstrap.min.css" />
</head>
<style>
html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}

.container-fluid {
  display: flex;
  flex-flow: column;
  height: 100%;
}

.header {
  height: 10%;
  background-color: blueviolet
}

.main {
  height: 70%;
  background-color: aliceblue
}

.banner {
  height: 10%;
  background-color: green
}

.footer {
  height: 10%;
  background-color: darkslategray
}
</style>

<body>
  <div class="container-fluid">
    <div class="row header">
      <div class="col-12" style="">
        header
      </div>
    </div>
    <div class="row main" style="">
      <div class="col-12">main</div>
    </div>
    <div class="row banner" style="">
      <div class="col-12">thin banner</div>
    </div>
    <div class="row footer" style="">
      <div class="col-12">footer</div>
    </div>
  </div>
</body>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5f3d30302b2c2b2b13c1e0c2c313d39fff7faf8fdffd6dbffeddcd9">[email protected]</a>/dist/js/bootstrap.bundle.min.js"></script>

</html>

Both of these declarations ensure proper alignment:

html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}

.container-fluid {
  display: flex;
  flex-flow: column;
  height: 100%;
}

Note the adjustment in percentages for a total sum of 100%.

Answer №2

When using the vh units, heights are set relative to the viewport height. This allows elements to occupy a specific portion of the viewport height without explicitly defining the height of the parent elements.

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="385a57574c4b4c4a5948780d160a">[email protected]</a>/dist/css/bootstrap.min.css" />

<body>
  <div class="container-fluid">
    <div class="row" style="height: 8vh; background-color: blueviolet">
      <div class="col-12">header</div>
    </div>
    <div class="row" style="height: 80vh; background-color: aliceblue">
      <div class="col-12">main</div>
    </div>
    <div class="row" style="height: 4vh; background-color: green">
      <div class="col-12">thin banner</div>
    </div>
    <div class="row" style="height: 8vh; background-color: darkslategray">
      <div class="col-12">footer</div>
    </div>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="294b46465d5a5d5b4859691c071b071a">[email protected]</a>/dist/js/bootstrap.bundle.min.js"></script>
</body>

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

Make the most of your Bootstrap 3 layout by utilizing a full page container that fills both the width and height between a fixed header and

I am currently working on a basic bootstrap page with the Example template from Bootstrap's website. I want the content in the middle to take up the space between the header and footer, while ensuring that both the header and footer remain visible at ...

Adjust the checkmark color of MUI Checkbox

When using Material UI's MUI library for React, I encountered an issue with the checkbox checkmark color. By default, the checkmark takes on the background color of the container element, which is great for light backgrounds but not ideal for dark one ...

Is it possible to size a child div to be larger than its parent without using absolute positioning?

I'm trying to figure out a way to have one or two divs on a page that are larger than their parent div without having to overhaul the entire website structure. I originally considered using position absolute, but that would mess up the container heigh ...

JavaScript progress bar with extended duration, inspired by the success-oriented approach

Currently, I am in search of a unique solution or plugin that can replicate the same effect as Kickstarter's crowd fund long term progression bar. It seems like existing solutions only offer short term dynamic progress bars that indicate the percentag ...

Automatic keyboard suggestions not working for HTML search input using Ajax

I am currently working on a PHP web application that uses a MySql database. I have implemented a search suggestion box using ajax. The issue I am facing is that the suggestions can be selected with the mouse and auto-completed, but not when using the keybo ...

Tips on transitioning between two tables

Recently, I created an HTML page entirely in French. Now, I am attempting to incorporate a language translation feature on the website that allows users to switch between French and English (represented by two flag icons). My concept involves using a tabl ...

Having difficulty receiving a response from my code

On my website, I am utilizing jplayer as the audio player. The majority of the commands conform to the HTML5 standard. I have been attempting to trigger the event $.jPlayer.event.progress to capture its value. By checking if it is greater than zero, I can ...

Ways to constrain checkbox choices to only one within an HTML file as the checklist with the checkboxes is being created by JavaScript

I am working on developing an HTML dialogue box to serve as a settings page for my program. Within this settings page, users can create a list of salespeople that can be later selected from a drop-down menu. My current objective is to incorporate a checkbo ...

Angular 10: Unexpected Behavior with Observables

When I initially call addPost(), the observable behaves as expected with a 5-second delay. However, when I call it a second time, the data updates in the HTML without any delay. On the other hand, the same observable with deletePost() functions perfectly. ...

I am looking to eliminate the right padding from a group of div elements

Is there a way to remove the right padding from the class "no-border"? I attempted some CSS adjustments, but so far nothing has worked. I tried using "padding-right:none" without success. #menu-bar-container-2 { border: 1px solid gray; } .menu-bar-2 a ...

Set the background image width to 75% of the screen size

I want to use this image https://i.sstatic.net/gt30c.jpg as a background for a section on my page. My preference is to have the white part cover only 75% of the screen, stopping before reaching the content in the left grid. Is it possible to achieve this ...

An additional "?" symbol found in the PHP file at the top left corner

I am experiencing an issue with a PHP page where a "?>" symbol appears in the top left corner. The code snippet looks like this: <?php include_once("DBconnect.php"); $getuser = $_POST["RegUsername"]; $getpass = $_POST["Pass"]; $getrepass = $_POST[" ...

Innovative sound system powered by React

I am working on implementing a music player feature on a website where users can select a song and have it play automatically. The challenge I am facing is with the play/pause button functionality. I have created all the necessary components, but there see ...

Utilize the scrollIntoView method within a jQuery function

My current setup involves using JQuery's show and hide function. Essentially, when an image is clicked, it triggers the display of an information log. The issue I am facing is that this log opens at the top of the page, whereas I would like it to scro ...

The copy to clipboard feature is malfunctioning when used with a hidden input field in a React application

To implement the copy to clipboard feature, I have successfully achieved it by using the button type as text with type="text". However, the functionality does not work when the button type is set to hidden. Here is the link to the code on CodeSandbox: htt ...

What is the best method for inserting a specific value into a tuple that exists within another tuple within Python using Django?

My dictionary for Categories consists of tuples within a tuple, but I am looking to extract only the literals - the category names. from django.db import models CATEGORIES = ( ('a', 'Clothing'), ('b&ap ...

How to Make a Doughnut Chart Using CSS

For a while now, I've been working on filling in 72% of a circle with a hole cut out of the middle. Throughout this process, I have been consulting different resources - like this method for calculating and this one for other aspects. However, I’ve ...

What is the best way to set a jpg image as the background for an HTML div utilizing CSS/Bootstrap?

Struggling to set the background image for the first div in my home_no_user.html (which has an id of "background-image") to use the image yoga-stock.jpg. I've done this before without any issues, but for some reason, it's not working now, and I c ...

I can't understand why this question continues to linger, I just want to remove it

Is there a valid reason for this question to persist? I'm considering removing it. ...

Strange gap between element and border on chrome

I noticed some strange spacing between the div borders and elements when I wrapped a few divs inside a container. This issue is only happening on Chrome and Edge, while Mozilla seems to display it correctly. My code includes the usage of Bootstrap along w ...