Rebuild image layout with HTML and CSS

I am struggling to create a fixed image grid using HTML and CSS, but the size of the image grid is based on the size of the images. I don't have much experience with front-end development. I have included an image of how it currently looks and how I want it to look. Any help in getting this to work would be greatly appreciated. Thank you.

This is how it currently looks

It should look like this

** HTML**

      <div class="row">
            <div class="col-md-3 col-sm-6">
                <div class="product-grid">
                    <div class="product-image">
                        <a href="#">
                            <img class="pic-1" src="http://bestjquery.com/tutorial/product-grid/demo9/images/img-1.jpg">
                            <img class="pic-2" src="http://bestjquery.com/tutorial/product-grid/demo9/images/img-2.jpg">
                        </a>
                        <ul class="social">
                            <li><a href="" data-tip="Quick View"><i class="fa fa-search"></i></a></li>
                            <li><a href="" data-tip="Add to Wishlist"><i class="fa fa-shopping-bag"></i></a></li>
                            <li><a href="" data-tip="Add to Cart"><i class="fa fa-shopping-cart"></i></a></li>
                        </ul>
                        <span class="product-new-label">Sale</span>
                        <span class="product-discount-label">20%</span>
                    </div>
                    <ul class="rating">
                        <li class="fa fa-star"></li>
                        <li class="fa fa-star"></li>
                        <li class="fa fa-star"></li>
                        <li class="fa fa-star"></li>
                        <li class="fa fa-star disable"></li>
                    </ul>
                    <div class="product-content">
                        <h3 class="title"><a href="#">Women's Blouse</a></h3>
                        <div class="price">$16.00
                            <span>$20.00</span>
                        </div>
                        <a class="add-to-cart" href="">+ Add To Cart</a>
                    </div>
                </div>
            </div>
        </div>

CSS (for the image part)

.product-grid {
  font-family: Raleway,sans-serif;
  text-align: center;
  padding: 0 0 72px;
  border: 1px solid rgba(0, 0, 0, 0.1);
  overflow: hidden;
  position: relative;
  z-index: 1;
}
.product-grid .product-image {
  position: relative;
  -webkit-transition: all .3s ease 0s;
  transition: all .3s ease 0s;
}
.product-grid .product-image a {
  display: block;
}
.product-grid .product-image img {
  width: 100%;
  height: auto;
}
.product-grid .pic-1 {
  opacity: 1;
  -webkit-transition: all .3s ease-out 0s;
  transition: all .3s ease-out 0s;
}
.product-grid:hover .pic-1 {
  opacity: 1;
}
.product-grid .pic-2 {
  opacity: 0;
  position: absolute;
  top: 0;
  left: 0;
  -webkit-transition: all .3s ease-out 0s;
  transition: all .3s ease-out 0s;
}
.product-grid:hover .pic-2 {
  opacity: 1;
}

Answer №1

I made some adjustments to your CSS code. There are two key techniques involved - one using object-fit: cover; and the other utilizing padding within the inner element.

.product-grid {
  font-family: Raleway,sans-serif;
  text-align: center;
  padding: 0 0 72px;
  border: 1px solid rgba(0, 0, 0, 0.1);
  overflow: hidden;
  position: relative;
  z-index: 1;
}
.product-grid .product-image {
  position: relative;
  -webkit-transition: all .3s ease 0s;
  transition: all .3s ease 0s;
  overflow: hidden; /* hiding parts of image */
}
/* This snippet allows for a flexible height, based on the parent's width */
.product-grid .product-image::before {
  content: '';
  display: block;
  position: relative;
  padding-top: 133%;
  width: 100%;
  height: auto;
}
.product-grid .product-image a {

}
/* The following code will treat the image like a background image */
.product-grid .product-image img {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
  object-fit: cover;
}
.product-grid .pic-1 {
  opacity: 1;
  -webkit-transition: all .3s ease-out 0s;
  transition: all .3s ease-out 0s;
}
.product-grid:hover .pic-1 {
  opacity: 1;
}
.product-grid .pic-2 {
  opacity: 0;
  -webkit-transition: all .3s ease-out 0s;
  transition: all .3s ease-out 0s;
}
.product-grid:hover .pic-2 {
  opacity: 1;
}

If you prefer not to have the image cropped, you can switch object-fit: cover; to object-fit: contain;.

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

Filter Products with Magento Layered Navigation - Checkbox Options

Is there a method to transform the layered navigation into checkboxes similar to Asos? I have noticed that there are modules available for purchase claiming to offer this functionality. I may consider using one of those, but I wanted to explore any other ...

The dimensions of the images in the Bootsrap carousel are fluctuating

