Is there a way to adjust the height relative to the viewport in Bootstrap 5 for values other than 100?

Attempting to create a new type of div using a custom stylesheet with minimal non-bootstrap css. I am trying to convert each style rule from my django static files CSS into a bootstrap class, but I am stuck on viewport-based sizing.

Prior to discovering these docs, the height of the div fit the content as expected:

I came across this amazing feature in Bootstrap 5

Setting my class to "vh-100" made the div take up the full vertical height of the view port.

However, it matched the height of the ENTIRE viewport, not just the remainder after considering the navbar and padding. This made sense though.

Great! All I need is to adjust it slightly so that it fits within the actual viewport for the desired end result.

In Bootstrap documentation, you can usually specify 25%, 50%,75%, or 100% like we did here. However, it doesn't mention that for viewport-based sizing, even though it does for many other bootstrap features. Therefore, I assumed it would work similarly. So I tried setting it to 75% Viewport to add a little bottom area.

Now we have "vh-50" or "vh-75"

Strange - the viewport must be set to 100 or else it reverts back to sizing based on content. I couldn't find any examples of other values for viewport-based sizing in the docs or anywhere else.

Here is the html for that div (part of a jinja content block in a django app)

{% block content %}
  <div class="body-area bg-dark text-light my-5 vh-75">
    <div class="container">
      <div class="row">
        Hi
      </div>
    </div>
  </div>
{% endblock %}
This is my CSS. I have already attempted removing ALL instances of height from my css with no effect. The body and html styles are applied via a base template, which works because the background gradient displays correctly (it tiles otherwise without that body/html style).

.body-area {
  margin-left: auto;
  margin-right: auto;
  width: 75%;
  border: 10px solid #000000;
  border-radius: 15px;
}

html {
  height: 100%;
}
body {
  height: 100%;
  margin: 0;
  background-repeat: no-repeat;
  background-attachment: fixed;
}

While I am starting to doubt whether I should even continue, and might not in this particular app, I believe it would be beneficial to be able to size elements based on percentages of the viewport, especially for a mobile app.

Am I approaching this incorrectly? Should I only ever size anything at 100% of the viewport? How should I go about this, either with the bootstrap tool I am trying to use or with some best practice that is currently unknown to me?

I am hoping there are CSS/bootstrap experts out there who know a trick that could help both me and anyone else who comes across this issue in the future.

Answer №1

Instead of manually setting heights, it is recommended to utilize the flexbox grid along with appropriate alignment classes for automatic height adjustment.

  1. Enclose the navbar and content elements in a flex column with the class vh-100. This can be as simple as applying it to the body element.
  2. Add the class flex-fill to the content element to ensure it expands to fill the remaining space.

<head>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a5c7cacad1d6d1d7c4d5e5908b948b96">[email protected]</a>/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
</head>

<body class="d-flex flex-column vh-100">
  <nav class="navbar navbar-expand-lg navbar-light bg-light">
    <div class="container-fluid">
      <a class="navbar-brand" href="#">Navbar</a>
      
      <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
      
      <div class="collapse navbar-collapse" id="navbarSupportedContent">
        <!-- ... -->
      </div>
    </div>
  </nav>

  <div class="body-area container-fluid bg-dark text-light flex-fill">
    <div class="row">
      <div class="col">
        Hi
      </div>
    </div>
  </div>
</body>

Answer №2

I find it puzzling that the developers didn't include 25, 50, and 75 in the vh-* options. It seems like a common request from users and wouldn't cause any harm.

If you're up for compiling Bootstrap yourself, you can easily do so using the Utilities API:

@import "bootstrap/scss/functions";
@import "bootstrap/scss/variables";
@import "bootstrap/scss/utilities";

$utilities: map-merge(
    $utilities,
    (
        "viewport-height": (
            property: height,
            class: vh,
            values: (
                25: 25vh,
                50: 50vh,
                75: 75vh,
                100: 100vh
            )
        )
    )
);

Alternatively, you can manually add these classes:

.vh-25 {
    height: 25vh !important;
}

.vh-50 {
    height: 50vh !important;
}

.vh-75 {
    height: 75vh !important;
}

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

Utilize flex to position content at the bottom

My layout features a fixed header and footer, positioned at the top and bottom respectively. The content section in between fills the remaining space, with a scrollbar available if the content extends beyond the area. <div id="app"> <d ...

Python Selenium: Cannot Click on Element - Button Tag Not Located

TL,DR: My Selenium Python script seems to be having trouble "clicking" on the necessary buttons. Context: Hello. I am working on automating the process of logging into a website, navigating through dropdown menus, and downloading a spreadsheet. Despite ...

