Having trouble centering the CSS image gallery properly

I recently created a website using HTML and CSS, which you can view here. It seems to be functioning fine overall, but there are some spacing issues within the image gallery section. Specifically, the first space appears larger compared to the others.

There is an inconsistency in the spacing between the first image and the subsequent ones.

Below is an excerpt from my CSS:

.gallery {
    -webkit-transition-duration: 0.25s;
    transition-duration: 0.25s;
    display: inline-block;
    border: 3px solid #8f8f8f;
    margin: 5px;
    border-radius: 15px;
    background-color: #29313d;
}
.gallery description {
    padding: 10px;
    text-align: center;
}
.gallery:hover {
    border: 3px solid #ccc;
    -webkit-transform: scale(1.05);
    transform: scale(1.05);
}
.gallery img {
    width: 200px;
    height: 100px;
    border-radius: 12.5px;
    object-fit: cover;
}

This portion of the code seems to be causing the inconsistency in spacing. How can I go about fixing this issue?

Answer №1

The area you are mentioning is known as margin. The width of the images in the .gallery is simply set to 200 without a unit specified, resulting in it being ignored by default in CSS.

Therefore, regardless of the actual size of the images in your gallery, the cards will adjust to the default size of the image. For example, all idk.png images are 1280 x 720 pixels (which is a 16:9 ratio). However, one image on the left has different dimensions at 768 x 432 pixels (also a 16:9 ratio).

The key difference lies in how CSS handles margins based on image sizes. When an image is scaled down, its margins also scale down accordingly. This explains why the margins appear smaller between all idk.png images. To resolve this issue, it's recommended to set a specific width for all images, not just those labeled as "idk.png".

<!DOCTYPE html>
<html>
<head>
  <!-- Styles -->
  <style>
    /* Defining CSS styles */
  </style>

  <!-- Title and Fonts -->
  <title>M100</title>
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  <link href="https://fonts.googleapis.com/css2?family=Nunito:ital,wght@0,200..1000;1,200..1000&display=swap" rel="stylesheet">
  <style>
    @font-face {
      font-family: myFont;
      src: url(Nunito-SemiBold.ttf);
    }
    
    /* Additional CSS styling */
  </style>
</head>

<body>
  <div class="containter">
    <div class="navbar">
      <nav>
        <ul>
          <li><a href=".">🏠 Home</a></li>
          <li><a href="./projects">đŸ“Ļ Projects</a></li>
          <li><a href="./resume">📄 Resume</a></li>
          <li><a href="./discord">đŸ’Ŧ Discord Server</a></li>
          <li><a href="https://www.roblox.com/groups/16658958">🕹ī¸ M100 Studios</a></li>
        </ul>
      </nav>
    </div>
    <!-- Content Section -->
    <div class="contentCenter">
      <center>
        <h1>Resume</h1>
        <p>Details about past work experiences with added flair.
          <img src="assets/images/cool-guy.png" width="25" height="25">
        </p>
        <br>
        <p>Visit my projects on <a href="https://scratch.mit.edu/users/Micamaster100">Scratch</a> or <a href="github.com/Micamaster100">Github</a></p>
        <br>
        <h2>Games</h2>
      </center>

      <!-- Gallery Section -->
      <div class="gallery">
        <a target="_blank" href="https://www.roblox.com/games/16955585161">
          <img src="https://raw.githubusercontent.com/Micamaster100/stuff/main/obbydoxxedthing.png">
          <div class="description">
            Interactive Obby Experience
          </div>
        </a>
      </div>
      <div class="gallery">
        <a target="_blank" href="about:blank">
          <img src="https://raw.githubusercontent.com/Micamaster100/stuff/main/idk.png">
          <div class="description">
            Under Construction...
          </div>
        </a>
      </div>
    </div>
  </div>
</body>
</html>

Answer №2

It seems like the issue may be caused by a missing .description class in addition to the width: 200 that you have already corrected. To resolve this, try adding a period before the description class as shown below.


 .gallery .description {
      padding: 10px;
      text-align: center;
    }

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

Alter the background color of the text input when it is selected for exit

How can I adjust the input field color when text is selected? I'm looking to get rid of the dark grey box highlighting. (Check out the image below) https://i.sstatic.net/OgWaz.gif <div id="ember1102" class="ember-view"> <i class="fa fa ...

Streamlined Navigation Bar Design for Websites

Struggling with aligning all the navbar options in a single row. Here is an example: https://jsfiddle.net/knyx2u8h/ ` Example Domain <meta charset="utf-8" /> <meta http-equiv="Content-type" content="text/html; char ...

