Make the HTML element expand to the remaining height of the viewport

Need help with creating a section that automatically fills the remaining viewport height below the navigation bar. The section includes a centered div and background image, along with a button at the bottom for scrolling down by one full viewport height. Here's the Bootstrap code for the navigation:

<body>
    <a href="javascript:" id="return-to-top"><i class="icon-chevron-up"></i></a>
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <a class="navbar-brand" href="#">
            <img src="images/main.png" class="d-inline-block align-top navImg" alt="UAB Betono zona">
        </a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation"> 
            <span class="navbar-toggler-icon"></span> 
        </button>
        <div class="collapse navbar-collapse justify-content-end" id="navbarNavDropdown">
            <ul class="navbar-nav navItems">
                <li class="nav-item"> 
                    <a class="nav-link" href="#">Link1</a> 
                </li>
                <li class="nav-item dropdown"> 
                    <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Link2
                    </a>
                    <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink"> 
                        <a class="dropdown-item" href="#">Item1</a> 
                        <a class="dropdown-item" href="#">Item2</a> 
                        <a class="dropdown-item" href="#">Item3</a>
                        <a class="dropdown-item" href="#">Item4</a>
                        <a class="dropdown-item" href="#">Item5</a>
                    </div>
                </li>
                <li class="nav-item"> 
                    <a class="nav-link" href="#">Link3</a> 
                </li>
                <li class="nav-item"> 
                    <a class="nav-link" href="#">Link4</a> 
                </li>
                <li class="nav-item"> 
                    <a class="nav-link" href="#">Link5</a> 
                </li>
            </ul>
        </div>
    </nav>
    <section class="mainSection">
        <div class="logoWrap">
            <h1>
              <span class="hFirst">Text1</span>&nbsp;&nbsp;&nbsp;
              <span class="hSecond">Text2</span>
             </h1>
        </div>
        <div class="scrollDown"> 
            <a href="#" class="next-viewport-down"><span></span>down</a>
        </div>
    </section>

Currently using height: 91vh as a temporary fix, but looking for a better solution to ensure consistent display across various devices. Here's the CSS code for the section:

section.mainSection {
    background-color: #eeeeee;
    height: 91vh;
    position: relative;
    background-image: url(images/Main3.jpg);
    background-repeat: no-repeat;
    background-size: cover;
}

