Is it possible to obtain an image that is responsive while maintaining a specific height?

Currently, I am working on a page builder to create a shop. It's great that I have the ability to change the CSS on this project.

However, I am facing a challenge with the responsive resizing of images in a 4 column row. Since the images have different heights, I need to set a height and ensure the width is responsive. Is there a way to make it scale correctly?

The width is set to auto, while the height is determined by the size of the screen.

As I resize the images, I notice that they sometimes separate from the box and end up getting squished in certain cases.

Answer №1

Utilizing the object-fit Property for Responsive Design

To achieve a responsive design, I incorporated the use of display: flex; and object-fit: cover; properties. I believe that the object-fit property applied directly to the image is crucial in ensuring that your images maintain their quality across various screen resolutions.

It is worth noting the utilization of object-position: center;, which ensures that the resizing focuses on the center of the image at all times.


index.html

<div class="foo">
    <div class="bar">
        <img src="https://picsum.photos/200/300" alt="">
        <div>
            <h4>Lorem ipsum dolor sit amet consectetur adipisicing.</h1>
            <p>$42</p>
        </div>
    </div>

    <div class="bar">
        <img src="https://picsum.photos/500/200" alt="">
        <div>
            <h4>Lorem ipsum dolor sit amet consectetur adipisicing.</h1>
            <p>$42</p>
        </div>
    </div>
    
    <div class="bar">
        <img src="https://picsum.photos/200/700" alt="">
        <div>
            <h4>Lorem ipsum dolor sit amet consectetur adipisicing.</h1>
            <p>$42</p>
        </div>
    </div>

    <div class="bar">
        <img src="https://picsum.photos/500/400" alt="">
        <div>
            <h4>Lorem ipsum dolor sit amet consectetur adipisicing.</h1>
            <p>$42</p>
        </div>
    </div>
</div>

style.css

body {
    background-color: #f7f7f7;
    font-family: Arial, Helvetica, sans-serif;
}

.foo {
    width: 100%;
    display: flex;
    gap: 20px;
}

.bar {
    background-color: #ffffff;
    display: flex;
    flex-direction: column;
    width: 100%;
    border-radius: 15px;
    overflow: hidden;
    box-shadow: rgba(100, 100, 111, 0.2) 0px 7px 29px 0px;
}

.bar > img {
    width: 100%;
    height: 200px;
    object-fit: cover;
    object-position: center;
}

h4 {
    color:#9ccf74;
}

.bar > div {
    padding: 10px;
    height: 100px;
}

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

A visual element positioned centrally within a row, directly beneath a paragraph tag

Every solution I attempt fails to work correctly, at times I achieve centering the image but then the figcaption ends up crammed into a small corner. <!DOCTYPE html> <html lang="en> <head> <meta charset="UTF-8> <link r ...

Sliding panels with Jquery

I've created some basic panels that slide in and out horizontally. My goal is to hide all content except the first panel when the page loads. When a link to other panels is clicked, the current content will slide left to be hidden and new content will ...

The error detected in the W3Schools validator pertains to CSS for audio elements that do not contain

Could somebody kindly explain why this code is being flagged as an error on w3schools? audio:not([controls]) { display: none; height: 0; } ...

Issue with dynamically adjusting flex box width using JavaScript

Currently, I am developing a user interface that heavily relies on flexbox. The layout consists of a content area and a sidebar that can be toggled by adding or removing a specific class. Whenever the sidebar is toggled, the content area needs to be manua ...

Troubleshooting Next.js and Vercel: Local dynamic images display correctly, but not in the live production environment

When running my next.js-app on Vercel, I encounter an issue with dynamic images. While everything works smoothly on localhost, only the fallback image is displayed on Vercel, resulting in a 404 error for the default image. Interestingly, manually specifyi ...

What are some ways to personalize the depth graph in amcharts?

I am having trouble modifying the depth graph amchart and I'm struggling to grasp how it functions and how to design it like the image below. Below is the link to the original graph that I am trying to customize: Link https://i.stack.imgur.com/nbWd ...

"Challenges with Full-Width Dropdowns in Multi-level Mega Menus

I am currently attempting to design a multi-level mega menu that spans the full width of the screen, while keeping the content within it restricted to a maximum width of 1240px. I have managed to set the content to the maximum width, but I am facing challe ...

Unable to render image despite being located in the identical directory

I'm currently working on an Angular component and facing some issues with displaying an image. I've tried hardcoding the img src for now, but eventually, I want to implement it dynamically. Both my component and the image files are located in th ...

fluid columns gliding from side to side

I'm currently working with Bootstrap to design responsive columns. My goal is to have the next column of content slide in from either the left or right side when the user clicks on the respective arrow button. While I have found solutions for fixed w ...

How can I display a button (hyperlink) on a new line using CSS or jQuery?

I am looking to create a new line after the last input of my form. On this new line, I want to add a "remove-link". This is my HTML: <div class="subform dynamic-form"> <label for="id_delivery_set-0-price"> Price: </label> <inp ...

I attempted to initiate a transition using JavaScript, but unfortunately it failed to execute

Hey there! I'm just starting out with HTML, CSS, and JavaScript, and I've been trying to make a transition work using JavaScript. The element I'm working with is a "menu-icon" which is a span tag with a small image nested inside another div ...

Diverging Width Values Between Chrome and Firefox with Jquery's .width() Method

Currently, I am implementing this JQuery script for my table. $(document).ready(function () { var tableBottom = $(window).height() - $('#compare-table').height(); $(window).scroll(function (event) { var y = $(this).scrollTo ...

The CSS within the WebComponent nesting is not valid

Upon testing, I have found that writing CSS directly using nesting is effective. However, when used within a WebComponent and nested with the :host selector, it becomes invalid. Below is a demo code snippet showcasing this issue. My Chrome version number i ...

Position the Bootstrap button at the bottom left with absolute placement

Currently, I am utilizing Django as my web framework and Bootstrap to enhance the rendering of the frontend. My goal is to implement an "add" button on my page that will always be in the bottom left corner regardless of scrolling, overlaying any other cont ...

Guide on moving to the following page of a website with the help of CSS selectors

I'm currently in the process of retrieving property details from a specific website (Find Properties For Sale). Initially, I successfully scraped the information from the initial page. However, upon attempting to gather data from subsequent pages, my ...

Can JSS support creating variables similar to Sass?

Currently, I am developing a project in React and utilizing Material-ui with JSS for styling. In Sass, we have the ability to define variables like $color: rgb(255, 255, 255) for later use. I am curious if it is possible to implement variable usage simi ...

Ways to prevent animations from displaying solely in IE

Is there a way to remove a keyframe animation if the browser is IE, while keeping it for Chrome, Safari, and FireFox? The animation I am referring to is defined as follows: // Animations @-webkit-keyframes fadeIn { from { opacity:0; } to { opacity:1; } } ...

Learn how to easily toggle table column text visibility with a simple click

I have a working Angular 9 application where I've implemented a custom table to showcase the data. Upon clicking on a column, it triggers a custom modal dialog. The unique feature of my setup is that multiple dialog modals can be opened simultaneously ...

What is the best way to incorporate an icon into my HTML search bar?

I'm having trouble getting the search icon from font awesome to display properly within my search bar. Here is the code I'm using: <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.2/css/all.min.css" rel="stylesheet"/> ...

How can I position my navbar above my background image using Bootstrap?

I am a newcomer to using bootstrap and I am attempting to create the following design: https://i.sstatic.net/EV9A1.png In the design, the navbar is situated on top of the background image. However, with my current setup, this is what I have: <!DOCTYPE ...