Using `object-fit: cover` on an image will cause the parent element's height to be affected

I am currently working on a component that will have variable content, and I want the image to respond like a background image by taking the height of its container instead of defining the height itself. Even when using object-fit: cover, the image still affects the height of its parent element. I would prefer the text to determine the height of the parent rather than the image. Unfortunately, I cannot use a background image because the image is populated via a wysiwyg editor and I do not have control over where the uploaded image is loaded (it must be an actual image). Thank you for any assistance you can provide.

Codesandbox

CODE:

.container {
    display: flex;
    flex-direction: row;
    padding: 20px;
    background: lightblue;
    width: 80%;
}

.img-item {
    align-self: stretch;
    flex-shrink: 2;
}

.img-item img {
    object-fit: cover;
    height: 100%;
    width: 100%;
}

.text-item {
    flex-shrink: 3;
    padding: 20px;
}
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <meta http-equiv="X-UA-Compatible" content="ie-edge" />
        <title>Static Template</title>
        <link rel="stylesheet" href="style.css" />
    </head>
    <body>
        <div class="container>
            <div class="img-item>
                <img src="https://i.imgur.com/2bvab7y.jpg" alt="" />
            </div>
            <div class="text-item>
                <h1>
                    Heading one
                </h1>
                <p>Hello world. You're the best.</p>
            </div>
        </div>
    </body>
</html>

Answer №1

you need to include the flex-basis CSS property

.container {
  display: flex;
  flex-direction: row;
  padding: 20px;
  background: lightblue;
  width: 80%;
}
.img-item {
  align-self: stretch;
  flex-shrink: 2;
}
.img-item img {
  object-fit: cover;
  height: 100%;
  width: 100%;
}
.text-item {
  flex-shrink: 3;
  padding: 20px;
}
[class$=item]{flex-basis: 120px;}
<div class="container">
  <div class="img-item">
    <img src="https://i.imgur.com/2bvab7y.jpg" alt="" />
  </div>
  <div class="text-item">
    <h1>
      Heading one
    </h1>
    <p>Hello world. You're the best.</p>
  </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

Utilize API or alternative methods for analyzing Instagram data

My master's thesis requires me to dissect multiple Instagram profiles, each containing over 1000 posts. I am in need of a comprehensive list including the following details: Type of Post (Image, Multi Image, Video) Description Likes Comments (total c ...

One way to integrate social sharing buttons into your Django blog

I am currently using the django_social_share module and I am struggling to understand how to send the sharing URL for a specific blog post on social media. Here is my post_detail.html: <article> <div class="embed-responsive embed-responsive-1 ...

What is the best way to extract both the link and text from an <a> tag that contains another element in XPath?

In a similar way to my current data (which cannot be shared due to company policy), the example below is based on information extracted from this response and this response. The aim is to extract both the text of the <a> element and the link itself. ...

The integration of jQuery in PHP is experiencing some technical challenges

There's one issue driving me crazy: I created a left-floating file tree view and included it in my website. However, when I include my website in the file treeview, it becomes unclickable in Chrome, and in IE 9, the thumbnail part for viewing PDF docu ...

Activate the sliding toggle feature on click for all div elements

Whenever a specific li element is clicked, I aim to open its corresponding div element. This is the code snippet I currently have: <ul class="no-padding pro-list"> <li><a href="#" class="pro-1 pro">A</a> <div class= ...

Unable to load YouTube video player

Having trouble getting the Youtube embed code to work, any suggestions on fixing it? Check out this link for potential solutions ...

Is there a way to stop the page from scrolling once I reach the bottom of a specific div within it?

My webpage has multiple divs that share a similar structure: <div style="height:200px; overflow:auto;"> <div style="height:300px;"> <!--There is a lot of content here--> </div> </div> When the user hovers ove ...

Tips for addressing flickering issues when scrolling on your device

I am facing an issue with two elements that are set to a fixed position on the page. When these elements reach the bottom of the page, I want them to revert back to a static position using JavaScript. The problem occurs when trying to scroll by clicking a ...

Developing a basic PHP calculator from scratch

I've put together a basic form that I want to use as a web calculator, where the total will be the sum of the values entered in the columns based on this example. How can I set it up so that I can actually perform calculations with the form? HTML: & ...

Is there a way to show an element on a website only when the height of the HTML content exceeds the height of the viewport?

My webpage serves as a dynamic to-do list, allowing users to add an endless number of tasks of all types. As the list grows in size, the page height increases and a scrollbar appears. Positioned at the bottom right corner of the page is a fixed button that ...

What is the reason for font names appearing yellow rather than blue in the "font-family" property?

Whenever I experiment with different font names, I notice that some fonts turn blue while others remain yellow. Can anyone clarify why this is happening? font-family: Arial, Helvetica, sans-serif; font-size:12px; font-weight:normal; ...

Angular TS class with an ever-evolving and adaptable style

Currently, I am working with angular 9. Does anyone know a way to dynamically change the CSS of a class in a component? .stick-menu{ transform: translate(10px,20px); } I am looking to dynamically adjust the position of x and y values. For example: .stic ...

A method for setting values independently of [(ngModel)] within an *ngFor loop and selecting an HTML tag

I am encountering an issue with [(ngModel)], as it is syncing all my selections to the same variable. My variable is selectStations = []; Therefore, when I choose an option in one select element, it updates all select elements to have the same selected o ...

What is the best way to incorporate CSS animations into JavaScript for mobile devices?

I need a way to copy text to the clipboard and show an alert confirming that it was copied successfully. When the user clicks/touches the coupon PRIMEIRACOMPRA, the text "cupom copiado" changes its opacity to 1 and runs an animation to hide it. See Examp ...

Changing the Size of React Bootstrap Cards to Fit Your Grid Layout with Custom CSS

I am facing an issue with the varying sizes of react-bootstrap cards within a grid display. Check out the problem I am encountering: https://i.sstatic.net/szHjI.png I need the size of Over Under 0.5 card to be different from the one for Match Winner, as ...

The command entered is not valid for 'text/css'. It is possible that there was a typo or the command is from a module that is not configured in

We are currently using Laravel 5.4 on a Linux server with Apache. Recently, we started encountering the following error in our /var/log/httpd/error_log file even though there have been no changes made to our .htaccess file: /var/www/html/public/.htaccess: ...

Tips for creating consistent spacing between elements using Tailwind CSS version 3

I am currently utilizing Tailwind CSS in conjunction with next.js for my project. I have a total of 8 images, each varying in size, and my goal is to display 4 images per row on medium and large devices, while displaying 2 images per row on small devices. ...

h1 class not adhering to CSS styles

I have implemented the toolbox theme on my WordPress website. Below is the code snippet I am using to display the title of each post on the blog page within an h1 tag: <h1 class="entry-title"><a href="<?php the_permalink(); ?>" title="<? ...

Disable the time selection feature in the text input field

Despite seeing the same question asked before for this issue, I am looking for a solution that does not involve replacing the textbox with the same ID. When Timepicker is selected in the dropdown menu timepicker, it should activate in the textbox (which i ...

Using jQuery to compare input with the current select optgroup choices

I'm struggling with looping through options within a form select. The options are grouped into optgroups, and I believe this is where my issue lies as I can't seem to find the correct syntax. My objective is to compare the value entered using .v ...