Maintain the aspect ratio of an image within a flex container when the height is adjusted

I'm currently facing an issue with an image in a flex container. My goal is to maintain the image's ratio when the height of the container or window is decreased or resized.

Check out this sample fiddle

html,
body {
  height: 100%;
}

.wrapper {
  display: flex;
  width: 100%;
  height: 60%;
  border: 1px solid red;
}

.image-container {
  display: flex;
  align-items: center;
  max-width: 30%;
  height: 100%;
  background: green;
}

img {
  max-width: 100%;
  max-height: 100%;
}

.content {
  display: flex;
  justify-content: center;
  align-items: center;
  flex: 1;
  width: 100%;
  height: 100%;
  background: yellow;
}
<div class="wrapper">
  <div class="image-container">
    <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e0/Clouds_over_the_Atlantic_Ocean.jpg/1200px-Clouds_over_the_Atlantic_Ocean.jpg" alt="">
  </div>
  <div class="content">
    <span>Some content</span>
  </div>
</div>

Currently, only the image's height changes while the width remains constant, causing the image to be stretched. Removing 'display: flex' from the image container solves the resizing issue, but I require it for centering purposes.

Is there a way to resize the image and other containers fluidly while maintaining the aspect ratio?

Answer №1

To achieve a cover effect on images, simply insert the following CSS code snippet within the img tag:

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

JSFiddle

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

When a label is clicked, the ng-change event is not triggered on the corresponding radio button

I developed a custom directive that allows you to select a radio button by focusing on its label and pressing the spacebar. However, although the radio button is checked, the ng-change function action() does not get triggered. Below is the HTML code snipp ...

Client-side resizing an image before sending it to PHP for uploading

Greetings! Currently, I am utilizing a JavaScript library from this source to resize images on the client-side. The image resizing process works successfully with the following code: document.getElementById('foto_select').onchange = function( ...

Which Angular Lifecycle event should I utilize to trigger a specific action when either two buttons are pressed or when one of the buttons undergoes a change?

In this scenario, we have a total of 6 buttons split into two groups of 3: one on the top and the other on the bottom. let valuesum; let value1; let value2; let ButtonGroup1clicked= false; let buttonGroup2Clicked= false; function click1 (value){ va ...

The $().bind function will not function properly unless the document is fully loaded

Do I need to include $(document).ready() with $().bind? Here is the HTML portion: <head> <script type="text/javascript" src="jquery-1.10.2.min.js"></script> <script type="text/javascript" src=&quo ...

Utilize Meteor-tabular to effortlessly export data with convenient HTML5 export buttons

Currently, I am utilizing the tabular package to display data from a collection in a table format. However, when attempting to export the data using html5 export buttons with the button (csvhtml5), only the length of data visible in the table is exported, ...

Using the onreadystatechange method is the preferred way to activate a XMLHttpRequest, as I am unable to trigger it using other methods

I have a table in my HTML that contains names, and I want to implement a feature where clicking on a name will trigger an 'Alert' popup with additional details about the person. To achieve this, I am planning to use XMLHttpRequest to send the nam ...

Centered iframe within a div

I am trying to center my iframe within a white box and need some assistance with this. I want the iframe to be in the middle of the white box rather than its current position at the moment. game1.html: <html> <head> <title>R ...

Customize each carousel item by adding a unique background image to enhance the visual appeal

I'm looking to customize my Bootstrap carousel by adding unique background images to each item. Here's a snippet of my HTML: <!-- Start of carousel --> <div id="mainCarousel" class="carousel slide carousel-fade" d ...

What is the best way to verify HTML using RSS?

Looking to improve my skills in HTML/CSS/PHP development, I'm faced with a challenge when it comes to validating code that contains content beyond my control, such as an RSS feed. For instance, on my home page, which is a .php document containing bot ...

What is the best way to transfer a PHP string to JavaScript/JQuery for use in a function?

Within my PHP code, I have the following: $welcome = "Welcome!"; echo '<script type="text/javascript">addName();</script>'; Additionally, in my HTML/script portion: <a id="franBTN"></a> <script type="text/javascript ...

A table featuring two tbody sections positioned side by side within the table structure, with the thead located at the top

My goal is to design an HTML table layout resembling the following: Left part | Right part | Remove row? ------------------------------------------- 1 [input] [input] Y/N 2 [input] [input] Y/N 3 [input] [inpu ...

Looking to showcase this information using an HTML Ajax jquery call

{"response":{"refno":"hfc","status":"Status : Reference number does not exist."}} After sending a request to a specific webpage, I received a response in JSON format. I am looking for a way to display this response in HTML, either in a table format or a s ...

Resize a responsive grid of images

Within my slider, I have three slides with 3 images each. The layout consists of one large image on the left and two smaller ones on the right. To ensure responsiveness, I've used max-width: 100% on the images so they can scale down proportionally as ...

A Guide to Encasing Flatlist Elements in React Native

I am attempting to display a list of items similar to the layout shown in this image. https://i.sstatic.net/5Uszy.png The content of each row will vary depending on the text size. Flatlist is being used to render the items. TagView.js <View style={st ...

Increased spacing HTML

Trying to create a footer in HTML, but each time I attempt it ends up with an extra margin on the left side. Here is my current footer code: <div id="footer"> Tester </div> #footer { background-image: url(images/backgroundrpatternf ...

Utilizing a Recycled jQuery UI Themeroller Overlay

I'm looking to optimize css-loading bandwidth by reusing the overlay from jQuery UI for a specific section of my page. However, I'm encountering an issue where the overlay only covers a semi-transparent blackout of the entire page instead of just ...

"Unlock the secrets to commanding respect in the web form layout by mastering the art

I have a fieldset that includes a legend with potentially long text content. I need the legend to only take up 50% of the width of the fieldset. Currently, when the legend is too lengthy, it still occupies 50% of the space but causes the entire fieldset ...

MUI Typography - Text turns white when hovered over

I've created a customized component that turns green on hover. Here's the code for it: const ProjectAdd= styled.div` background-color: white; list-style-type: none; list-style: none; height: 100%; width: 10%; height: 100%; border-ra ...

Accessing the data from an HTML5 slider using JQuery

Having some difficulty fetching a value from an HTML5 slider using JQuery. Here is my code snippet: JQuery Code: // Getting the value from slider one $("#submit").on("click", function(evt) { var sliderValue = $('#slider01').attr('value ...

Issue with Anchor class in CakePHP where the variable $action is not defined

I'm struggling to apply a class to my anchor link when it is active. How should I define the $action variable in this case? Version: 4.2.9 Error: Undefined variable: action [ROOT\templates\layout\default.php, line 108] templates/lay ...