div.logoWrap {
    width: 100%;
    margin: auto;
    text-align: center;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

div.logoWrap .hFirst {
    font-weight: 600;
    position: relative;
    font-size: 2rem;
    font-style: italic;
    line-height: 4rem;
    vertical-align: top;
    width: auto;
}

div.logoWrap .hSecond {
    position: relative;
    font-weight: 700;
    font-size: 4rem;
    font-style: normal;
    line-height: 4rem;
}

Appreciate any insights or suggestions!

Answer №1

Utilize flexbox for optimal space distribution, check out the new CSS code snippet provided below:

* { box-sizing: border-box; }

body {
  margin: 0;
  display: flex;
  align-items: stretch;
  flex-direction: column;
  height: 100vh;
}

.navbar {
  flex: 0;
}

.mainSection {
  flex: 1 1 auto;
  overflow: auto;
}

/* The following code remains unchanged */

section.mainSection {
    background-color: #eeeeee;
    position: relative;
    background-image: url(images/Main3.jpg);
    background-repeat: no-repeat;
    background-size: cover;
}

div.logoWrap {
    width: 100%;
    margin: auto;
    text-align: center;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

div.logoWrap .hFirst {
    font-weight: 600;
    position: relative;
    font-size: 2rem;
    font-style: italic;
    line-height: 4rem;
    vertical-align: top;
    width: auto;
}

div.logoWrap .hSecond {
    position: relative;
    font-weight: 700;
    font-size: 4rem;
    font-style: normal;
    line-height: 4rem;
}
<body>
  <a href="javascript:" id="return-to-top"><i class="icon-chevron-up"></i></a>
  <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
    <a class="navbar-brand" href="#">
      <img src="images/main.png" class="d-inline-block align-top navImg" alt="UAB Betono zona">
    </a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation"> 
              <span class="navbar-toggler-icon"></span> 
          </button>
    <div class="collapse navbar-collapse justify-content-end" id="navbarNavDropdown">
      <ul class="navbar-nav navItems">
        <li class="nav-item">
          <a class="nav-link" href="#">Link1</a>
        </li>
        <li class="nav-item dropdown">
          <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Link2
                      </a>
          <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
            <a class="dropdown-item" href="#">Item1</a>
            <a class="dropdown-item" href="#">Item2</a>
            <a class="dropdown-item" href="#">Item3</a>
            <a class="dropdown-item" href="#">Item4</a>
            <a class="dropdown-item" href="#">Item5</a>
          </div>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Link3</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Link4</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Link5</a>
        </li>
      </ul>
    </div>
  </nav>
  <section class="mainSection">
    <div class="logoWrap">
      <h1>
        <span class="hFirst">Text1</span>&nbsp;&nbsp;&nbsp;
        <span class="hSecond">Text2</span>
      </h1>
    </div>
    <div class="scrollDown">
      <a href="#" class="next-viewport-down"><span></span>down</a>
    </div>
  </section>
</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

Converting Selenium Webdriver Xpath value to CSS: A Step-by-Step

We are looking to transform the lengthy HTML Xpath values in the existing code into a shorter CSS value: driver.findElement(By.cssSelector(".form button")).click(); ...

CSS from the parent page is not being applied to AJAX-injected content

I recently added AJAX functionality to a new website to dynamically load and insert Wordpress page content using AJAX. However, I encountered an issue where some CSS styles that were supposed to be loaded in the page head were not being applied. This resu ...

Change SVG path data into a range of 0 to 1 in order to utilize it as a clip path with objectBoundingBox

My SVG shape, exported from Illustrator as a clipping path, is quite complex. The issue I'm facing is that objectBoundingBox restricts path data to the 0-1 range, but my path data exceeds this range. Here is the current code I'm using: <svg& ...

When seen on a mobile device, the background image is set to repeat along the

Having trouble getting my background image to repeat on the y-axis when viewed on a mobile device. On desktop, the page doesn't scroll and the background image fills the screen perfectly. However, when switching to tablet or mobile, scrolling is neces ...

Click on the link that surrounds an inline-block span in Internet Explorer

In Chrome and Firefox, the following code works fine and makes the container clickable. However, in Internet Explorer, only the inner div changes the cursor to indicate that it's clickable, not the span. I could use cursor:pointer to fix this issue, ...

A complete guide on utilizing *ngFor in Angular to display data rows

I am facing an issue with using *ngFor to create new "rows" for adding new categories dynamically. Although there are no errors displayed when I run the program, the intended functionality is not achieved. I have tried making some modifications but it see ...

The 'utf-8' codec is unable to interpret the byte 0x96 at position 227, as it is an invalid start byte

Recently, I began working with Django. Unfortunately, I encountered an error that I haven't been able to solve. Can anyone explain why this error is occurring? Specifically, I am utilizing {% extends "base.html" %} on my login page. Here is the exa ...

Accessing the initial element inside a DIV using CSS

In the structure of my code, I have a WYSIWYG editor enclosed in a table within a div element: <div class="mydiv"> <li> <table>My WYSIWYG</table> </li> </table> Within the WYSIWYG editor itself, there a ...

What is the best way to set the screen height without needing to scroll vertically?

How can I make the orange and blue area extend to the end of the screen or have the full screen size minus the height of the navbar? When using `vh-100`, it fills the full height but creates a vertical scrollbar which is not desired. Is there a way in Boot ...

I'm looking to have a span and an input side by side, both spanning the full width of the containing div. How can I achieve this?

In my code snippet below: <html> <body> <div style="width: 700px;"> <span>test</span> <input style="width: 100%;"></input> </div> </body> </html> I am facing an issue where the span and input el ...

Difficulties encountered when adjusting the size or reducing the images in a Cycle2 slideshow

Greetings to all fellow coders! Thank you for taking the time to read this post. I am currently facing a challenge in my web development project where I am trying to make my Cycle 2 slideshow images resize and reposition alongside other divs when the wind ...

What could be the reason for my MVC 5 ASP.Net application functioning perfectly on my local machine but encountering issues when deployed to Azure

Looking for some help with my first question here. I've recently completed a small ASP.Net project focused on racing drivers and teams, which works well locally but encounters issues when deployed to Azure. The first problem I've encountered is ...

issue with border color staying the same when focusing

I've been struggling with changing the border on focus in my code. Despite trying various methods, nothing seems to be working. Can someone help me figure out what I'm doing wrong? <input type="text" spellcheck="false" data-placement="top" id ...

Failure to correctly apply CSS to Angular custom components causes styling issues

I have been working with a custom component and I have incorporated several instances of it within the parent markup. When I apply styles directly within the custom component, everything works as intended. However, when I try to set styles in the parent co ...

Different methods to disable scrolling when the mobile menu pops up

I have implemented a mobile menu option that appears when you click on the triple line logo, but unfortunately, users can still scroll while the menu is open. I've tried using position:fixed; but haven't been successful in preventing scrolling be ...

Having numerous calls to isNaN within a JavaScript if-else statement

I've created a JavaScript function that generates div elements and styles them based on user input values. However, I'm facing an issue when trying to display a warning message if the input is not a number. Currently, I'm unable to create th ...

Minimum width of Angular Material's mat-menu

I am looking to create a compact Material mat-menu using Angular 15. Below is the code I have: <mat-menu #flagMenu="matMenu"> <button mat-menu-item> <img src="assets/flags/en.png" class="flag"/> ...

Differences between flexible and fixed-width columns in responsive grid layouts

Recently, I've been diving into the world of flexbox and creating my own grid system. Traditionally, when building a grid system using floats, you have to calculate the number of columns per layout and assign percentages for each column's width. ...

Is it possible to update table cell content depending on selected option?

Displayed here is the select block: <form> <select id="select"> <option disabled selected value="choose"> CHOOSE </option> <option value="i2g" id="i ...

What is the best way to remove bootstrap when transitioning to a new microfrontend?

Our team is facing an issue with styling when using a combination of React, Bootstrap, and Tailwind. The problem arises when linking our micro frontend to modules that use Tailwind, as the Bootstrap styles persist despite the change. Since both Tailwind an ...