CSS Grid margin not correct

Viewed 213

I currently have two CSS grids. One of them is spaced evenly while the other one is not, and they are the exact same.

This is the correctly spaced grid:

Correctly Spaced

This is the incorrectly spaced grid:

Incorrectly Spaced

Here's the codepen: https://codepen.io/Hedgy134117/pen/GRRWjxw (I apologize for the weird spacing, I am 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 very new to CSS grid and margin and padding, all help would be greatly appreciated.

2 Answers

I think your CSS grid syntax worked perfectly. It is just the display: flex parent making the second .stat-section look awful.

I suggest you define min-width to the .stat-section. So that the second .stat-section could retain its space.

Checkout this CodePen :)

You can use this code - display exactly what your code presents - change width

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>

Related