What is the best way to adjust the height of a div element according to the width of its child element?

I am facing an issue with two nested divs, where the inner one is absolutely positioned inside the outer one. I want the height of the outer div to adjust dynamically according to changes in the width of the inner div. To better illustrate the problem, I have created a simple example on JSFiddle:

http://jsfiddle.net/2GTDd/5/

Currently, the width of the inner div is fixed at 200px and so is the height of the outer div. Is there a way to make the width of the inner div dynamic based on its content, while also automatically adjusting the height of the outer div to match it? I know this can be achieved using JavaScript, but I would prefer a non-JavaScript solution if possible.

The code snippet provided in the fiddle is as follows:

<div id="outer">
    <div id="inner">
    </div>
</div>
#outer {
    width: 100px;
    height: 200px;
    position: relative;
    background-color: red;
}
#inner {
    width: 200px;
    height: 100px;
    position: absolute;
    background-color: green;
}

Answer №1

Here is the solution provided:

Fiddle

CSS

#outer {
    width: 200px;
    height:100px;
    position:relative;
    background-color:green;
}
#inner {
    position:absolute;
    width:100%;
    height:100px;
    background-color:red;
    -ms-transform: rotate(90deg); /* IE 9 */
    -webkit-transform: rotate(90deg); /* Chrome, Safari, Opera */
    transform: rotate(90deg);
}

Update:

Due to rotation from the center, the CSS output was not precise. Therefore, I applied transform-origin:0 0; in the CSS to rotate the div from the left instead of from the center:

Updated Fiddle

Answer №2

If you want to rotate an element using CSS3, check out this example on jsFiddle.

Here is the CSS code:

#box {
    transform: rotate(45deg);
    -ms-transform: rotate(45deg);
    -webkit-transform: rotate(45deg);
    transform-origin: center;
    position: relative;
    background-color: blue;
    width: 200px;
    height: 100px;
}

Answer №3

Give this a try.

#container {
    width: 150px;
    height:250px;
    position:absolute;
    background-color:blue;
}
#box {
    width:250px;
    height:auto;
    position:relative;
    left:50%;
    background-color:yellow;
}

Fiddle Demo

Answer №4

This example doesn't provide a complete solution to your issue. However, I believe it can help you make some progress. It demonstrates how to achieve an aspect ratio where the height equals the width using only CSS.

Click here for the example

Here is the HTML code:

<div class="box ratio1_2">
    <div class="content">Aspect ratio of <span>1:2</span></div>
</div>

And here is the corresponding CSS:

.ratio1_2:before {
    padding-top: 200%;
}
.box:before {
    content: "";
    display: block;
    padding-top: 100%;
}
.box {
    display: inline-block;
    float: left;
    font-size: 12px;
    margin: 5% 5% 0 0;
    position: relative;
    width: 40%;
}

.content {
    background: none repeat scroll 0 0 #000000;
    bottom: 0;
    color: #FFFFFF;
    left: 0;
    padding: 30px;
    position: absolute;
    right: 0;
    text-align: center;
    text-transform: uppercase;
    top: 0;
}
.content span {
    display: block;
    font-size: 3em;
    font-weight: bold;
}

Answer №5

To achieve the desired result, set the height of your main container to auto

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

Is there a way to condense my child table directly below its parent column?

When attempting to collapse the "child tr" within the "tr" in my code, the collapse is not positioning correctly underneath its parent tr. While it works fine in a JSFiddle, the issue arises when using my editor and live preview. In this scenario, both "ch ...

An issue occurred during the compilation of an Angular6 project

I am embarking on my first Angular project and may not grasp every detail perfectly. In my search for guidance, I came across the command "ng build --prod" on Google and decided to give it a try. Everything seemed to be going smoothly at first until an err ...

If the element is checked and equal to a specific value, take action

I am trying to hide one of the 3 radio buttons on my page. Although they all have the same class, each button has a different value. I attempted to write code to achieve this, but unfortunately, it is hiding all radio buttons instead of just one. Could s ...

How to achieve divs with see-through text using CSS?

How can I create a CSS (non-HTML5) based website with a filled div that has a cutout revealing an image underneath? There are various overlays and images involved, making the use of static images inconvenient. Additionally, I anticipate needing to scale th ...

