Creating an Image Gallery with HTML and CSS that does not automatically resize images to make them square

How can I create a 2-column, 3-row image gallery where the height of the boxes automatically fills up the grid? I want the images to be square with equal width and height without specifying pixel sizes. Is there a stretch property or something similar that can achieve this? Here's the CSS and HTML code:

.grid-container {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  grid-gap: 0;
  padding: 0px;
}

img {
  width: 100%;
  height: auto;
  padding: 0px;
}
<div class="grid-container">
  <img src="https://www.woodlandtrust.org.uk/media/100078482/Sycamore01.jpg?cb=-11897985&amp;preset=gallery-tab-main-image">
  <img src="https://statesymbolsusa.org/sites/statesymbolsusa.org/files/styles/symbol_thumbnail__medium/public/primary-images/Applesfreshpicked.jpg?itok=YmYkBfY7">
  <img src="https://openbookphilly.com/wp-content/uploads/2016/11/bookstack.png">
  <img src="https://media.wired.com/photos/5b86fce8900cb57bbfd1e7ee/master/w_582,c_limit/Jaguar_I-PACE_S_Indus-Silver_065.jpg">
  <img src="https://atlantis.nyc3.digitaloceanspaces.com/styled/1bec9ec74aac91e70b3ef91fee1fc0f9">
  <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR3DXqVk9AhGSx2PIYoUepA1UfZFnGt_kY6iJTq3hb10ZLGhFwPQg">
</div>

Answer №1

To ensure that the box height is fully filled, it is important to utilize the "align-items" property set to "stretch" on the grid container.

.grid-container {
        display: grid;
        grid-template-columns: repeat(2, 1fr);.
        grid-template-columns: repeat(3, 1fr);
        grid-gap: 0;
        padding: 0px;
        align-items: stretch;
    }

Check out the demo code here

Answer №2

Here is a solution for automatically resizing images when the window is resized:

.grid-container {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    grid-gap: 0;
    padding: 0px;
    align-items: stretch; /* Default. Items are stretched to fit the container */
}

img {
   width: 100%;
   height:auto;
   padding: 0px;
}
<div class="grid-container">
   <img src="https://www.woodlandtrust.org.uk/media/100078482/Sycamore01.jpg?cb=-11897985&amp;preset=gallery-tab-main-image">
   <img src="https://statesymbolsusa.org/sites/statesymbolsusa.org/files/styles/symbol_thumbnail__medium/public/primary-images/Applesfreshpicked.jpg?itok=YmYkBfY7">
   <img src="https://openbookphilly.com/wp-content/uploads/2016/11/bookstack.png">
   <img src="https://media.wired.com/photos/5b86fce8900cb57bbfd1e7ee/master/w_582,c_limit/Jaguar_I-PACE_S_Indus-Silver_065.jpg">
   <img src="https://atlantis.nyc3.digitaloceanspaces.com/styled/1bec9ec74aac91e70b3ef91fee1fc0f9">
   <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR3DXqVk9AhGSx2PIYoUepA1UfZFnGt_kY6iJTq3hb10ZLGhFwPQg">
</div>

This is the link to your source code: source code

Answer №3

Here is some code you can try:

.grid-container {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
}

img {
    width: 100%;
    height: 140px;
}
<div class="grid-container">
   <img src="https://www.exampleimage1.com/image.jpg">
   <img src="https://www.exampleimage2.com/image.jpg">
   <img src="https://www.exampleimage3.com/image.jpg">
   <img src="https://www.exampleimage4.com/image.jpg">
   <img src="https://www.exampleimage5.com/image.jpg">
   <img src="https://www.exampleimage6.com/image.jpg">
</div>

Remember to use images of the same size if you plan on using height:auto.

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

Ways to interact with a button that toggles between states

I am working with a button that has both an enabled and disabled state. Enabled State: <button type="button" class="btt-download-csv site-toolbar-menu-button icon-download no-icon-margins ng-isolate-scope" title="Download CSV" rc-download-csv="" curren ...

Tips for preventing Chrome from masking the background image and color on an autofill input

Google Chrome Browser has caused the background-color and background-image effects to be removed from the Username and Password input fields. Before autocomplete https://i.stack.imgur.com/Ww7Hg.png After autocomplete https://i.stack.imgur.com/hbG2C.png ...

I'm looking to eliminate the redundant code within this CSS file

