Designing two stacked divs below two adjacent divs

Struggling to figure out how to achieve a simple alignment:

I want a line with 2 side by side divs, the first one taking up its necessary space and the second occupying the remaining space (this part is easy with inline-block or float)

The right div should have two overlaying child divs, both taking up 100% of the parent div on the right (this is where I'm stuck).

I am trying to create a progress bar with a label on the left and a percentage label on top of the progress bar. The text in the progress label needs to be centered (hence the need for it to take up 100% of the parent div)

If anyone has any suggestions, please share!

Here's an illustration of my issue:

<div id="all">
    <div id="left">
        left
    </div>
    <div id="right">
        right
    </div>
</div>

#all {
    width: 200px;
    height: 100px;
    background: grey;
    border: 1px solid;
}

#left {
    background: red;
    float: left;
    height: 100%
}

#right {
    background: blue;
    display: block;
    height: 100%;
}

Check out this jsFiddle link for a visual representation: http://jsfiddle.net/a9cnH/3/

The issue at hand: struggling to position 2 divs inside the "right" div, each one taking up 100% of the right div. If I use absolute positioning on them, then I have to change the position of the "right" div from "static", which throws off the alignment next to the left div.

Answer №1

To achieve a stacking effect with divs, the key is utilizing z-index and positioning them absolutely. Take a look at this sample code:

   <div style="border: 1px solid red; width: 300px; height:200px; background-color:White;position:relative;">
        <div style="width:50%; height:100%; background-color:Red; z-index:2; float:left; "></div>
        <div style="width:50%; height:100%; background-color:blue;z-index:2; float:right; "></div>
        <div style="width:50%; height:50%; background-color:green;z-index:3; top:25%; left:25%; position:absolute; "></div>   
   </div>

Answer №2

Success at last!

I realized that in order to achieve the desired effect, I needed both the left and right divs to have overflow hidden. Once I made this adjustment, I was able to set the position of the right div to relative.

<div id="all">
    <div id="left">
        left
    </div>
    <div id="right">
        <div id="right1">
            r1
        </div>
        <div id="right2">
            r2
        </div>
    </div>
</div>

#all {
    width: 200px;
    height: 100px;
    background: grey;
    border: 1px solid;
}

#left {
    background: red;
    height: 100%;
    float: left;
    overflow: hidden;
}

#right {
    background: blue;
    height: 100%;
    overflow: hidden;
    position: relative;
}

#right1 {
    width: 100%;
    background: green;
    position: absolute;
}

#right2 {
    width: 100%;
    position: absolute;
    text-align: center;
}

Check out how it looks here: http://jsfiddle.net/nSD66/

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

Difficulty with Line Breaks in Spans

I've noticed that my multi-line address in the footer is not breaking correctly at certain screen widths. Each line of the address is enclosed within a <span> tag with a specific class and then styled either as block or inline-block in the CSS ...

Is there a way to eliminate duplicates from an array in JavaScript by utilizing a set and placing each element in an li tag? Here is the code

const numbers = [" 4", "9", "16"," 4", "9", "16"," 4", "9", "16"] <ul> {(<li> {[...new Set(numbers)]} </li>)} </ul> const numbers = [" 4", "9", "16"," 4", "9", "16"," ...

Code snippet in webpage body

Hey there, I could really use some assistance: I currently have the following: A) Login.html B) Documentation.html C) Base.html Within the login page, there is a form with fields for User and Password. In Documentation, there are links to various folder ...

Adjustable Greybox Overlay Resizes Window Dimensions

Utilizing greybox on my business website allows users to easily log in and access their accounts. View an example of my site However, when the login or sign up window is activated at the top of the page, the page or window size changes, causing scroll bar ...

Tips for customizing the ui-select element's stylesheet in Angular

I created a small program in Angular using a ui-select component. The issue I am facing is that the search bar is too long and I'm struggling to figure out how to shorten it. Typically, you can adjust the width properties in CSS to make changes like t ...

The label for a custom file input is not displayed correctly in Bootstrap 4

I'm attempting to show a label above a custom file input field and another label inside it using bootstrap 4. I have tried the code below but encountered an issue where 'label 1' is not being displayed. <label class="w-100">label 1 ...

Issue with the formatting of the disabled button

I am facing an issue with styling a disabled button. Whenever I try to apply custom styling, it reverts back to the default styling. I have already cleared my cache but the problem persists. The button is disabled using JavaScript with document.getElementB ...

Accessing a hyperlink within the existing page

How can I make a hyperlink in an HTML document that opens the linked document (link1.html) under the link on the same page? links.html: <body> <h3>Links</h3< <a href="link1.html">link1</a> </body> Desired out ...

Overlapping Dropdown Javascript Menus

To achieve the desired effect in the header, I need to create two expandable dropdown menus for the first two titles. My coding experience is limited, especially when it comes to javascript and jquery. Currently, I have managed to create the effect for one ...

What impact does zoom have on the width of the image?

For more information, you can check out the code here: http://codepen.io/anon/pen/VvdyQb?editors=010 I'm struggling to comprehend how the viewport width changes when zooming in or out. My media queries are activated based on changes in width measured ...

Angular 6 CSS spacing dilemmas

We recently made the switch from Angular 5 to Angular 6 and noticed that there is no spacing between the buttons/icons, etc. We are looking to bring back the spaces between the buttons and icons. I have recreated the issue below. As you can see in the Ang ...

The absence of an eye icon in the password field when using bootstrap 5

I'm having trouble positioning the eyeball icon to the right side of my form when using Bootstrap v5 and FontAwesome v5. Instead, the password field is being shifted further to the right compared to the username field, and the eye icon is not displayi ...

Ensure that the div is aligned properly with the div above

I've been struggling with the alignment of divs on my website for the past day. Any tips on how to properly align the image (the text image) inside a div? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1 ...

JavaScript Evaluation Test Outcome

(Apologies, I am still in the process of learning JavaScript) My goal is to create a checklist quiz that provides different outcomes based on the number of checkboxes selected by the user. There are a total of 7 checkboxes, and if the user selects 1 or few ...

Retrieve the erased document using Google's cache feature

After accidentally overwriting my .php web document file with an old version, I attempted to access the cached copy on Google. The only thing I could find was the HTML scripts, as the PHP coding was not visible. Now I am seeking a method to recover the e ...

Troubleshooting issue with Jquery animation from left to right

What's the issue with this code? It is supposed to animate the arrow from left to right and then back to left again repeatedly, but it's not working as expected. Any ideas on why that might be? function moveRight(){ $('#myIcon'). ...

menu featuring a creative favicon and an accompanying label

I am aiming to replicate this menu design: It consists of a flavicon with a label below it. Menu I attempted to create it in two different ways, but both attempts were unsuccessful. Using Bootstrap groups. <link rel="stylesheet" href="https://u ...

Creating dynamic tables with jquery mobile

When the screen width decreases, I need to find a way to position 3 tables below each other without using float or breaking the jquery mobile layout. Using float: left; on the surrounding containers of each table is not an option as it disrupts the jquery ...

Get the Bootstrap download that includes CSS files rather than the LESS files

What is the reason behind bootstrap switching to only having LESS files instead of the traditional bootstrap.css file? Is there a way to download the bootstrap framework that includes the CSS files instead of just the LESS files? ...

Styling a div element in CSS so that it adjusts its size to fit its

How can I make a div element adjust to its content and also break the line so that the next element appears below it? I attempted setting the display property to inline-block, but then the div no longer breaks the line... Normally, it breaks the line, but ...