Blazor Shared CSS File

Currently, I have successfully implemented 2 pages in my Blazor app: Login.razor located in the Auth folder AddPerson.razor situated in the Profile directory At this point, I have managed to integrate a CSS file specifically for the AddPerson.razor page ...

Designing a unique pagination layout for your WordPress website

Currently, I am working on a WordPress project. I have created my own template using CSS and HTML format. The implementation is going well, but I am facing difficulty integrating WP pagination design into my template. Below is the pagination code from my ...

Oops! An error occurred while using the Google Maps API. Let's fix this hic

I recently encountered an issue on my website where the Google Map displayed suddenly showed an error message saying: "Oops! Something went wrong. This page didn't load Google Maps correctly. See the JavaScript console for technical details." Afte ...

Select the specific element only if it is followed by no other siblings

I am puzzled by the following HTML markup and CSS style: [class^="container"] .target:last-of-type { color:red; } <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> ...

Tips for creating multiple popups using a single line of JavaScript code

I am new to JavaScript and I am attempting to create a popup. However, I am facing an issue in opening two divs with a single line of JavaScript code. Only one div opens while the other remains closed despite trying various solutions found on this website. ...

A data table created with Bootstrap is not displayed inline with the pagination feature

Customizing Bootstrap Data Table Code Instructions on how to customize this code: <div class="row"> <div class="col-sm-5"> <div class="dataTables_info" id="example_info" role="status" aria-live="polite"> Showing 1 to 10 of 2 ...

Is there a way to make text smoothly scroll and fade in upon page initialization?

I want to create a unique effect where text loads on the page, revealing one letter at a time from left to right in a fading manner. However, I also desire for each letter to slightly increase and decrease in size once while loading, creating a wave-like m ...

What is the best way to design a Global Navigation menu for websites?

For example, I am looking to integrate a Navigation menu into my website using just one file. I have considered using PHP or creating an HTML frame, but I am wondering what the current industry standard is for professionals. Any insights? ...

Is it possible to incorporate personalized JavaScript alongside bootstrap?

Just recently started using bootstrap and was curious about something. Can custom JavaScript be integrated with bootstrap buttons without any additional adjustments? ...

Adjusting Size Dynamically for Tooltips Based on Content Length

In my angular app using ng-bootstrap v4.2.1 on an older codebase, I have successfully created a tooltip with some very long sentences as content. I am trying to make the tooltip 800px wide, but when I set the width, only the first few words fill the space. ...

Guide on aligning all list items to the left using React Material-UI, incorporating fontAwesome icons, and styling with Tailwind.css

I am trying to align the text of my list items to the left. Here is what I have currently: https://i.stack.imgur.com/a5o5e.png Is there a way to left-align the text in this code snippet? import React from 'react'; import PropTypes from 'p ...

Is it possible to utilize retina images with CSS3's :before selector?

Can I adjust the size of an image inserted using :before? For example, if I want to use a high resolution image in the content below - how can I specify the dimensions? .buy_tickets_btn:before{ content: url(../images/buttons/pink_ticket_btn_bg.pn ...

Tips for adjusting an input box to match the width of a UI-grid cell

In a customizable ui-grid v3.1.0, the text input field doesn't expand to fill the cell when the column is resized by the user (refer to the image provided). https://i.sstatic.net/LRvst.png I attempted to adjust some CSS properties as shown below, bu ...

Trigger the opening of a class or window upon pressing the button

Good evening, I'm attempting to create a functionality where pressing a specific button will open a window. However, I am unsure of how to achieve this using CSS classes. My initial thought was to add a new class in the CSS file and call it every ti ...

When a jquery confirm dialog is displayed, the scroll bar mysteriously fades away

Whenever a jQuery confirm dialog is displayed, the scroll bar on the page disappears, causing the page to suddenly shift to the right. Here is the JavaScript code for the dialogue: <script src="vendor/jquery-confirm.min.js"></script> <link ...

Any tips on creating a website with a gradient background that moves along with the window as you scroll?

Can anyone help me figure out how to achieve this? I'm trying to create a similar effect to the one on the fireship website, but I'm having trouble replicating it. Appreciate any guidance. Thanks! ...

Trouble aligning Bootstrap 4 form fields in a horizontal layout

I'm struggling to align the user image and biography form fields with the rest of my signup form in Bootstrap 4. I've tried tweaking the CSS in various ways, but I can't achieve the desired visual effect: I want the biography and photo upl ...

Can one use Flexbox inside another Flexbox?

I am attempting to create a layout that looks like this: +---------+---------------------+ |legend | | +---------+ | |csv_input| map | | | | | | ...