Issues with CSS Grid margins being incorrect

Currently, I am working with two CSS grids. One of them has even spacing, while the other does not, although they are identical.

Below is the grid with correct spacing:

https://i.sstatic.net/iwNwf.png

And here is the grid with incorrect spacing:

https://i.sstatic.net/AEvgY.png

You can view the codepen at: https://codepen.io/Hedgy134117/pen/GRRWjxw (Apologies for the strange spacing - I'm using django to set up the HTML.)

HTML

<div class="stat">
<div class="stat-section popout">
            <h1>Equipment</h1>
                <div class="item">
                    <p class="item-name">1 A Jar of Tentacles with Eyeballs on the end</p>
                            <p><a class="item-edit" href="/sheets/editItem/A%20Jar%20of%20Tentacles%20with%20Eyeballs%20on%20the%20end/xyvz/">Edit</a></p>
                            <p><a class="item-remove" href="/sheets/removeItem/A%20Jar%20of%20Tentacles%20with%20Eyeballs%20on%20the%20end/xyvz/">Remove</a></p>


                </div>
                    <p style="text-align: center;"><a href="/sheets/addItem/xyvz/">Add</a></p>
        </div>
<div class="stat-section popout">
            <h1>Spells</h1>
                <div class="spell">
                    <p class="spell-name"><a href="https://5thsrd.org/spellcasting/spells/acid_splash/">Acid Splash</a></p>
                    <p class="spell-level">Cantrip</p>
                            <p class="spell-remove"><a href="#"><s>Remove</s></a></p>
                </div>
                    <p style="text-align: center;"><a href="/sheets/addSpell/xyvz/">Add</a></p>
        </div>
</div>

CSS

.stat-main {
    text-align: center;
    padding: 5px;
    width: 100%;
}

.stat-section {
      padding: 10px;
      margin: 10px;
}

.stat {
      display: flex;
      flex-wrap: wrap;
      justify-content: space-between;
      align-items: center;
}

.spell {
    display: grid;
    grid-template-columns: 50% 25% 25%;
}


.item {
    display: grid;
    grid-template-columns: 50% 25% 25%;
}

.popout {
    border-radius: var(--rounded);
    background-color: #FFFFFF;
    box-shadow: 5px 5px 0px rgba(0, 0, 0, 0.4);
}

I'm still learning about CSS grid, margin, and padding so any assistance would be greatly appreciated.

Answer №1

Your CSS grid syntax executed flawlessly! However, the culprit for the unsightly appearance of the second .stat-section lies in its parent's display: flex property.

To remedy this, I recommend assigning a min-width to the .stat-section so that it can maintain its designated space properly.

Feel free to explore this CodePen for further reference :)

Answer №2

You have the option to utilize this code snippet to showcase your code with a specific width adjustment.

body {
  margin: 0px;
}

.stat-main {
  text-align: center;
  padding: 5px;
  width: 100%;
}

.stat-section {
  padding: 10px;
  margin: 10px;
}

.stat {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
}

.spell {
  display: grid;
  grid-template-columns: 50% 25% 25%;
}

.item {
  display: grid;
  grid-template-columns: 50% 25% 25%;
}

.popout {
  border-radius: var(--rounded);
  background-color: #FFFFFF;
  box-shadow: 5px 5px 0px rgba(0, 0, 0, 0.4);
  width: 100%;
  max-width: 270px;
}
<div class="stat">
  <div class="stat-section popout">
    <h1>Equipment</h1>
    <div class="item">
      <p class="item-name">1 A Jar of Tentacles with Eyeballs on the end</p>
      <p><a class="item-edit" href="/sheets/editItem/A%20Jar%20of%20Tentacles%20with%20Eyeballs%20on%20the%20end/xyvz/">Edit</a></p>
      <p><a class="item-remove" href="/sheets/removeItem/A%20Jar%20of%20Tentacles%20with%20Eyeballs%20on%20the%20end/xyvz/">Remove</a></p>
    </div>
    <p style="text-align: center;"><a href="/sheets/addItem/xyvz/">Add</a></p>
  </div>
  <div class="stat-section popout">
    <h1>Spells</h1>
    <div class="spell">
      <p class="spell-name"><a href="https://5thsrd.org/spellcasting/spells/acid_splash/">Acid Splash</a></p>
      <p class="spell-level">Cantrip</p>
      <p class="spell-remove"><a href="#"><s>Remove</s></a></p>
    </div>
    <p style="text-align: center;"><a href="/sheets/addSpell/xyvz/">Add</a></p>
  </div>
</div>

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

What steps should be taken to ensure input validation in jquery SmartWizard when loading content via ajax requests?

I have successfully implemented jQuery SmartWizard to validate input type text. However, when the input text is loaded from Ajax, the validation does not work. Code for validation: // Toolbar extra buttons var btnFinish = $('<button></butto ...

Words displayed on an image, perfectly aligned in the center

