What causes padding to appear when adjusting the image size within a CSS grid layout?

Currently, I am working on my portfolio and have a projects section. In this section, I have implemented an image CSS grid that is functional but the images appear to be too large https://i.sstatic.net/KNHqJ.png. I would like to make them smaller, however, when I adjust the max-width or set a lower width for the image, it creates padding https://i.sstatic.net/erCgX.png. How can I solve this issue? I do not want there to be padding between the images in the grid. Thank you in advance for any advice!

.mygrid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  grid-column-gap: 0px;
  grid-row-gap: 0px;
}

.mygrid img {
  max-width: 75%;
  border: 1px solid green;
}
        <div class="mygrid mt-6">
          <img src="https://placekitten.com/1024/1024" alt="Sample photo" />
          <img src="https://placekitten.com/1024/1024" alt="Sample photo" />
          <img src="https://placekitten.com/1024/1024" alt="Sample photo" />
          <img src="https://placekitten.com/1024/1024" alt="Sample photo" />
          <img src="https://placekitten.com/1024/1024" alt="Sample photo" />
          <img src="https://placekitten.com/1024/1024" alt="Sample photo" />
          <img src="https://placekitten.com/1024/1024" alt="Sample photo" />
          <img src="https://placekitten.com/1024/1024" alt="Sample photo" />
          <img src="https://placekitten.com/1024/1024" alt="Sample photo" />
        </div>

Answer №1

When the term 75% is used, it indicates that there will be 25% of free space regardless of the configuration, so it is important not to manipulate the image size. Instead, adjust the column size accordingly:

.mygrid {
  display: grid;
  grid-template-columns: repeat(3, 25%); /* make adjustments here */
  justify-content: center;
}

.mygrid img {
  max-width: 100%; /* maintain 100% width here */
  border: 1px solid green;
}
<div class="mygrid mt-6">
  <img src="https://placekitten.com/1024/1024" alt="Sample photo" />
  <img src="https://placekitten.com/1024/1024" alt="Sample photo" />
  <img src="https://placekitten.com/1024/1024" alt="Sample photo" />
  <img src="https://placekitten.com/1024/1024" alt="Sample photo" />
  <img src="https://placekitten.com/1024/1024" alt="Sample photo" />
  <img src="https://placekitten.com/1024/1024" alt="Sample photo" />
  <img src="https://placekitten.com/1024/1024" alt="Sample photo" />
  <img src="https://placekitten.com/1024/1024" alt="Sample photo" />
  <img src="https://placekitten.com/1024/1024" alt="Sample photo" />
</div>

Answer №2

One alternative to using

grid-template-columns: repeat(3, 1fr);
is to try out
grid-template-columns: repeat(3, fit-content(100%));
. This adjustment ensures that each column adjusts to the size of the image being displayed.

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

Creating responsive CSS layouts for various device sizes

My webpage features a form with a prompt for the input box, followed by the input box itself. I want to implement two different layouts based on the device accessing the page. For cell phones, I want everything stacked vertically. However, for computers, ...

Having difficulty making Skrollr compatible with BootStrap 3 single page wonder

I am completely new to JavaScript, which is why I decided to use Skrollr. However, I have been facing some challenges in getting Skrollr to work properly on my webpage. I added the following code at the end of my page: <script type="text/javascript" sr ...

Implementing CSS animations in ReactJS: A guide to activating onClick and onHover events

Is there a way to activate the onClick and onHover CSS animations for the ReactJS button component below? I attempted to use ref = {input => (this.inputElement = input)}, but I only see the animation when clicking the button. <td> ...

Animate the page transition with jQuery once the CSS animation finishes

My screen is split in two halves, and when you click on a side, they are supposed to slide open like curtains and then fade into another page. I have successfully created the animation using CSS and jQuery to add the appropriate class on the click event. ...

Loading text with a remote web font has been successfully initiated in Fabric.js

