What is the best way to combine height and min-height in order to ensure that a div always extends to the bottom and keeps

I've tried multiple suggestions without success so far. In my website, I have a main section with white background centered on the page. It stretches nicely to the bottom using flex. See image:

https://i.sstatic.net/RmJ2o.png

However, there's a specific section with lots of content where scrolling brings a white background into view instead of keeping the image fixed:

https://i.sstatic.net/cxpcl.png

I managed to fix this issue, but it breaks the layout for other pages (see below). Here is the current HTML and CSS code (also utilizing Bootstrap):

<body>
  <div class="bg flex-column d-flex">
    <header class="container">...</header>
    <div class="container" flex-grow-1 flex-shrink-0">
      <main role="main" class="h-100">
         ...
      </main>
    </div>
  </div>
</body>
body, html {
    height: 100%;
    margin: 0;
}

.bg {
    background-image: url("../images/bg.jpg");
    height: 100%;
    background-position: center;
    background-repeat: repeat-y;
    background-size: cover;
    background-attachment: fixed;
}

The "fix"

By modifying the CSS as follows:

body, html {
    min-height: 100%; /* ! */
    margin: 0;
}

The scrolling white background issue has been resolved. However, now other pages are affected because the div no longer stretches to the bottom of the page:

https://i.sstatic.net/J1ZGp.png

Any assistance would be greatly appreciated.

Answer №1

To ensure that all content within the .bg div remains within the parent div's boundaries and includes a scrollbar for overflow, simply include the CSS property overflow: scroll.

Example:

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

.bg {
    overflow: scroll;
    background-image: url("https://via.placeholder.com/150");
    height: 100%;
    background-position: center;
    background-repeat: repeat-y;
    background-size: cover;
    background-attachment: fixed;
}
<body>
  <div class="bg flex-column d-flex">
    <header class="container"></header>
    <div class="container" flex-grow-1 flex-shrink-0>
      <main role="main" class="h-100">
         <ol>
          <li>Some content</li>
          <li>Some content</li>
          <li>Some content</li>
          <li>Some content</li>
          <li>Some content</li>
          <li>Some content</li>
          <li>Some content</li>
          <li>Some content</li>
          <li>Some content</li>
          <li>Some content</li>
          <li>Some content</li>
          <li>Some content</li>
          <li>Some content</li>
          <li>Some content</li>
          <li>Some content</li>
          <li>Some content</li>
          <li>Some content</li>
          <li>Some content</li>
          <li>Some content</li>
          <li>Some content</li>
          <li>Some content</li>
          <li>Some content</li>
          <li>Some content</li>
          <li>Some content</li>
          <li>Some content</li>
          <li>Some content</li>
          <li>Some content</li>
          <li>Some content</li>
          <li>Some content</li>
          <li>Some content</li>
         </ol>
      </main>
    </div>
  </div>
</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 create a hyperlink on the second page that directs to the first page and triggers a specific JavaScript function on the first page?

Working with a former colleague's code in C#, asp.net MVC, Telerik/Kendo controls. I am trying to navigate to a specific section on a page from another page that uses tabs/sections with links at the top of the page to toggle visibility based on the se ...

CSS hover effect ceases to function after the button has been clicked once

I am facing a dilemma with styling that I can't seem to resolve. There is a basic toggle feature on my page with two options -- the user can select either Toggle1 or Toggle2, resulting in different data being displayed dynamically based on the active ...

HTML: Organize a slightly intricate column in a table

I am facing an issue with sorting columns in an HTML table that has 2 headers row: <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="styles.css"> </head> <body> <table ...

Is there a way to prevent all attribute menu items within a ul/li List from being clicked when one of them has already been selected

When I quickly click on the Photos1, Photos2, Photos3, and Photos4 menus, the "$.get(..." is sent to the server four times. It's important that all of these menus are disabled after any one is clicked. My development environment includes NodeJS, Expre ...

Automatically, the "ng-hide" class gets added despite using the correct syntax for ng-show

I am trying to dynamically show/hide a tr tag within a table based on the value of a scope variable in the controller. Despite writing the code correctly, I am facing an issue where the "ng-hide" class is automatically added to the tr tag every time it is ...

