Ensure that the image within the div consistently occupies an equal amount of space at all times

<div id=“parent”>
    <div id = "child1">
       <img src="123"/> 
   </div>
   <div id = "child2"> 
      <p> Some text1 </p>
      <p> Some text2 </p>
   </div>
</div>

In my HTML structure, the parent div has a fixed height and always contains two child containers:

  • The first child container is a div with an image.
  • The second child container is a div with some data.

Sometimes, the size of the image inside the first child container varies. When the image is larger, it occupies more space in #child1 which results in the text in #child2 being partially hidden. I can see the "Some text1" paragraph but not "Some text2".

I am looking for a solution to make the image inside #child1 always take up the same percentage of space within #parent, possibly by zooming it in proportionally.

Answer №1

Set the width for #child and #child2, then ensure that the img element takes up 100% of its parent's width.

.parent {
  height: 400px;
  width: 500px;
}

.child {
  width: 49%;
  display: inline-block;
}

.child img {
 width: 100%;
}
<div class=“parent”>
    <div class="child">
       <img src="https://thumbor.forbes.com/thumbor/1280x868/https%3A%2F%2Fspecials-images.forbesimg.com%2Fdam%2Fimageserve%2F42977075%2F960x0.jpg%3Ffit%3Dscale"/> 
   </div>
   <div class="child"> 
      <p> Some text1 </p>
      <p> Some text2 </p>
   </div>
</div>

Answer №2

Flexbox is truly remarkable for achieving this layout. The CSS comments provided explain the purpose of each line.

* {
  box-sizing: border-box;
  /* Including both border & padding in width calculation */
}

.parent {
  display: flex;
  /* Utilizing flexbox for layout */
  background: papayawhip;
}

.child {
  flex: 0 0 50%;
  /* Defining layout of child elements relative to parent .parent, using shorthand notation for flex-grow: 0; flex-shrink: 0; flex-basis: 50%; Consider flex-basis as initial width setting */
  border: 1px solid dodgerblue;
  padding: 20px;
  /* Adding space around edges inside */
}

#child1 {
  display: flex;
  /* Applying flexbox layout to child1 wrapper */
  align-items: center;
  /* Vertical alignment */
  justify-content: center;
  /* Horizontal alignment */
}

#child1 img {
  max-width: 100%;
  height: auto;
}
<div class="parent">
  <div id="child1" class="child">
    <img src="https://picsum.photos/200/300" />
  </div>
  <div id="child2" class="child">
    <p> Some text1 </p>
    <p> Some text2 </p>
  </div>
</div>

Answer №3

To ensure the height of the child elements does not exceed 250px, adjust their height and use a background image. Implementing the overflow property will help contain the content within the specified height.

#parent { 
  height:500px; 
}
#child1, #child2 { 
  height: 250px; 
  overflow:auto; 
  width:100%;
}
#child1 { 
  background-size:contain;
  background-position: center;
}

Here is an example of the HTML structure:

<div id="parent">
    <div id="child1" style="background-image:url('path');"></div>
    <div id="child2">Text</div>
</div>

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

Retrieve corporate information by clicking a button with the help of jQuery

I have set up a table called company_list with the following fields: 1) Company Name 2) Country 3) Vertical Let's say there are 5 companies in this table: Company Names: Water, Earth, Sun, Moon, Star Countries: Water= UK, Earth=UK, Sun=Australia, ...

Using the factory pattern in a Node.js (Express) application

As I delved into the realm of design patterns, I found myself drawn to the factory pattern. However, as I perused through code written by others, I noticed that this particular pattern wasn't as prevalent, especially in newer stacks. Take for example ...

Are MEAN stacks and boilerplates suitable for custom applications requiring specialized functionality?

After spending a significant amount of time learning and coding, I have encountered a common issue. You can find the link to the discussion below. Instead of reinventing the wheel, should I utilize existing boilerplates that combine Express, Angular, conn ...

Ar.js results in objects experiencing deformation or modification when manipulated

Recently, I started experimenting with Ar.js and encountered an issue where the objects displayed on the screen seemed distorted. To illustrate this problem, let me share a basic A-Frame example featuring a perfectly round sphere: <!DOCTYPE> <html ...

additional dark border streak