My CSS design for the three 'nav-item's requires me to have different styles for each one, making the code repetitive. Is there a way to streamline the code while keeping the unique design of the first 'nav-item'? .nav { width: 100 ...

The Owl carousel seems to be malfunctioning as there are no error messages displayed and the div containing it disappears unexpectedly

I'm having an issue with Owl carousel. I've included the necessary owl.carousel.css, owl.carousel.js, and owl.theme.css files. Created a div with the class "owl-carousel", and called the function $(".owl-carousel").owlCarousel();. However, after ...

Utilizing the Jquery hover feature to reveal or conceal an element

My Hover function is designed to display and hide sub menus when a person hovers on them. The issue I'm facing is that the menu disappears when I move the mouse down towards it. Can someone help me identify what I am doing wrong here? ...

What is the best way to tailor a SELECT statement option to reveal a specific div on a different webpage?

Whenever I click on a drop-down list, it redirects me to the same page. What I actually want is for each drop-down option to lead to different pages within my project. Can someone please assist me with this? Below is the PHP code snippet responsible for h ...

The outline color cannot be removed from vue-carousel

I recently downloaded the Vue Carousel library and here is the version I am using: "vue": "^2.6.11", "vue-carousel": "^0.18.0", When I click on the pagination, a focus effect is added to the element with an outlin ...

What are the steps to personalize Twitter Bootstrap with Less for changing the height of the Navbar?

I recently stumbled upon some interesting articles that explain how to customize various elements of Twitter Bootstrap using LESS. You can check out one of the articles here and find more information about Twitter Bootstrap here. However, I am still unsure ...

Encountered an Angular 9 error at core.js:6228, where a TypeError occurs when trying to read the property 'name' of an undefined

I'm currently working on developing a decentralized application (Dapp) with Angular 9 for the frontend. However, I've encountered an error that says: ERROR TypeError: Cannot read property 'name' of undefined. Here's a snippet of ...

"Effortlessly move files with Filedrop HTML 5 and Jquery drag-and-drop feature

I am trying to implement a drag and drop file feature on my webpage, but for some reason, it's not working as expected. The drop zone is supposed to change its background color when I drag a file onto it, but that functionality doesn't seem to be ...

What is the best way to send a variable to a custom component without having it executed or processed upfront?

I recently created a unique custom component that includes a form group with specific functionality. <template> <div class="form-group"> <label for="desc">{{label}}</label> <input id="desc" type="text" class="form-cont ...

Personalized element IDs and unique CSS styles

After editing the code snippet below... <div id="rt-utility"><div class="rt-block fp-roksprocket-grids-utility title5 jmoddiv"> I applied the changes in my custom.css file like this: #rt-utility .rt-block {CODE HERE} However, when attemptin ...

Embed a Telegram account onto an HTML page using the <iframe> element

Is there a way to display the personal Telegram account in an iframe tag? I experimented with the code below, but unfortunately it did not produce the desired result: <iframe src="https://telegram.me/joinchat/BfNEij9CbDh03kwXacO5OA"></iframe> ...

What could be causing the video autoplay feature to not function properly when used in conjunction with a bootstrap carousel

My video is not auto-playing. The same code works when placed in a different file, but it's not working on my page. Can anyone help with this issue? The URL where the video is not autoplaying: I have tried everything I can think of to get it working ...

Exploring the Differences in CSS Styling between Web and Mobile Platforms

Recently, I launched my new website and encountered a peculiar issue. Upon clicking on Pickup Date & Time, a bootstrap datepicker pops up like it should. However, the problem arises when viewing the website on mobile devices - the datepicker appears offsc ...

Include the HTTP header in a GET request for an HTML hyperlink

Within my HTML code, I am using an <a> tag that will trigger a 302 redirect when clicked. However, I need to incorporate some HTTP headers into this GET request. Is there a way to achieve this without including the headers in the href attribute? Tha ...

Issues with the functionality of the submit button in Jquery Mobile

Hey everyone, I'm currently developing a web application that features a menu on the left side and a div container on the right. When I click on any of the menu items, the content from other pages should be displayed in the div using Ajax. Here is the ...

configure dynamic content within the slider element

Currently, I am experimenting with two jQuery plugins (awkward/Coda Slider 3) to implement a sliding effect for DIV content. Everything seems to be working smoothly until I attempt to set dynamic content (using JavaScript) after creating the plugin object. ...

Creating a form in PHP with the power of JavaScript, AJAX, and jQuery

I have successfully created a registration form using HTML, processed the data with PHP, and utilized javascript, ajax, and jquery. However, I am facing an issue where I want to display a notification stating whether the operation was "inserted/failed" on ...

Modifying URL in Node.js

Recently delving into node.js, I'm eager to learn how to customize my URL structure like localhost:8080/someText, with 'someText' representing the desired page. I'd greatly appreciate any tutorials on achieving this, as well as guidance ...