CSS: Strategies for eliminating empty cells within a grid layout by filtering out rows that contain partial or incomplete content

I am in need of creating a grid layout where each div is a specific width, and the number of rows depends on how many items there are. The twist is that I am unsure of the width of the outer container. My initial solution was to use CSS grid: #container ...

How to vertically align radio buttons with varying label lengths using Angular and Bootstrap 4?

Is there a way to properly align radio buttons with variable length labels? When each label has a different length, the radio buttons appear misaligned. How can this be fixed? <div class="row"> <div class="form-check form-check-inline" *ngFor="l ...

Limit the ability to zoom in a webview application

I am facing an issue with my Android WebView app that is designed to load a specific URL and display it along with its links within the app. Despite setting the webpage size to 720px x 1280px using CSS to fit the screen of a Galaxy S3, I am encountering di ...

What causes z-index to be ineffective with sticky elements?

In my website, I've implemented rollover effects in a sticky footer and a responsive menu that stays at the top. However, when the menu is opened and extends over the footer, it covers everything except the rollovers. Closed navigation http://www.mus ...

Leveraging .Net ConfigurationManager.AppSettings in Cascading Style Sheets

I am facing an issue where I have a specific color key in my AppSettings for the company's brand color, which needs to be applied to a CSS class. The challenge is how to incorporate this key into my stylesheet. Is there a way to access the Configurati ...

The layout of my website is perfect when my laptop's zoom is set to 90%, but at 100% it starts overflowing

Currently, I am in the process of building a website on my laptop while following an instructor's video tutorial. It has been important for me to set the same pixel measurements as the instructor in order to replicate the design they provided. However ...

Interact with users on a website by using PHP to process form data

Here is the code snippet I am struggling with: <form action="profiles.php" method="POST" name="SearchSimple" id="SearchSimple" > <input name="search" id="s" style="width: 150px;" type="text"> <a style="display: inline-block; widt ...

Troubleshooting challenges arise with Bootstrap's button group spacing issue

I am encountering spacing issues with the markup provided below. <h2>Profitability Report</h2> <form method="post"> <div class="row"> <div class="col-md-12"> <div class="btn-group btn-group-toggle ...

conceal menu upon click (gradually disappear)

This is a basic HTML/CSS menu. Currently, it is functioning properly for page redirection (href). However, I would like it to hide after being clicked on. I plan to call a function that will initiate an AJAX request upon clicking. Here is the code on JS ...

Unable to retrieve the values from document.getElementById("") or document.getElementById("").innerHtml appears to be malfunctioning

I am currently working on an HTA application that includes multiple <input type="text"> fields for users to input data. My goal is to pass this data to a VBScript for processing. Below is the HTML structure: <div id="tableContent" name="tableCon ...

Problem with updating an Html.TextBoxFor based on the value of another one

I have been trying to implement a basic jQuery functionality that involves changing the text in one textbox to be half of the numeric value entered in another textbox. Below is the script I am using: <script> $(document).ready(function () { $(& ...

Mouse selection is not functioning in jQueryUI Autocomplete

I'm experiencing an issue where the jQueryUI autocomplete doesn't work with the mouse when the input is in a bootstrap modal form. It only works when selecting with the keyboard, completely ignoring mouse selection. Below is some sample code sho ...

Is it possible to trigger a modal to open automatically after a 3-second delay?

code: <script> $(document).ready(function() { setInterval(function() { $('#contact').modal(); }, 3000); }); </script> html code: <a href="#contact"><h4><i class="fa f ...

The CSS styles are not being applied correctly in the HTML document

I have this snippet in my .css file: .attachment-label { font-size: 10px; font-style: italic; color: rgba(0,0,0,.5); } When I try to use it in my HTML like this: <div class="card-body"> <span class=" ...

Angular backslash is encoded

Experiencing the same issue as this individual: angularjs-slash-after-hashbang-gets-encoded The URL is getting encoded and not routing correctly, causing it to fall to the otherwise in my route config. I have not been able to identify the root cause yet, ...

Updating the time and date format within an AngularJS application

I am receiving date and time data from the API and would like to adjust the format. The current format is "2016-05-12", "07:17:35". Desired format: 12-May-2016 7:30 PM: <table class="table"> <thead> <tr> <th& ...

Ways to calculate the total order amount when the quantity is modified

The order entry form includes columns for product name, price, and quantity: <table id="order-products" class="mobileorder-table"> <colgroup> <col style="width: 80%;"> <col ...

Trouble displaying certain HTML code and its output on the browser?

I am in the process of developing a user profile page. I have designed a form with various fields, but the code within the section is not displaying correctly in the browser. I attempted to inspect the elements, but the code within that section remains inv ...