Arranging div elements in a dynamic container

I'm facing a challenge with structuring a new widget I'm developing. My goal is to incorporate a geographic map with hotspots without resorting to image maps.

Here's the incomplete solution I've come up with so far:

http://jsfiddle.net/nielsbuus/FZJ8E/1/

HTML:

<div class="parent">
    <!-- I serve as a container and need to expand in height along with my children to prevent overlapping of elements below me, similar to clearing floats -->
    <div class="lower">
        <!-- I contain an image with fixed width but variable height -->
        <img src="http://placekitten.com/300/325" />
    </div>
    <div class="higher-container">
        <div style="top: 20px; left: 30px" class="higher-spot">foo</div>
        <div style="top: 80px; left: 50px" class="higher-spot">bar</div>
        <div style="top: 85px; left: 70px" class="higher-spot">baz</div>
    </div>
</div>
<div class="successor">
  Please make sure to keep me positioned below all of this.
</div>

CSS:

.parent {
    background-color: #ccffff;
    padding: 20px;
    position: relative;
}

.lower {
    position: absolute;
    width: 300px;
    background-color: rgba(0,0,0,0.2);
    z-index: 0;
}

.higher-container {
    position: absolute;    
    background-color: rgba(255,0,0, 0.3);
}

.higher-spot {
    position: absolute;
    width: 25px;
    height: 25px;
    background-color: rgba(0, 255, 0, 0.5);
    z-index: 1px;
}

.successor {
    font-size: 18px;
    font-family: sans-serif;
}

The main issue here lies in using absolutely positioned z-indexed divs. They cause detachment from the layout, preventing the parent container from expanding and placing subsequent elements beneath the absolutely positioned ones.

I am seeking suggestions on how to stack two divs (lower and higher-container) together while ensuring that the parent container expands according to the size of the lower div.

Answer №1

It seems a bit peculiar to me why one would opt for using position: absolute for the .lower container. Consider changing it to relative and then applying the top: 0 rule to the .higher-container.

Check out the updated fiddle here

Answer №2

It is advisable to have only one of your children set as absolutely positioned: the one that is placed on top (.higher-container). By doing this, .lower, which will always be taller than .higher-container, will stretch the parent container normally. Then, the higher-container will be layered above it using absolute positioning. Simply add a top and left positioning for it:

To see an example, check out this jsfiddle link: http://jsfiddle.net/rgthree/M6jm8/

.lower {
    position: relative;
    width: 300px;
    background-color: rgba(0,0,0,0.2);
    z-index: 0;
}

.higher-container {
    position: absolute;
    top: 0px;
    left: 0px;   
    z-index: 1;
    background-color: rgba(255,0,0, 0.3);
}

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

Obtain the text content within a textarea using jQuery

There is a text area that includes both html and regular text, and I need to extract both from the text area. However, it seems that the current code is only retrieving the html code. HTML CODE <textarea id='post'> <div class="separato ...

Highcharts: Show tooltip above all elements

Is there a way to configure tooltip display above all elements? I want to define specific coordinates so that the tooltip remains open even if it is covering the chart, and only closes when interacting with the top block. Screenshot 1 Screenshot 2 For e ...

Place centered items within a flexbox that uses space-between alignment

When designing a navigation section, I am looking to achieve space-between justification. However, for smaller screens where the navigation items may need to wrap onto multiple rows, I want the items to center themselves instead of aligning to the left whe ...

Is it possible to retrieve real-time data from a database using AJAX technology?

The information I am retrieving is stored in a specific database. By using an arduino-box, the data is transmitted. The data is then presented using the following CSS & HTML code: <div class="event"> <img src="http://dummyimage. ...

Conceal list items by clicking elsewhere on the page

Currently, I am facing an issue with my search user functionality. Whenever a user clicks anywhere else on the page, the list of results does not disappear unless the user manually deletes all the words they have typed. This behavior is not ideal and despi ...

