Creating a div's height to dynamically adjust based on another div that is positioned absolutely

I am working on a web design project where I want the height of the content division to adjust based on its nested divs. Here is how I have set it up:

<div id="content">
    <div id="col1">content</div>
    <div id="col2">content</div>
</div>

These are the CSS styles I am using:

#content {
height:auto
position: relative;
}

#col1 {
width: 30%px;
height: 100%;
position: absolute;
left: 0px;
top: 0px;
}

#col2 {
width: 68%;
height: auto;
position: absolute;
right: 0px;
top: 0px;
}

The issue is that when the content height is set to auto, the div collapses instead of adjusting to match the taller column (#col2).

Do I have to manually set a specific height for the div to prevent it from collapsing, or is there another solution that I am overlooking?

Answer №1

Absolute positioned elements are removed from the normal flow of the page, so achieving your desired layout with the current CSS is not possible.

Instead, consider using floats:

#content {
    height:auto;
    width:300px; /* set a width */
    float:left;
}

#col1 {
    width: 30%;
    float:left;    
}

#col2 {
    width: 70%;
    float:right;
}

For an example using your HTML markup, you can refer to this link: http://jsfiddle.net/x3ebK/3/

Answer №2

To achieve this layout, you can use absolute positioning for the right-hand column, setting it at both the top and bottom. This way, it will expand along with the outer relative container as it grows. Here is an example demonstrating this technique: http://jsfiddle.net/x3ebK/4/

#content {
    height:auto;
    position: relative;
    width:100%;
    float:left;
    background:#ccc;
    color:#fff;
}

#col1 {
    width: 30%;
    background:#ff0000;
}

#col2 {
    width: 70%;    
    background:#000fff;
    position: absolute;
    top: 0;
    bottom: 0;
    right: 0
}

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

CSS: Is it possible to make a div even smaller than the invisible padding of a font?

link to code: http://jsfiddle.net/xVCrn/1/ (best viewed in chrome / webkit) I'm attempting to create a 1px margin for the red section within the dark button area, but I am struggling to adjust the height of the red part. =( The objective is to: ...

Converting HTML content into a single string simplifies data manipulation and extraction

exampleHTML=" This is sample HTML code that needs to be converted into a string using JavaScript </p>" I am looking to transform it into a single string format like str="This is sample HTML code, that needs to be converted into a string ...

Utilizing Javascript and CSS for horizontal alignment from left to right

I have a JavaScript function that generates a list of store locations and creates a DIV beneath the map. Currently, the items are displayed top to bottom on the page. However, I would like to change the layout so that the items are shown as 3 in a row fro ...

What are some methods to customize the CSS subnavigation without changing the appearance of the main navigation

http://jsfiddle.net/uuyVY/ screenshot https://i.sstatic.net/CzUPk.png I'm attempting to configure the subnav to be white and span the entire containing div (which is 1100px wide), with only the text links displayed vertically. However, I'm stru ...

The Bootstrap theme showcases a narrower layout than its parent container

I'm struggling with managing the width of the bootstrap container and row. Inside the row, I've placed all the content, but for some reason, the sizes are showing differently on jsfiddle and the image preview. <!DOCTYPE html> <html lang ...

Distinguishing each unique JavaScript property within an array of objects

I've been struggling with this problem for quite some time. I have an array of objects, focusing on the "00" object at the moment, and I am trying to group together the bestScore properties in a specific way: .. User Group apple .. User Group ba ...

Exploring the World of Bootstrap 4 Cards

Just starting out in UI/UX, I played around with some gallery transitions featuring hover effects, and incorporated Bootstrap cards into the mix. When I hover over the image (which was blank at first), it now displays an overlay with the intended text, alo ...

Button for Selecting Colors

I love the color picker button on Google Keep, where a variety of colors pop up for selection. I'd like to add something similar to my website. What steps should I take to implement this feature? ...

Learn how to configure an Angular2 Template Driven form element by implementing two-way binding and integrating template driven form validation techniques

Can anyone help me figure out the correct way to set up an Angular template driven form element for both validation and two-way binding? I've tried using ngModel in different ways, but cannot seem to achieve two-way binding without encountering issues ...

Tips for Retrieving Html Element Attributes Using AngularJS

Update: Even though the discussion veered off track, the main question still stands - how can I access an attribute of an HTML element within an Angular controller? Check out my attempt on Plnkr: http://plnkr.co/edit/0VMeFAMEnc0XeQWJiLHm?p=preview // ...

Expanding the Bootstrap panel to a specific size with a scrollable panel-body

Is there a way to create a panel in bootstrap with specific maximum and minimum lengths? My goal is to set a fixed length for the panel body and enable a scroll bar if the text overflows that length. I've been attempting to increase the length of the ...

Position the image slider uniformly at the bottom using CSS (utilizing the Slick Carousel library)

I am utilizing the slick carousel library to create a unique and elegant slider. However, when using images with varying heights, I am encountering issues in aligning the images at the same bottom: https://i.stack.imgur.com/ab5s3.png Here is my code snip ...

What is the best way to incorporate a button that, when clicked, reveals two separate images on the frontend?

describe food option heredescribe juice option hereHow can I create a button using HTML, CSS, JavaScript, and Bootstrap that displays different images for food and juices when clicked? For example, clicking on "food" will display 3 different food images, w ...

Having trouble limiting the number of special characters in AngularJS

I am having trouble restricting special characters and spaces in the input text field. I tried using the following code snippet: ng-pattern="/^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]+)$/" to prevent special characters, but it doesn't seem to be workin ...

Issue in Laravel5.8: Unable to collapse the HTML code retrieved from the database

How can I display HTML code from the database without affecting the functionality of the Collapse feature? The Collapse feature works fine when I don't decode the HTML, but it stops working when I decode it. Why is this happening? <!-- START TAB ...

Using Material UI's DropDownMenu to achieve full height coverage

On a specific page, I have a DropDownMenu embedded inside a table column. Whenever I try to open the menu, it expands to 100% of the page height. I couldn't find any information regarding this issue in the documentation or community forums. The chi ...

After reducing the size of the table, the data spills over

Hey there, amazing individuals of the internet! Hope you're all doing well today. I've encountered a rather perplexing issue that's got me stumped. So, here's the situation - I have this table full of data and I want it to initially be ...

Stopping halfway through a jQuery toggle width animation to close again

Perhaps the question may be a bit unclear, but allow me to provide an example. When repeatedly clicking the button that toggles the width, the div continues to animate long after the button press, which is not visually appealing. My desired outcome is for ...

Utilizing HTML to hide rows within a table by comparing each row with the one that comes before it

I am in need of assistance to solve a project I am working on that involves a table structure similar to this: https://i.sstatic.net/U0AI4.png Below is the code snippet I am currently using: <table class = "table table-striped table-hover"&g ...

What is the process for accessing my PayPal Sandbox account?

I'm having trouble logging into my SandBox Account since they updated the menu. The old steps mentioned in this post Can't login to paypal sandbox no longer seem to work. Could someone please provide me with detailed, step-by-step instructions o ...