Check out this carousel design with a login screen form here. The carousel in the link above is experiencing issues with changing sizes for each image. I've attempted to adjust the width and height of the carousel-inner, carousel, and img elements wi ...

The mobile navigation panel fails to open

I have a menu that I would like to toggle hide/show by adjusting its top position. Before logging in, the menu is structured as follows: <div class="lc"> <h1><a href="./"><img src="logo.png" /></a></h1> <a h ...

Tips for keeping several buttons in the spotlight: HTML/CSS/JS

Looking for a solution to keep multiple buttons toggled on? I'm not very familiar with JavaScript, but I know that adding a class on click won't work if there's already a class set. Maybe giving the element an ID could be a possible solution ...

Returning draggable elements to their original placement

I'm looking to create a custom function that resets my draggable elements to their original positions when invoked. This is not related to the built-in revert functionality. $('.drag').draggable({ stack: ".drag", snap: ".dro ...

What steps can be taken to avoid or halt focusing on the following input text element?

I came across a situation where I have to focus on the h1 element of an overlay instead of moving to the next tabbable element. The overlay appears after a service call triggered by blur event from the first input text field. Every time the blur event is ...

Retrieve data from HTML Form arrays using JavaScript

I have a situation in my forms where arrays are being sent back in the following format: <input class="checkbox-service" name="services['electricity']" type="checkbox"> <input class="checkbox-service" name="services['water'] ...

Parent element unable to trigger event due to interference from child elements

Within my HTML document, there is a parent <span> element that contains nested <em> elements. This <span> is styled with a specified width and display:block property using CSS. Despite a jQuery trigger event being successfully fired on t ...

Select a particular column (utilizing CSS multi-column functionality)

I want to style the second column in a text block with two columns using CSS or JavaScript. How can I achieve this without creating separate containers for each column? I'm aware that I could create two containers and target the second one for styling ...

Customize Angular Material styles uniquely across various components

Within my application, I am utilizing two components that contain tab groups. The first component serves as the main page, where I have adjusted the CSS to enlarge the labels by using ViewEncapsulation.None. The second component is a dialog, and I aim to m ...

Bursting at the seams: concealed issues

I am currently facing an issue with a facebook like button placed in the caption section of a slideshow div, located at the far bottom right corner. The problem arises when the 'post to wall' prompt pops up below the like button, but is cut off d ...

Tips on extracting code differences from code inspector

Utilizing the Chrome code inspector is extremely valuable, but I often find it challenging to track the modifications made to CSS and HTML live. This becomes particularly cumbersome when there are multiple tags being modified. Are there any Chromium exten ...

What is the best way to incorporate a jQuery script to make an engaging slideshow on my website?

I've been trying to develop a slideshow similar to the one showcased on this website. First of all: I am using Kompozer. My website is not yet live as I am still in the process of putting it together on my computer. To give you an idea, here is the d ...

Tips for properly formatting quotes in JSON to activate a link

I am working with a JSON configuration file that looks like this: "type": "script", "label": "coreapps.archivesRoom.printSelected", "script": "emr.fragmentActionLink(${project.parent.artifactId},\"downloadPatientHistory\", &bs ...

When the source is added with jQuery, the IMG element displays at 0x0 pixels

My latest project involved creating a custom modal popup named #light1. In my JavaScript code, I capture the source from the image the user clicks on and insert it into the img tag with the id of addable. Using jQuery: var images=$('#raam img') ...

Using the clip-path property to determine the content padding within a div

I am encountering an obstacle while attempting to insert icons into a polygon-shaped div. The problem lies with the padding, as the icons get cut off by the borders of the polygon. #container { width: 200px; height: 200px; border: 2px solid blac ...

Expanding all cards in a Bootstrap collapse when one is clicked

Hello, I am currently working on integrating bootstrap collapse into a section of my django application. Here is the code snippet that I have implemented so far: {% extends 'base.html' %} {% block content %} <div class="container"> & ...

Is there a way to retrieve all the default CSS properties of the input checkbox tag using JavaScript?

Despite attempting to use console.log with JavaScript, my efforts were unsuccessful. Here is an example: console.log(document.getElementsByTagName('input')[0].attributes[0]) ...

How can we use an SQL query to identify shared data that meets multiple conditions within a single table?

I am in possession of a list of country codes, each with its own account holder counts. My goal is to retrieve the account numbers along with the corresponding country codes for countries that are present in multiple locations. For example: https://i.sst ...

The magic of CSS Pseudo-classes in Inline Styling

For the longest time, I've been under the impression that you cannot include in-line :hover{..} styles to an element. However, recently I stumbled upon this page which showcased examples like this. <a href="http://www.w3.org/" s ...