Why doesn't the background color display properly when the div height is set to auto?

When I set the height of #container to auto, the background-color does not display. However, when I set it to a fixed height like 1000px, it shows up. I've attempted using "overflow:auto", but that did not solve the issue. How can I fix this? I want #container to expand vertically based on its content and still have its background color visible. Any suggestions would be greatly appreciated!

Below is the HTML code snippet:

<div id="container">
    <div id="main">
        <div id="div1">text text text text text text text text text text text texttexttext text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text</div>
        <div id="div2">text text text text text text text text text text text texttexttext</div>
        <div class="clear"></div>
    </div><!--end of main-->
</div><!--end of container-->

And here is the corresponding CSS code:

#container {
    background-color: rgb(255, 102, 204);
    height: auto;
    width: 1200px;
    position: absolute;
}
#container #main {
    background-color: rgb(204, 51, 0);
    height: auto;
    width: 900px;
    position: absolute;
    overflow: auto;
}
#container #main #div1 {
    background-color: rgb(0, 204, 255);
    float: left;
    width: 300px;
    padding: 5px;
    height: auto;
    margin: 20px;
}
#container #main #div2 {
    background-color: rgb(0, 153, 153);
    height: auto;
    width: 400px;
    float: left;
    margin: 10px;
}
.clear {
    clear: both;
}

Answer №1

It seems like the issue at hand is related to the #main div having a position:absolute; property. Keep in mind that elements with this property, as well as position:fixed;, are taken out of the normal flow of the document, meaning they are essentially not contained within their parent element.

If you'd like to see your code in action, feel free to click on this link

For further information on this topic, I recommend checking out the resources available at W3Schools

I hope this explanation proves helpful to you.

Answer №2

It appears that all of your elements are either positioned absolutely or floated, which prevents the parent from computing a height. You may want to consider setting a min-height on some of your elements or at least on the container.

Please be patient while I work on setting up a jsfiddle with a possible solution :)

Check out lazychio's answer for the solution you're seeking!

Answer №3

To fix the overflowing content issue, you can include "overflow:auto;" in your .css file like so:

#products_section{
    width:800px;
    height:auto;
    margin:auto;
    background-color:pink;
    overflow: auto;
    text-align:center;
    margin-left:200px;
}

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

Extracting table data using regular expressions in Import.io is a powerful method to retrieve specific values

Need some assistance. I am currently utilizing import.io and aiming to extract table data using regex. My goal is to retrieve values from specific columns, below is the code snippet: <table> <tr> <td class='label&apos ...

Issues with escaping HTML encoding

Currently working on an Android app that involves querying a webservice and receiving a JsonObject. Once I have the desired String, I encounter characters like: est&amp;aacute; I've experimented with two methods: StringEscapeUtils.escapeHTML4(te ...

Show or hide a div based on the visibility of another div in Angular using *ngIf

Looking for a way to dynamically display images based on the visibility of another image? Consider the code snippet below: <div *ngFor="let badge of user.progress.unlockedBadges"> <img id="{{i}}_unlockedImage" *ngIf="badge == achievemen ...

Is there a way to horizontally align the content in my footer section?

I am currently exploring React Material UI for the first time. The table I am working with can be found here under the "Custom pagination actions" section. Within this section, there is footer content containing buttons for rows per page, next, previous, ...

Setting up a custom PrimeNG theme to match our unique style is a great way to

I am currently using the most recent version of "primeng": "^12.2.0", and I am looking to implement my own custom theme for primeng. Despite searching through numerous blogs, I have yet to find a solution. In an attempt to create my cu ...

Display the HTML/CSS layout following the JavaScript window.open action

Encountering issues with printing output in an opened window using JavaScript. I'm creating a new document through window.open and including CDN links to Bootstrap files. While everything appears fine in the opened window, when attempting to print (XP ...

Are there any alternatives to PHP for implementing an auto-complete search feature?

I have decided to focus on using HTML, MySQL, JavaScript, and jQuery for my project. Though I understand that learning PHP would be beneficial in the long run, I don't have enough time to master it all within a week. As for the server-side, I will be ...

The onClick function for the "div" element is failing to append

I am currently working on a project that involves searching for strings in a database and then appending the selected string to a text box. So far, I have been successful in retrieving the search results from the database and appending them below the text ...

CSS: Stack elements vertically based on their height (column-wise)

Looking for some help with designing a menu where the list items float in columns instead of rows. For example, This is how my current menu looks like: I want to change the layout to look like this: My code for the list items is as follows: .mnu-third ...

Correct placement of elements using absolute positioning and optimized scroll functionality

I am currently working on implementing tooltips for items within a scrollable list. My goals for the tooltips are as follows: Ensure they are visible outside and not restricted by the scroll area Appear immediately after the corresponding item Be detach ...

Customizing Material-UI Grid: Is it possible to assign a background color solely to the cells, without affecting the surrounding empty spaces?

I've been experimenting with designing a grid system that incorporates a gradient background color. The issue I'm facing is that the background color extends to the empty spaces between the grid cells as well. Is there a way to scope the gradient ...

Unusual Actions from the Text Field

Please take a look at this link <div id="textarea_wrapper"> <textarea>How and where is my width derived from?</textarea> </div> #textarea_wrapper{ height: 250px; border:thick solid green; } textarea{ background-color: #930; ...

"Utilize the hover functionality in React JS to track the mouse movement

I'm currently facing an issue with a design that involves a 6-column grid and text placed in front of it. The requirement is that when I hover over the text, the background of the corresponding grid column should change to an image. While this functio ...

If you want to retrieve the calculated value of a div using jQuery

I have a scenario where I have 3 list items (li) under an unordered list (ul). I am interested in finding the height of these list items, but without explicitly defining their height. So far, when inspecting with Firebug, I noticed that the computed height ...

Is it possible for JQuery to interact with an Access database for both reading and writing

Is it possible to utilize Jquery to establish a link with a Microsoft Access database? I have been exploring this possibility but haven't found any relevant information. This database won't be used for a production site, but rather for a small gr ...

Issue with using variables in nodejs, express, and EJS: Undefined

Having some trouble with EJS here. I am using response.render('agent.html', {name: 'aaaa'});, and in my HTML script, I attempt to access <%= this.name %>. Unfortunately, I keep getting a ReferenceError: aaaa is not defined. Any sugge ...

There are no elements displayed beneath the div

If you're short on time, let me explain my point with an image: Here is the HTML code I am working with: <div align="center"> <div class="img-container"> <div class="myconatiner"> <h2>Headline< ...

Warning: Validation issue in console.js at line 213 - It is not valid for a <div> element to be nested inside a <p> element. Please correct this nesting validation

react-dom.development.js:86 Warning: validateDOMNesting(...): <p> cannot appear as a descendant of <p>. at p at http://localhost:5173/node_modules/.vite/deps/chunk-WQ5FBX7J.js?v=539aff10:2964:50 at Typography2 (http://localhost:5173/node_module ...

What is the best way to center a fixed position background image within a container that is slightly shifted from the top of the viewport?

How can I center a background-image vertically, which has a fixed background-attachment and is positioned 100px from the top? The background-size property is set to cover for horizontal centering but the vertical alignment is off. Below is the HTML code: ...

Calculating the total sum of values using a dropdown list as a filter with J

I have successfully implemented a dropdown filter in my jQuery datatable to filter data. However, I am now looking to calculate the sum of all values in the filtered table each time a user selects a value from the dropdown list. How can I achieve this? My ...