Navigating a local and server environment with relative paths in a web server's multiple sites

My ASP.NET website is published to a web server with multiple sites, such as www.example.com/SiteA or www.example.com/SiteB. Before publishing, I test the site locally at localhost:12345. When referencing an image path like /Images/exampleImage.gif, it wo ...

How to access a particular tab in Bootstrap 5 using an external link

Is there a way to direct users to a specific tab upon clicking a link on another page? Check out the example below: <div class="products-btn"> <a href="products.html#pills-profile">view all</a> </div> On Pro ...

How to give a stylish touch to your :after element with a css

I have a unique bubble design that pops up when hovering. I'm trying to add a border around the bubble, but I'm facing difficulty adding a border to the pointer of the bubble. The arrow is created using the .bubble:after selector. You can check ...

Tips for incorporating transition animations into route changes in next.js

On the homepage, there are 2 links that I want to have distinct transition animations for. For example, if I click the link on the left, the animation should start from the left and move towards the right when changing routes. ...

Verify whether an accordion is in the open or closed position

I am trying to determine if an accordion item is currently open or closed in Framework7. Can anyone provide guidance on how this can be accomplished? To open an accordion item, I have been using the following code: myApp.accordionOpen(".accordion-item"); ...

How to customize JavaFx context menu appearance using CSS to achieve a full-width background color

I am trying to customize my context menu, but I'm having trouble with the white space between the menu item and the context menu border. Here is an example: view the rendered context menu with CSS styles What I want is for the color to fill the entir ...

The sass compiler has produced two separate CSS files, but I am only able to link one of them to the index.html file

Everything was going smoothly with my SASS compiler. The styles were reacting perfectly in the live web server. But then, out of nowhere, it stopped working. Upon closer inspection, I realized that the extension had created a new CSS file. Instead of compi ...

Codeception is unable to interact with an element that it recently encountered

Greetings to the wonderful users of Stackoverflow, Currently, I am in the process of creating acceptance tests using Codeception with Selenium as the WebDriver module. I have encountered an issue while trying to verify the existence and functionality of o ...

The background on my modal remains unchanged

I have integrated a modal using Iframe for user login/registration on my website. Here is the HTML code: <div class="modal" id="ays-popin-connexion" aria-hidden="true"> <div class="modal-body" aria-hidden="true"> <iframe src=" ...

Tips for automatically closing a dropdown menu when it loses focus

I'm having some trouble with my Tailwind CSS and jQuery setup. After trying a few different things, I can't seem to get it working quite right. In the code below, you'll see that I have placed a focusout event on the outer div containing th ...

What steps should be taken to ensure that the onmouseover and onmouseout settings function correctly?

The Problem Currently, I have a setup for an online store where the shopping cart can be viewed by hovering over a div in the navigation menu. In my previous prototype, the relationship between the shoppingTab div and the trolley div allowed the shopping ...

Tips for avoiding cropped images on mobile websites:

I recently created a website that looks perfect on desktop but appears cropped on mobile devices. The site in question is doc.awsri.com Here are the images causing the issue: https://i.stack.imgur.com/eRJXU.png The problem arises when viewing it on a ph ...

What is the best way to save a JavaScript array in HTML5 local storage?

I am looking for a way to store an array of inputs using the local storage feature of HTML5. Currently, I am able to add weight inputs to the array, but they disappear once I refresh the page. Can anyone assist me with this? <!DOCTYPE html> <html ...

Executing a function within a ng-repeat iteration

Is there a way to call a method within an ng-repeat loop, even if the element does not exist at that moment? I'm facing this issue and I'm not sure how to resolve it... The ID seems to be correct. Strangely, when I run it in the console after ev ...

Using jQuery to create a div that animates when scrolling

Recently, I came across this interesting code snippet: $(document).ready(function(){ $(window).scroll(function(){ if($("#1").offset().top < $(window).scrollTop()); $("#1").animate({"color":"#efbe5c","font-size":"52pt", "top":"0", "position":"fix ...

modify CSS style once the modal has been hidden

Is there a way to change the default background color of my table row back to normal after closing the modal window that appears when I click on it? <tr class='revisions'> <td>{{ $revision->date_of_revision->format(' ...