Adjusting the size of an image within a div container alongside additional elements

How can I ensure that an image and variable text inside a div do not exceed the height of the parent div?The image should resize based on the amount of text present.

In the code snippet below, the image occupies the full height of the div (30vh) with text below. I want the image to take up maximum space possible, resizing only enough to accommodate the text within the parent div.

#test {
  height: 30vh;
  width: 50%;
}

img {
  max-height: 100%;
  object-fit: cover;
}
<div id="test">
  <img src="..."><br />
  <div>Variable amount of text</div>
</div>

If there are any Bootstrap 4 functions available to simplify this, I would like to utilize them.

Answer №1

To transform the container with the class .test into a flexbox column layout, you should replace the image with a div that has the image set as its background and utilize the CSS property flex: 1.

.test {
  display: flex;
  flex-direction: column;
  height: 30vh;
  width: 50%;
}

.img {
  flex: 1;
  background: url(https://picsum.photos/id/464/200/300) no-repeat;
  background-size: contain;
}
<div class="test">
  <div class="img"></div>
  <div>Variable amount of text</div>
</div>

<div class="test">
  <div class="img"></div>
  <div>Variable amount of text Variable amount of text Variable amount of text Variable amount of text</div>
</div>

Answer №2

test it out using this method

div#example img {
    max-height: 100%;
    object-fit: cover;
}

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

How to reference a ListView control using its DataSource in a C# class

Currently, I have a ListView embedded in a webpage connected to a C# class datasource called CommentsDAO. This class contains the necessary methods to retrieve or delete data from the ListView. While data retrieval is successful, deleting a row proves to b ...

Creating a Django URL path using an HTML search form

Within my template, I have implemented an input field with the type "search". Upon triggering an action, the page redirects to "/search_results.html". The current dilemma I am facing is that it is adding /?search=yoursearch to the end of the URL. This is ...

What is causing the top and bottom margins to be invisible in my browser?

Why can't I see the top and bottom margins in Safari, Chrome, or Firefox when I've set the margin to 50px? HTML: <div style="width:400px;background-color:#ffffaa"> <p style="background-color:#aaffff; padding:20px; ...

Angular package that includes horizontal scrolling functionality with arrow buttons

Currently seeking an Angular 2-6 package featuring a horizontal image scroll with arrows, similar to the functionality on Airbnb: https://i.sstatic.net/BOWzD.jpg Any suggestions or recommendations are greatly appreciated – thank you! ...

Arrangement of box and buttons on R shiny interface

My dashboard page features action buttons aligned to the left, along with a plot and two additional zoom and reset buttons. I am looking to center the box on the screen and position the zoom and reset buttons at the top right corner. I attempted to use t ...

The dropdown menu is erroneously showing buttons instead of the intended options

My dropdown function is causing a small issue. The desired behavior is that if the selected value is "", labeled "Please Select" in the dropdown menu (I've added a comment in the function to pinpoint the problem), then buttons A, B, and C should not b ...

Looking to showcase an uploaded image next to the upload button in Vue.js using Element.ui?

I am currently working on implementing a feature that allows users to preview an image after uploading it. My approach involves uploading the image and then making an axios/AJAX call. Please see the following code snippet for reference: HTML code: ...

Are there any reliable sources for a complete list of browser CSS properties?

Is there a way to easily access a comprehensive list of CSS properties supported by specific browsers, particularly IE8? Any suggestions on where I can find this information? ...

Move and place multimedia items using IE's drag and drop feature

Is there a way to enable the drag and drop feature across different browsers or windows using HTML5's native DnD API? I have noticed that when I set the data type to 'text' in Internet Explorer, it functions properly. However, if I attempt t ...

Discover the color value within an array that begins with the "#" symbol

In my PHP code, I have written a function that extracts values from a CSS file and creates an array. Now, I need to loop through this array and create another array that only contains color values (strings starting with #). The challenge is that the length ...

Angular single page application with a sleek, user-friendly navigation bar for easy browsing

I have been attempting to solve the issue at hand without much success. As a newcomer to web development, I have been working on creating a basic app using angularjs and bootstrap. My pages are fairly sparse, consisting only of a few inputs and buttons t ...

What is causing the failure of these "span" elements within an "a" tag in IE7?

Here is a code snippet that I am working with: HTML (version 4.0) <div class="temperatura"> <a href="/link/" class="temperatura_localita"> <span style="padding-left:12px;"> T ...

What is the most effective way to programmatically select checkboxes based on array values in

Trying to keep it concise :) Working on a project that generates multiple pages of thumbnail images with checkboxes. The total thumbnails vary and are sorted into 1000 item html pages, called by a parent html page via iframe. Goal is for users to check che ...

Forward the jsp to the servlet before navigating to the following page

Issue: After submitting the JSP form on Page1, it redirects to a server-side JSP page but appears as a blank page in the browser. Instead, I want it to redirect to Page2 which includes a list box that highlights the newly created item. Seeking help with t ...

Having trouble with Floating Labels in Bootstrap 5

Having trouble with my floating labels - any advice? https://i.sstatic.net/fk0CG.png Here is the code snippet: <div class="form-floating mb-3"> <input type="text" class="form-control" autofocus id="txtNome" placeholder=" "> <la ...

Is there a way to align text in the middle while having different icons on each side?

Is there a way to center align the text in this image with minimal use of CSS/HTML? The icons on the left are causing it to be off center: https://i.sstatic.net/TKOYG.png Below is the relevant HTML and CSS for this top section: <div class="navbarhead ...

What's the reason this picture isn't displaying in its full size?

I am having trouble adjusting the size of this image to fit the full width, but there seems to be some remaining space on the right side. Category: CSS/HTML /* Header */ .header { background: url('https://picsum.photos/1920/1080') center ...

Interacting between client and server with the help of JavaScript and websockets

As someone new to Javascript, I am eager to learn about the steps and initial codes needed to establish a connection between the client side and the server side using JS and websockets. ...

Implementing a parallax scroll effect using CSS and dynamically updating the background-position value with JavaScript

How can different Y position percentages be dynamically assigned to multiple layered background images in CSS using JavaScript so that they update as the page scrolls? <style> body { background-image: url(img/bg-pattern-01.png), ur ...

Tips for changing an input value when clicked using JQuery

Struggling to create an interactive mind mapping tool using JQuery? One challenge I'm facing is editing the content of a div once it's been set. I've implemented a function that successfully adds text to a div within the (mind-container). H ...