Image that adjusts its size according to the device screen width while

My goal is to create responsive images with a fixed height.

Below is the HTML code I am using:

<div class="col-md-6">
   <div class="img">
       <img src="http://image.noelshack.com/fichiers/2016/16/1461065658-b-v-s.jpg">
   </div>
</div>
<div class="col-md-6">
   <div class="img">
       <img src="http://image.noelshack.com/fichiers/2016/16/1461065665-kfp3.jpg">
   </div>
</div>

I have also included my CSS:

.col-md-6 {
   width: 50%;
   float: left;
 }

.img {
   overflow: hidden;
   position: relative;
   height: 290px;
}

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

You can view the implementation on JSFiddle here: https://jsfiddle.net/23o81xrb/

The issue I'm facing is that even though the .img class has a height of 290px, the images are not adapting when the window size is reduced. I would like the images to maintain their 290px height and adjust in width like a "zoom" effect.

Apologies for any language errors, thank you for your understanding.

I would greatly appreciate any assistance provided. Thank you.

Answer №1

If you're looking to style your images using the img tag, you can achieve this by utilizing the object-fit property in CSS3. See an example below:

.col-md-6 {
  width: 50%;
  float: left;
}

.img {
  overflow: hidden;
  position: relative;
  height: 290px;
}

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

This solution should effectively address your issue.

Answer №2

If you're seeking to achieve a zoom effect with a fixed height in img tags, the task may prove to be challenging!

However, by utilizing your images as backgrounds, it becomes possible to fixate on the height and find a suitable solution that meets your requirements. Here's an illustration:

.col-md-6 {
  width: 50%;
  float: left;
}
.img {
  overflow: hidden;
  position: relative;
  height: 290px;
}
.img.first {
  background-image: url('http://image.noelshack.com/fichiers/2016/16/1461065658-b-v-s.jpg');
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
}
.img.second {
  background-image: url('http://image.noelshack.com/fichiers/2016/16/1461065665-kfp3.jpg');
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
}
.img img {
  max-width: 100%;
  min-width;
  100%;
  min-height: 100%;
}
<div class="col-md-6">
  <div class="img first">

  </div>
</div>
<div class="col-md-6">
  <div class="img second">
  </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

The Div element containing the rollover button/picture is failing to resize properly as the window is decreased in size

Whenever I try to resize my browser window in Firefox or Chrome, the div ends up moving instead of resizing. I've searched on stackoverflow for answers but haven't found any solutions yet. Can someone please help me with this issue? Here is what ...

Revolutionize iPad user experience with seamless HTML5 video integration

What is the most effective method for dynamically embedding HTML5 video to ensure compatibility with the iPad? (using pure Javascript) I'm having difficulty getting this approach to work: <div id="placeholder"><script type="text/javascript" ...

The animation fails to function, both when hovering over and when not

ul { display: flex; flex-direction: row; align-items: center; list-style-type: none; background-color: $base-gray-t; margin: 1em; padding: .25em 0 0 0; height: 3em; border-bottom: 0.375em solid $secondary-color; overflow: hidden; ...

Eclipse now features automatic CSS class suggestions to help streamline your coding

After using NetBeans for a while, I am now considering making the switch to Eclipse. Everything seems fine, but one thing that is bothering me is that Eclipse does not provide CSS class proposals like NetBeans does. In NetBeans, whenever I use or create a ...

What is the best way to navigate users to a different page when they click on a button?

I recently designed a button in Dreamweaver software. Could you please guide me on how to set up the button so that when clicked, it redirects the user to a different page using Dreamweaver? Below is the HTML code for the button: <input type="submit" ...

How can I navigate and click on a drop-down menu link in Python using Selenium and CSS selectors?

This is an example of the HTML code: <span class="MenuIcons searchButton"></span> ... (additional content) <a data-bind="" url="/ParagonLS/Search/Property.mvc/Index/1" tabdescription="RESIDENTIAL" subtabdescription="Criteria" subtab ...

Using the on-change event with a textfield can help ensure that the field is cleared if the min-date condition is not met

I recently encountered an issue with an input field that had the type "datetime" and a min-date condition set to today's date. The flow was such that if a valid date was entered, the submit button would be enabled, otherwise it would remain disabled. ...

Adjust the content to expand and occupy the entire height using Material UI cut-off Drawer

I am using the provided Material UI code for a persistent clipped drawer, but I am encountering an issue with the content height. I have set the background of the content section to red in my demo sandbox to showcase the problem. My goal is for the content ...

What other function can I combine with the foreach function?

I am working on populating the empty array named $Errors with specific items to enforce restrictions on my file upload form. My approach involves adding a <div> element as an item within the array, containing certain strings. I aim to concatenate t ...

JavaScript animation for sequencing traffic lights

I have been working on completing a task that was assigned to me. (iii) Explain the organization of an array that could manage the traffic light sequence. (iv) Develop a script that utilizes the array outlined in part (iii) to create an animated display ...

Get all text that comes after a closing tag up to a designated tag using JSOUP

I have this snippet embedded within a plethora of table rows in a table: ......... <tr class="greycellodd" align="right"> <td align="left"> <input type="checkbox" name="cashInvestment" value="100468057"/> </td> <td align="left"& ...

Tips on positioning content beneath a fixed header or navigation bar when viewed in a web browser

Hi, I'm having an issue with creating a fixed header using HTML and CSS. When I set my header to be in a fixed position, it covers up the content below it. I want the content to be positioned under the header when the page is loaded. Additionally, I&a ...

The scrollHeight property for a textarea element that is set as

Currently, I am working on enhancing a form results page by implementing a readonly textarea to showcase the submitted email address. My main goal is to allow the textarea to dynamically adjust its height in order to display the full email instead of trunc ...

Change the information displayed on buttons when clicked and provide options for users to navigate between different

In order to change the content when a button or number is clicked, you can utilize the buttons "1,2,3,4" or the Next and Prev buttons shown in the snippet. Both methods allow access to the same article. If you want to switch between articles using the Next ...

Styling with CSS: Creating a scrollable gallery of images with a hover effect

I'm struggling to align multiple images horizontally on the page with a hover effect for each image. I can achieve one or the other separately, but not both together. As a beginner in HTML/CSS, I know it's a simple process. Below is my code with ...

Toggle the sidebars to slide out and expand the content division using JavaScript

I am looking to achieve a smooth sliding out effect for my sidebars while expanding the width of the middle content div. I want this action to be toggled back to its original state with another click. Here's what I've attempted so far: $(' ...

Struggling to get custom CSS to apply properly in Joomla 4 with Bootstrap 5 template

Having created a simple Bootstrap 5 template for my Joomla 4 website, I've been attempting to customize the navbar with CSS in my user.css file. However, it seems that the styles added in user.css are not being applied. I have verified that user.css ...

Tips on revealing the following div using jQuery

Can anyone help me figure out how to reveal the next div with a class of .hide when a Clickbox is clicked? Here is a link to the demo: http://jsfiddle.net/fvuqW/ This is the jQuery code I currently have: $(function(){ $('.form input[type=checkb ...

Having trouble with the functionality of Bootstrap 5 carousel?

I've been attempting to integrate this carousel into my Bootstrap 5 webpage. I found it on Codepen - https://codepen.io/sahil-verma/pen/wxXryx Thus far, I've copied the code into my webpage and linked the corresponding CSS within style tags, sim ...

Use jQuery to insert the TEXT heading as an input value

Can anyone help me with copying text from a heading to an input value? Here's an example: <h2 class="customTitleHead">This is a Heading</h2> I want to transfer the text from the heading into an input field like this: <input type="tex ...