When you resize the browser window, you will notice a different layout for iPhone. I am seeing an additional black line below the slider and I'm unsure how to remove it. I have already tried removing the border property but the black line remains. Bel ...

Troubleshooting an issue with IE regarding box-shadow CSS functionality

I have a problem with my website design where the box-shadow effect on the top menu is not displaying properly in Internet Explorer. I tried adding specific code for IE8 and lower versions to fix this issue: zoom:1; /* This enables hasLayout, which is nec ...

"The Ajax function for replacing a div upon change event is not functioning properly

I am having an issue with my select box using the onchange event, <select class="form-control" onchange="getval(this.value,'<?php echo $prd->pr_id;?>','ajax<?php echo $key?>','<?php echo $key ?>')"&g ...

Design a clickable div element embedded within an interactive article using HTML5

Currently, I am facing an issue with placing clickable div elements inside an article tag in HTML5. The problem is that when I try to click the divs, it registers as a click on the article itself. Below is an example of my code: HTML: <article id=&apo ...

Using Bootstrap Select with a callback function

I am currently working with 2 Bootstrap Select dropdowns. One dropdown contains a list of countries, while the other contains a list of states. The country list is static and loads when the page is loaded. On the other hand, the state list is populated dyn ...

What is the best way to locate the position of a different element within ReactJS?

Within my parent element, I have two child elements. The second child has the capability to be dragged into the first child. Upon successful drag and drop into the first child, a callback function will be triggered. What is the best way for me to determi ...

The section element cannot be used as a <Route> component. Every child component of <Routes> must be a <Route> component

I recently completed a React course on Udemy and encountered an issue with integrating register and login components into the container class. The course used an older version of react-router-dom, so I decided to upgrade to v6 react router dom. While makin ...

What is the best way to set the z-index for elements within CSS containers that are utilized for container queries?

I am thrilled about the introduction of container queries in CSS. It's great to see this feature now available across all browsers. However, I have encountered an issue when working with multiple consecutive containers and placing an absolutely positi ...

After submitting the form, the delete button in Bootstrap 4 has transformed into an unusual shape

I have a panel on my page, inside the panel header there is a red delete button. The panel content includes a form with several buttons (the red delete button is not inside the form, but linked to it with the form="abc" attribute). In the image below, whe ...

Ways to stylize bullet points in lists with CSS in HTML document

I am currently working on a Q&A page using simple HTML. This is the current appearance of my page. Check it out here on JSFIDDLE. My task involves adding an 'A' to the answer for each question. Some answers have multiple paragraphs, so the ...

Utilizing various Firestore requests at varying intervals using the useEffect hook in React

useEffect(async() => { await getWord(); const interval = setInterval(() =>{ time2 = time2 - 1; if (time2 == 0) { clearInterval(interval); let dataURL = canvasRef.current.toDataURL(); const db = firebase.firestore(); ...

Guide on iterating through an array of objects a specified number of times to ensure it loops through all child arrays within the objects, if they exist

How can I iterate through an array of objects a certain number of times to access all the child arrays within each object and retrieve their IDs in order? let data = [{ "id": "1", "child": [ { "id": "12", "child": [ { ...

Steps to dynamically modify an HTML element within an Android WebView

https://www.bing.com/search?q=кму here I want to switch the bing icon to google I attempted : if (url == "https://www.bing.com/search?q=") { view.evaluateJavascript( ("\n"+ "wind ...

How to Link an Object's Value to a Checkbox in Vue

My goal is to populate the array selectedParks with the value of each item. However, I am facing an issue where the value is always set to the string "item" instead of the actual value of the Park Object. Here is the code snippet: <ul class="list- ...

position the tooltip within the ample available space of a container in an angular environment

In my editor, users can create a banner and freely drag elements within it. Each element has a tooltip that should appear on hover, positioned on the side of the element with the most space (top, left, bottom, right). The tooltip should never extend outsid ...

The error occurs when Facebook and Twitter iframes are attempting to access and set 'document.domain'

When attempting to add Tweet and Facebook Like buttons to the project I'm working on, everything appears to be functioning properly. However, upon clicking the buttons, a JavaScript error occurs: Unsafe JavaScript attempt to access frame with URL htt ...