Incorporating user input into a div element

So, I'm in the process of building my own Task Tracker using JavaScript to enhance my skills, but I've hit a roadblock. I successfully implemented adding a new div with user-inputted tasks, however, there's no styling involved. <div cla ...

Turn off hover effect using solely CSS

This is my first time posting on stackoverflow, so please excuse me if I overlook something obvious. I tried searching for a solution beforehand but couldn't find one that seemed relevant. In the following jsfiddle example, I have a div that acts as ...

Having trouble with SCSS styles not being applied after refactoring to SCSS modules?

Currently, I am in the process of restructuring an application to ensure that component styles are separated from global styles using CSS modules. However, I have come across an issue where the styles are not being applied correctly. The original code sni ...

When I start scrolling down, the Jumptron background image vanishes

Utilizing bootstrap jumptron with a background image has led to an issue where the background image disappears as I scroll down, leaving only the jumptron div class displaying the heading. How can this be fixed so that the image remains visible even when s ...

The height of the input element is greater than the size of the font

I am facing an issue with an input element that has a height 2px higher than what I expected. Here is the relevant CSS: .input { font-size: 16px; padding: 15px; border: 1px solid black; border-radius: 3px; width: 400px; } <input class=" ...

"Running experiments with Google Ads and Google Analytics scripts results in a blank page being displayed

Currently, I am working on a plain HTML file and conducting tests to ensure that the Google Ads and Analytics scripts are functioning correctly before implementing them on other pages. A colleague who has an AdSense account provided me with the script code ...

Utilizing MaterialUI and CSS to craft this stunning box

Looking to create a new MaterialUI component similar to this one: original box Struggling with implementing it using the card component, but so far it looks like: poorly designed box Here's my custom styling using makeStyles: const useStyles = ma ...

Ways to verify the application of CSS hyphens using Chrome's developer tools

I cannot seem to get automatic hyphens to work via CSS. I have applied the following CSS: body { -moz-hyphens: auto; -ms-hyphens: auto; -o-hyphens: auto; -webkit-hyphens: auto; hyphens: auto; } Despite adding this CSS, my text is not being hyph ...

Error encountered: Unrecognized media in CSS validation and parsing issue

I've been having trouble validating my CSS and keep encountering error messages. Despite ensuring I have closing brackets, there are parse errors on line 39 and unrecognized media issues on lines 79 and 81. Additionally, lines 70 and 89 also trigger p ...

Items seem to vanish into thin air and become immovable when attempting to relocate them

I am attempting to create a unique grid layout with 3x3 dimensions, where each grid item represents a fragment of a single image. These items should have the capability to be dragged and dropped inside another 3x3 grid, in any desired sequence. I have hit ...

Alpinejs Mega Menu Issue: Hover Functionality Glitchy

I'm working on a complex Mega Menu project that is activated upon hovering, using the powerful combination of Tailwind CSS and Alpinejs. The functionality is mostly there, but I've encountered some bugs along the way. Despite my attempts to impl ...

Using CSS to create hover effects for specific classes of elements

Is there a way to make the button change color on hover when hovering over anywhere in the navigation bar (.topNav) instead of just when hovering over the individual button elements (.top, .middle, .bottom classes)? I tried using span to achieve this, but ...

Using the onclick event with an image created through PHP code

I am currently in the process of developing a dynamic image represented as a graph displaying the number of documents generated each day. This interactive image is being created using PHP, and I aim to improve user engagement by enabling them to click on t ...

Adjusting div height as you scroll down the page

Hello, I am new to CSS and I have encountered an issue with setting the height of a div element to 100%. When the list on my page exceeds the size of the page, the div is no longer visible when scrolling. Do you have any ideas on how to fix this? Here is t ...

"Utilizing Bootstrap's form-check feature places the checkbox alongside the text

While attempting to create a checkbox using the Bootstrap form-check class, I encountered an issue with my code. Here is the snippet: <form> <div class="form-section"> <div>Title</div> <div class="form-check"> ...