What could be causing the issue with the focus not being activated when clicking on the input field in Vue?

I am facing an issue where I have to click twice in order to focus on a specific input field, and I'm having trouble setting the cursor at the end of the input. I attempted to use $refs, but it seems like there may be a deeper underlying problem. Any ...

Keep Dropdown menu open when clicking on a menu item to avoid closing

I'm facing an issue with a drop-down menu in the sidebar that contains links as menu items. Whenever I click on a menu item (link), the link works fine but the drop-down menu closes. What I actually want is for the drop-down menu to stay open even af ...

Ways to display a box following the submission of a value within a form

There's a pop-up window where the user has to enter their name. After clicking the submit button, this window closes. You can try it out here: (Click the 21 button, enter any value, and see what happens) The function used for the pop-up box is: fu ...

PHP email script encounters error message despite receiving a network response code of 200

I found this PHP code online and it seems to be correct, but I keep encountering an error message every time I attempt to send a test email. However, the network message indicates that the email was sent successfully. Please refer to the provided images an ...

Finding web page links from competition rankings using pattern matching

Challenge I am currently delving into the realm of natural language processing projects. Before diving in, I intend to explore existing works on datasets, particularly those organized on a leaderboard (refer to the "Three-way Classification" section). Ho ...

Limits in exporting data from an html table to a pdf document

The output on the page only displays 17 out of 60+ rows of data. I've searched online for a javascript function to help with this, but unfortunately, I couldn't find one. Here is a sample code snippet: <script type="text/javascript"> ...

Is it still necessary to include -webkit- and -moz- prefixes for widely recognized CSS3 properties?

I'm currently in the process of tidying up a CSS file, and I'm contemplating whether it's now acceptable to remove vendor-specific properties if they have been standardized (or at least partially standardized). For instance, should I contin ...

"What is the best way to prevent a button from being enabled in Angular if a list or array is not

I am incorporating data from my service in the component: ngOnInit() { this._sharedService. getReceiptItem().takeUntil(this.ngUnsubscribe). subscribe(products => this.receiptItems = products); } Is there a way to deactivate a button if there ...

Ways to revert all modifications to styles implemented using JavaScript

Hey there, just checking in to see how you're doing. I was wondering if there's a way to reset all the styles that have been changed using JavaScript? I'm referring to the styles shown in this photo: https://i.sstatic.net/Zawjt.png Thanks ...

Eliminate viewed alerts by implementing a scrolling feature with the help of Jquery, Javascript, and PHP

My goal is to clear or remove notifications based on user scrolling. The idea is to clear the notification once the user has seen it, specifically in a pop-up window for notifications. I am relatively new to Javascript/jQuery and feeling a bit confused abo ...

The div is struggling to contain the image, can anyone assist with this issue?

Greetings all! I am a beginner with CSS and currently working on my website. You can check it out at this link. I am facing an issue where the image on my site is not fitting properly and it keeps repeating. Can anyone guide me on how to fix this using CS ...

Is there a way to navigate to a specific div when clicking on it?

Need help with scrolling after running code function scrollFunction() { document.getElementById('insert').scrollIntoView(); } I have a button that triggers some code and I want the page to scroll afterwards. Currently, it scroll ...

__str__ method not displaying the string properly

I'm currently developing a Django application and I'm working on displaying the first and last names of the user upon login. This is a snippet of my model - class Admins(models.Model): user = models.OneToOneField(User, on_delete=models.CASCA ...

When the screen size changes, the sidebar in Bootstrap 5 smoothly reveals hidden content

I've been working on a sidebar design and everything seems to be going well so far. However, I've encountered an issue that has been giving me trouble for days now. Whenever I display a new page and then resize the screen, the content of the page ...

Tips for sending data through AJAX before the browser is about to close

My issue is with a javascript function that I've called on the html unload event. It seems to be working fine in Google Chrome, but for some reason it's not functioning properly in Firefox and IE. <script> function fun() { ...