Placing CSS elements in an absolute position over dynamic heights

I am facing a challenge with positioning two divs inside a parent div. My goal is to have the first div positioned absolutely at bottom:0 of the parent div, while the second div should always be on top of the first one.

The issue arises because the height of the first div is variable. How can I ensure that the second div remains on top of the first one in such a scenario? Please refer to the attached image for better understanding:

My attempt so far has not been successful:

HTML:

<div class="box">
   <div class="wrap">
       <div class="box2">box 2</div>
       <div class="box1">box1</div>
    </div>
</div>

CSS:

.box{
    width: 200px;
    height: 200px;
    background: green;
    position: relative;
}

.wrap{
    position: absolute;
    bottom: 0;
    border: 1px solid blue;
}

.box1{
    background: yellow;
    height: 50px;
    position: relative;
}

.box2{
    background: red;
    position: absolute;
    top: 0;    
}

Demo: http://jsfiddle.net/P46ht/

Answer №1

You're almost at the finish line.

Here's a suggestion - simply remove the positioning from the boxes and instead set the width on .wrap:

.wrap{
    position: relative;
    width: 100px;
    border: 1px solid green;
}

.box1{
    background: orange;
}

.box2{
    background: purple;
}

Example: http://jsfiddle.net/K27tv/2/

Answer №2

Check out this example (DEMO):

.wrapper{
        position: absolute;
        bottom: 0;
        width: 100%;
        border: 1px solid blue;
        box-sizing: border-box;
    }

    .container1{
        background: yellow;
    }

    .container2{
        background: red;
        height: 50px;
    }
    

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

Issues persisting with vertical alignment in table cells on Firefox browser

I've successfully managed to vertically center the contents of .onesizechart in Chrome and Safari, but I'm facing issues with Firefox and IE. Interestingly, the contents of .homepage-sizechart are displaying correctly, leading me to believe that ...

Extracting data from tag definitions using Beautiful Soup

Here is the HTML Code: <option data-formated='&lt;span class="price"&gt;AUD $100.08&lt;/span&gt;' data-qtyid="qty-219" value="1"> Unit Price </option> ...

While using Google Chrome on a mobile device, users may encounter a white box that unexpectedly appears

I'm in the process of developing a website. Check out the website here. You can view the code on Github as well. Access the code here. If you prefer, here is the HTML and CSS: HTML: <!DOCTYPE html> <html lang="en"> ... // HTML code goes ...

What could be causing my div elements to not appear on the page?

Hey there, this is my debut post here so bear with me on the formatting. I'm currently working on a project and encountering some hurdles with my divs. Even though I've left space for them, I just can't seem to get my divs (or cards) to disp ...

Ways to achieve a gradient effect on top of an image

Hey, I'm trying to implement a gradient on images in my project. Here's what I have so far: import Image from 'next/image' import Box from 'mui/material/Box' import PlayIcon from './PlayIcon.tsx' <Box ...

After hovering over the box, we can observe the :after pseudo

Encountered an issue where the box fails to appear when hovering over the :after element. HTML CODE: <div class="top_line_dropdown"> <span class="logged_in_user"> <a href="/cdn-cgi/l/email-protection" class="__cf_em ...

Apply the background-color specifically to the text within a td, excluding any empty space

I need to make the background color wrap around just the text, not the empty spaces. Currently it looks like this: <TD> Text <BR><BR; Longer text that stretches out above text-background color... </TD> With the following CSS: td {b ...

Text exceeding boundaries within a horizontal flexbox (accordion)

Need help with a flexbox (accordion) issue. The demo below showcases multiple groups of content, each contained within a cell in a horizontal flexbox. However, when the content within a group is extensive, it overflows the flexbox causing display problems. ...

What is the best method to extend my borders to the very edge?

Struggling with table borders extending to the edge on a website using master pages and CSS stylesheet. This issue requires some troubleshooting, so any help would be appreciated. The problematic website is here. I need the border under the banner menu to ...

Mastering the ngIf Directive in Angular: A Comprehensive Guide

Before you approve and publish, please take note: You are about to give your approval for the Industry Formile Deliverable. The content will be rendered when the specified condition is met. div class="form-group" ...

Does .NET MVC provide the necessary separation of HTML/CSS/JS for my project?

Currently, I am collaborating with my ASP.NET development team in an effort to improve the quality of our HTML output. We are facing challenges due to ASP.NET's tendency to insert JavaScript directly into the page, creating dependencies on JS for form ...

Imitate the actions of images with {width: 100%; height : auto} properties

I am interested in creating a unique layout that consists of a stripe composed of images with varying widths and heights. These images need to be proportional, scaled at the same height, and collectively have a width equal to the parent element. However, ...

Steps to modify the background-color of an iFrame using CSS

I am attempting to extract HTML code from an iframe and apply a background-color to the class: class="body" The structure of my HTML source is as follows: <iframe id="sc_ext_XT29EK" class="sc_ext" width="100%" height="1300px" frameborder="0" src="some ...

Leveraging JavaScript to trigger onClick() functions for multiple buttons throughout a webpage

        While searching for solutions, I came across several articles with similar topics, but unfortunately, none of the proposed solutions worked. Please forgive me if it was due to my own error. Background I am assisting a client who is transition ...

Update the font style of the navigation bar

Hey there! I recently downloaded a template from this website: . However, I'm facing an issue with the nav bar displaying strange fonts when using Vietnamese letters. For example, "Nước" shows up as all uppercase and the letters "uo" are shortened. ...

Is it possible for me to dynamically alter the innerHTML of an input element using

I am working on a small Angular application. One of my components uses innerHTML to display content. <div [innerHTML]="serviceShared.currentMood"></div> However, when I change the value of serviceShared.currentMood in the main component, noth ...

The natural flow of CSS floats is towards the right side

From my understanding, float elements typically behave in this way. They fill the entire space from left to right, which is expected. My question is how can we achieve this behavior? The float element would first fill up the bottom part of the first colu ...

Prevent further scrolling on the slider by utilizing a link with the target ID in pure CSS

I've created a CSS slider, but I'm facing an issue where clicking the bullet and arrow navigation scrolls to the top of the slider from the extra space. Is there a way to prevent this behavior using only HTML and CSS? I've tried using a hre ...

Modern UI Styling for ASP.NET MVC with Metro Design

I am interested in incorporating the METRO UI CSS into my application: Begin by creating an ASP NET MVC Project To download METRO UI MVC, follow these steps: Package Manager Console: PM> Install-Package Metro.UI.CSS Adjust the bundles to incl ...

Trouble Arising in Showing the "X" Symbol upon Initial Click in Tic-Tac-Toe Match

Description: I'm currently developing a tic-tac-toe game, and I've run into an interesting issue. When I click on any box for the first time, the "X" symbol doesn't show up. However, it works fine after the initial click. Problem Details: ...