Currently, I am immersed in developing a large custom application using Fabric JS and thus far, my progress has been commendable. However, I have encountered an issue concerning the initialization of loaded text objects that utilize web fonts. When the fo ...

My code seems to be malfunctioning - why can't I keep the aspect ratio?

UPDATE: Can someone please assist me with maintaining the aspect ratio of a drawn image? I am struggling to achieve this and would appreciate any help. I have been attempting to upload an image, draw it using the imageDraw() function, and fit it within a ...

Refreshing all parts of a webpage except for a single div

Currently, I am in the process of creating a simple web page that includes a small music player (niftyPlayer). The individuals I am developing this for request that the player be located in the footer and continue playing when users navigate to different s ...

Utilizing a combination of jQuery and JavaScript, construct an innovative image slider to meet

I'm working on creating a slider that allows users to navigate through reviews by clicking arrows, moving from review1 to review2 and so on. I've set up my HTML with the reviews and my CSS with hidden values for now. Any assistance would be great ...

Responsive timeline displaying issue on the left column

After tackling some challenges in previous forums to fix my responsive timeline, I resorted to copying code from codepen.io due to my laziness to create one from scratch. The complexity of creating a timeline also played a role in my decision. My current ...

The second div element remains unselected in jQuery

Below is the example of an HTML structure: <span id="17">yes here</span> <div class="PricevariantModification vm-nodisplay"></div> <div class="PricesalesPrice vm-display vm-price-value"> <span class="a"></span> ...

What are the steps to implement character movement in a 2D game using JavaScript?

I'm having trouble getting the image with the ID "yoshi" to move around in my 2D game document.onkeydown = (e) => { if (e.keyCode == 37) yoshi.style.left = yoshi.offsetLeft - 5 + "px"; else if (e.keyCode == 38) yoshi.style.top = yoshi.offset ...

How to pass the id value between pages in web developmentactics?

I've been struggling to call the id value of one page in another for a while now. I assigned the id value "addedcart" to the form and tried to call it in my PHP code, but no cart value is being displayed. I'm not sure if I am calling the id corre ...

Ensure this div adapts to different screen sizes

I have attempted using a width of 100%, but unfortunately, it did not result in a responsive design. To illustrate my point, here is the link to what I am striving to achieve in terms of responsiveness: codepen link div { background:url(https://i.stac ...

Using jQuery to remove an iframe upon a click event

My goal is to remove an iframe whenever anything on the page is clicked. I tried using this code: $('*').on('click',function(){ $('iframe').remove(); }); However, I encountered a problem where the i ...

Styling an input button to look like a link in Firefox and Internet Explorer

I'm currently using CSS to give my input button the appearance of a link. Here's how I've styled it: input#linkLike { background: none; border: none; cursor: pointer; margin: 0; padding: 0; text-decoration: none; ...

What is the best way to insert a "Read More" link following a short snippet of text

I am looking to implement multiple "read more" links to enable users to continue reading after a specified amount of text, such as 1000 words or 2 paragraphs. <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script> <script&g ...

Launching in an Angular reactive form

When working with Angular reactive forms, I am facing an issue where I want to set a form control value using a property from a service. However, upon submitting the form, the value of the form control does not reflect the expected property value from the ...

Pictures are not aligned at the center of the bigger card container

I am facing an issue with centering images on my cards. The containers look fine on PC (centered) when smaller, but on responsive mode they revert back to the larger size and are left aligned. I want the images in the card containers to be centered regardl ...

The significance of tabindex in CSS

Is it possible to manipulate tabindex using CSS? If so, which browsers support this feature and on what types of elements? UPDATE I am interested in capturing keydown events on a div element. After researching keyboard events on http://www.quirksmode.org ...

Sliding sideways with CSS thumbnail navigation

My goal is to create a horizontal scroll menu for a series of thumbnails. I have managed to make it scroll horizontally, but the issue I'm facing is that the thumbnails stack on top of each other and continue to scroll vertically within my div. What I ...