Adjusting the height of one div to match another dynamically

I'm fairly new to HTML and CSS, but I have a basic layout in mind that I'd like to achieve. Currently, I have two "child" divs set up in a row-based flexbox format controlled by the main "parent" div. One of these child divs contains images, while the other has text content. My objective is to adjust the dimensions of the images relative to the size of the text; so that if I resize the text, the images will also resize proportionally.

I initially believed that this could be achieved using height: auto and height: 100% in different sections, like this:

HTML:

<div class="parent">
  <div class="child-img">
    <img>
    <img>
  </div>
  <div class="child-txt">
    <p>Title Text Here</p>
  </div>
</div>

CSS (excluding flexbox code, focusing on height):

.parent {
  height: auto;
}

.child-img {
  height: 100%;
}

.child-txt {
  height: auto;
}

img {
  height: 50%;
}

p {
  font-size: xx-large;
}

The thought process behind this was:

  • .child-txt has its height set to auto, therefore adjusting to fit the content height, which in this case is the <p> block with font-size set to xx-large
  • .parent has a height value of auto, implying it should inherit the height from .child-txt
  • .child-img has a height value of 100%, indicating it should take up the vertical space within .parent
  • img has a height value of 50%, meaning it should occupy half the vertical space inside .child-img

However, following this structure actually allows the images to expand to their full sizes, suggesting that .child-img isn't restricting its height based on .parent. Why is this happening? Are there alternative methods to realize my intended design?

Answer №1

Don't forget to add a quotation mark to the parent class:

<div class="parent">
  <div class="child-img">
    <img src="path_to_image1.jpg" alt="Description of image1">
    <img src="path_to_image2.jpg" alt="Description of image2">
  </div>
<div class="child-txt">
   <p>Title Text Here</p>
</div>

Hope this correction is useful for you! Best of luck :)

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

Interactive Material Icons with Hover Effects in CSS

I'm having trouble adding a hover effect to material icons in my code. Despite everything else looking good, the hover effect is not working as expected. Below is the code snippet I've been using: import ManageAccountsIcon from '@mui/icons-m ...

Is there a way I can ensure the values are loaded when the page loads, rather than displaying NaN?

I've recently created a car rental calculator for a client, and it's almost complete. Everything is working smoothly, from the calculations to the conditions. However, I'm facing an issue where the price isn't calculated on page load. I ...

Is it feasible to utilize Socket.io and Express in conjunction with templates without the need for routes?

Apologies for what might be considered a beginner question, but I'm curious if it's possible to run a Socket.io event server in one folder and have an HTML page utilize that server from a different folder. The reason for this question is because ...

`The functionalities of classList.add and classList.remove aren't behaving as anticipated.`

I'm currently working on a list of items (ul, li) that have a class applied to them which adds a left border and bold highlight when clicked. My goal is to reset the style of the previously clicked item back to its original state when a new item is c ...

The categorization of items into arrays according to selections made with radio buttons is producing varied outcomes

I currently have two groups of radio buttons: One for attendance with values "yes" and "no" Another for dietary choices with values "Vegetarian / Vegan" and "Non-vegetarian" The dietary fields are only displayed if the user selects "yes" for attendance. ...

Elements not following percentage in top positioning

I am facing an issue with resizing containers on my webpage. Despite using position: relative for everything, I am unable to properly adjust the size of the top containers using percentages. Any assistance would be greatly appreciated! ...

Differences in vertical font positioning between Mac and Windows systems

One element on my page is a quantity balloon for a basket, but I'm struggling to get it looking right. The font and vertical position don't seem to be cooperating for such a simple element: https://i.sstatic.net/IfSi6.png Despite trying solutio ...

Trigger JQuery function post CSS styling completion

Upon using $(document).ready(function() {...}, it seems that the function is invoked after the DOM has loaded, but potentially before CSS styles have been completely applied. My goal is to obtain the width of a particular element. Strangely, when attempti ...

Ways to access the scrollTop attribute during active user scrolling

I've been working on a website that utilizes AJAX to keep a chat section updated in real-time. One issue I encountered was ensuring the chat automatically scrolled to the bottom when a user sent a message, but remained scrollable while new messages we ...

Updating pages dynamically using AJAX

There's a glitch I can't seem to shake off. I've successfully implemented AJAX for page loading, but an issue persists. When I navigate to another page after the initial load, the new page contains duplicate tags like <head> and <f ...

unable to expand navbar in Angular 5 project with Bootstrap 4

I am encountering an issue with my navigation bar in a project that uses Angular5 and Bootstrap4. The navigation bar is supposed to expand when the screen size is small, as indicated by the navbar-expand-sm class in Bootstrap4. However, the navbar remains ...

What is the significance of z-index in relation to relative and absolute positioning?

Is there an issue with z-index when working with a div that has absolute position relative to another div? I am trying to place the absolute div below the other one, but it always appears on top. How can I resolve this problem? ...

What is the optimal tech solution for creating a cross-browser introduction video that is compatible with both iPhone and IE6?

We are in need of an introductory video for our website that must be compatible with all browsers, including Safari on the iPhone and IE6. Our plan is to use a combination of flash with HTML5 fallback or vice versa. Has anyone tried this before? We want t ...

``It seems like the Tailwind background image is not appearing on

The issue lies in the background image not displaying despite trying different methods such as using ""bg-[url('/image/example.jpeg')"" tailwind.config.js theme: { extend: { colors: { darkBlue: "#0 ...

Can you tell me about the feature called "Inspect Browser" box?

Can you identify the significance of this enigmatic purple box while inspecting elements in the browser's developer tools? It does not correspond to padding, margins, or borders, yet it seems to be occupying space within the designated div. [1]: http ...

Unfortunately, the CSS3 feature of 'transform: scale()' does not function properly on any web browser except for Firefox

I am currently utilizing Vue CLI 3. Within the CSS, I have implemented a hover effect that scales up by 10% when hovering over the div. Here is the snippet of code: (this.boxData.scaleOnHover ? `-webkit-transform: scale(${this.scale}%); -moz-transform: ...

Where can I find the specific code needed to create this button style?

There is a button here for adding an event, but unfortunately, the style code is not displayed. Is there a way to obtain the full cascading style sheet for this button? ( ) ...

W3C validation issue: "Failure to immediately follow a start-tag with a null end-tag" and similar errors

Encountering validation errors with the following code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <hea ...

Automatically updating the results section while executing SQL queries in PHP

Here is a JavaScript/Ajax code snippet: <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready (function () { var updater = se ...

Steps for placing a menu link at the far right edge of the menu

I'm looking to customize my NAVBAR by adding a Donate Link Menu on the right side, next to the search icon. Can anyone help with this simple task? Thank you in advance! Here is the link to the website: www.clihelp.org Below is the menu code for my N ...