Check out this fiddle: http://jsfiddle.net/WdZeC/ <div class="text_align_center" style="text-align: center;"> <div style="position: relative;"> <img width="27" height="28" src="http://www.kavoir.com/img/text-over-image.jpg"> ...

The menu includes a "scroll to #href" feature, however, it does not support links that open in a new tab (target blank)

I have encountered an issue with my website's navbar, which has a scroll-to-link function as it is a one-page site. Recently, I tried to add a new link to the menu that directs users to an external page (not within the same page). However, when I cl ...

Design a PHP tool for generating custom CSS frameworks

After attempting to create a CSS frame generator similar to this one, I stumbled upon a solution that works well with one exception. PHP Code: <?php function print_css($parentTag, $prefix, &$dup_checker) { if ($parentTag->nodeName == &apos ...

Maintaining the proportions of images in different screen sizes when resizing

I apologize if this question has already been addressed, but I have been unable to find a solution that works for my specific issue. My Gallery consists of a side list of available images in one section, which when clicked changes the image source in anot ...

Handling routerLink exceptions in Angular 2, 4, and 5

In my app, I am working on catching routing exceptions. There are three ways in which a user can navigate: If the user types the address directly - this can be caught easily by using { path: '**', redirectTo: 'errorPage'} in the route ...

Tips for using various versions of jQuery at the same time

Despite searching on Stack Overflow, none of the answers provided seem to work for my issue. My requirement is to utilize two separate versions of jQuery. Below is the code snippet: I first link to the initial version and use the following code: <sc ...

Conceal the div element if the value exceeds 0

I'm looking for a way to display a div when the number of remaining days is less than 0. I got it working on jsfiddle, but for some reason, it doesn't work anywhere else. if ($('.daysrem&a ...

Creating an animated Gif using HTML and CSS styling

I am trying to create an effect where a .gif animation plays once when the mouse hovers over a specific image. I have one static image in PNG format and one animated gif. The goal is for the gif to play every time the mouse is hovered over the png image. ...

The dimensions of the div do not match the size of the image

I am struggling to adjust the color of a transparent image within a div using jQuery. I am facing difficulties in keeping the image aligned within the div while changing its color. Can someone provide assistance? My goal is to have the image filled upon a ...

Refresh choices for the user interface selection menu

I've successfully mastered the art of redefining options for Webix ui.richselect/ui.combo. The technique involves: richselect.getList().clearAll(); richselect.getList().parse(options_data) Now, I'm facing a challenge with changing options fo ...

Display Bootstrap 4 Carousel thumbnails on the right side rather than the bottom

I have implemented the Bootstrap 4 carousel slider with the help of this example. In the example provided, I am trying to align the bottom thumbnails to the right side along with the thumbnail text, similar to the image attached below. https://i.sstatic. ...

Tips for maintaining a highlighted navigation link upon selection in Vue.js

My dilemma involves a navigation bar featuring router-links. My goal is to have the selected link visually highlighted so users can easily identify which page they are on. The current setup looks something like this: <div class="mynav"> ...

Is it better to return JSON before streaming a file through an iframe or sending HTTP headers?

I have created a form that utilizes a hidden iframe to submit a file to a script which then modifies the file and returns the altered version. I have realized that I can avoid saving the file anywhere by simply using echo file_get_contents(tmp);, where tmp ...

Having trouble retrieving the default selected value using the index in Angular Material's mat-button-toggle functionality

I'm encountering an issue with setting the default value for a toggle button group. The code is simple and the toggle buttons are correctly fetching values from the index, but I can't seem to get one of them to be default selected. I tried settin ...

Displaying an iFrame with a height that excludes the header section

My website contains an iframe with a height of 100% and a div positioned above it with a height of 70px. The issue is that the iframe overflows the page, causing the scrollbar to extend beyond the visible area. I am looking for a solution to adjust the ifr ...

Problem occurs when tab links vanish after clicking on another part of the website

Exploring the world of Javascript as a newcomer has been quite an adventure, but I've encountered a hurdle with tab links on my website. Everything seems to be working fine – the links cycle through and display the correct content. However, when it ...

Adjust the background color of a column in HTML based on its corresponding color name

Is there a way to change the background color of a column in an HTML table if the value is green? We want the background color to reflect the color name. The data will be retrieved from a web service. Here is the HTML code used to display the data in the ...

Exploring Website Layouts Across Various Screen Resolutions

I am facing an issue with my website layout. While it looks great on a 20" screen, on an 11" screen the elements are not displaying correctly. The #logo is overlapping the #menu, and the #bubble is appearing below the #frame. I had set percentage size para ...

Create a unique CSS class that combines custom styling with the pre-defined classes provided by

Hello everyone, I'm curious if there is a way to create a custom CSS class that incorporates existing Bootstrap classes? For instance, take a look at the following snippet of HTML: <div class="text-primary center-block">Here is some text</ ...