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

Is it possible to incorporate a label within a dropdown menu (<select>)?

How can I add a label inside a select box using Bootstrap? Here is an example: Before selecting an option After selecting an option, the label should appear in a small size like this: After selecting an option : <div class="form-group"> & ...

Modify the placeholder color for a disabled Input element to match the overall Mui theme design

I am struggling to change the color of the placeholder text in a disabled input field within my app's theme. Despite attempting to target the MuiFormControl, MuiInputBase, and MuiInput components in my theme.tsx file on CodeSandbox, I have not been su ...

Is there a way to apply CSS styles to a specific word within a div that corresponds to the word entered in the search box?

Using a searchbox, you can enter words to find pages with the searched-for word in the page description. If the word is found in the description, a link to the page along with the highlighted word will be displayed. However, I am encountering an issue wher ...

Tips for incorporating a Font Awesome icon <i> within a select dropdown and customizing its appearance

I am facing an issue while trying to insert an icon into a select option. The error message I received is: Warning: validateDOMNesting(...): cannot appear as a child of <option> To indicate that certain fields are required, I am using asterisk ic ...

Modifying the order of Vuetify CSS in webpack build process

While developing a web app using Vue (3.1.3) and Vuetify (1.3.8), everything appeared to be working fine initially. However, when I proceeded with the production build, I noticed that Vue was somehow changing the order of CSS. The issue specifically revol ...

Converting Uniquely Designed Photoshop Shapes into HTML

I am working on a project to design an HTML page that features a profile picture with a heart frame. I have the PSD file with the heart frame included. The profile pictures will be rectangular in shape and should fit behind the heart frame without overlap ...

Responsive menu not collapsing properly and appearing funky in Drupal

I've managed to create a responsive navigation bar, but I'm encountering two issues. Firstly, when I resize the screen on my test pages, not all of the links hide as expected. Secondly, after inserting the code into Drupal, the nested links appea ...

Different custom background images for every individual page in Nuxt

Looking for a unique background-image for each page in my app is proving to be challenging. Currently, I have defined the background-image in style/app.scss. @import 'variables'; @import 'page_transition'; @import url('https://f ...

Is Ruby on Rails featured in Designmodo's Startup Framework Kit?

I'm curious if the Startup Framework Kit from Designmodo can be seamlessly incorporated into my Ruby on Rails project. While I've had success integrating their Flat-UI-Pro using a gem, I haven't come across one for the Startup Framework yet. ...

Utilizing DOMStringMap data to populate CSS URLs

I am attempting to create a universal CSS modification for a webpage element that resembles: <button data-action="link" class="cardImageContainer coveredImage cardContent itemAction lazy lazy-image-fadein-fast" data-blurhash="lo ...

Place an image on top of adsense

Is there a way to overlay an image on top of Google AdSense ads in order to track user clicks? I am trying to implement an onclick method for when someone clicks on the ads. .ad-alert { width: 728px; height: 400px; background: #cccccc; mar ...

What is the method for customizing the background color in a .vue file using Bootstrap?

Can anyone assist me in updating the background color of a div class from grey to white? The code is within a .vue file using Bootstrap and includes an enclosed SVG file. Here's the snippet: <div class="text-left my-3 bg-white"> <button var ...

Setting Image Height with CSS

I have a div with a height of 105px. The image inside the div is 359px tall. How can I adjust the div's size to prevent cutting off the image and ensure it displays in full height? Thank you for your help! ...

Divider displayed between images in Internet Explorer 8

On my website, I have arranged four images in a square using the code below: <div id="tempo_main"> <div id="tempo_content"> <div style="text-align: center;z-index: 3;position: absolute;right:350px; left:350px; t ...

Enhance User Experience with the Tooltip Feature and Customized

Here is the jQuery code I am using to style my tooltips: $(function() { // select all input fields within #tooltips and attach tooltips to them $("#tooltips :input").tooltip({ // place tooltip on the right edge position: "cen ...

Changing the color of a div while implementing a show and hide effect in JavaScript

I have designed a checkout system with three distinct parts - shipping information, billing information, and order confirmation. These sections are all on the same page, allowing for a seamless flow during the checkout process. Once a customer completes t ...

Help Needed: Adding a Simple Element to jQuery Tabs Script

I recently came across a fantastic jQuery tabs script on a website called Tutorialzine. The link to the article can be found here. As I was implementing this script, I realized I needed to customize it by adding specific classes to certain tabs. Specifica ...

When I attempt to incorporate multiple sliders on a single page, I encounter difficulties in determining the accurate stopping position if the number of slides varies

I am having trouble setting the correct stop position for sliders with different numbers of slides on a page. When I have the same number of slides in each slider, everything works fine. However, I need to have a different number of slides in each slider ...

Hover over with your mouse to open and close the dropdown menu in React JS

Just starting out with React JS and encountering a small issue. I'm trying to make the menu disappear when the mouse leaves that area, so I used onMouseOut and onMouseLeave to close it. However, I noticed that having these options in place prevents th ...

How come the inclusion of a DOCTYPE tag impacts the styling of my CSS?

I am puzzled why the code below renders correctly without any issues until I add a doctype declaration: <body style="margin:0; padding:0; background-color:#eeeeee"></body> <div id="HeaderContainer" style="background-color:#eeeeee; color:Bl ...