maneroto
maneroto

Reputation: 163

Why are grid items overflowing the grid container?

I want my grid container to auto-adjust to the content it has. It's parent also has a height of auto. At this time, the grid is being overflowed by its content. It has this behavior:

xxxxxxxxxxcontainerxxxxxxxxxx   _
|         |        |        |    |
|         |        |        |    |
|_________|________|________|    |
|         |        |        |    |
|         |        |        |    |
|_________|________|________|    |---------Grid items
|         |        |        |    |
|         |        |        |    |
|_________|________|________|    |
|xxxxxxxxx|xxxxxxxx|xxxxxxxx|    | 
|         |        |        |    |
|_________|________|________|   _|

I've tried to use the grid-template-columns, but it was only affecting the first row, so tried using grid-auto-columns and it resizes all the rows, but still overflowing.

I expect that if I add another row, the grid container will be resized to its content.

#allyes {
  position: relative;
  width: 100%;
  height: auto;
  background-image: url('../img/fondo-laptop.webp');
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
  background-attachment: fixed;
}

#allyes #allyesLogos {
  height: auto;
  margin-left: auto;
  width: 60%;
  padding: 2% 3%;
  background: rgba(255, 255, 255, 0.6);
}

#allyes #allyesTitle {
  font-size: var(--bigTitleSize);
  font-family: var(--titleFamily);
  color: white;
  text-transform: uppercase;
  margin-bottom: 5vh;
  text-shadow: 2px 2px rgba(0, 0, 0, 0.6);
}

#allyesLogos .logosTable {
  width: 100%;
  height: auto;
  display: grid;
  margin-bottom: 5vh;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  grid-auto-rows: 70px;
  align-items: center;
  justify-items: center;
  grid-gap: 2% 3%;
}
<section id="allyes">
  <div id="allyesLogos">
    <h1 id="allyesTitle">alianzas comericales</h1>
    <div class="logosTable">
      <div class='allyLogo'><img src="<?php echo $httpProtocol.$host.$url.'img/logos-alianzas/4yousee.webp'?>" alt="ally"></div>
      <div class='allyLogo'><img src="<?php echo $httpProtocol.$host.$url.'img/logos-alianzas/benq.webp'?>" alt="ally"></div>
      <div class='allyLogo'><img src="<?php echo $httpProtocol.$host.$url.'img/logos-alianzas/bright-sign.webp'?>" alt="ally"></div>
      <div class='allyLogo'><img src="<?php echo $httpProtocol.$host.$url.'img/logos-alianzas/dell.webp'?>" alt="ally"></div>
      <div class='allyLogo'><img src="<?php echo $httpProtocol.$host.$url.'img/logos-alianzas/dynascan.webp'?>" alt="ally"></div>
      <div class='allyLogo'><img src="<?php echo $httpProtocol.$host.$url.'img/logos-alianzas/elo.webp'?>" alt="ally"></div>
      <div class='allyLogo'><img src="<?php echo $httpProtocol.$host.$url.'img/logos-alianzas/fortinet.webp'?>" alt="ally"></div>
      <div class='allyLogo'><img src="<?php echo $httpProtocol.$host.$url.'img/logos-alianzas/glassapps.webp'?>" alt="ally"></div>
      <div class='allyLogo'><img src="<?php echo $httpProtocol.$host.$url.'img/logos-alianzas/hisense.webp'?>" alt="ally"></div>
      <div class='allyLogo'><img src="<?php echo $httpProtocol.$host.$url.'img/logos-alianzas/iadea.webp'?>" alt="ally"></div>
      <div class='allyLogo'><img src="<?php echo $httpProtocol.$host.$url.'img/logos-alianzas/lenovo.webp'?>" alt="ally"></div>
      <div class='allyLogo'><img src="<?php echo $httpProtocol.$host.$url.'img/logos-alianzas/LG.webp'?>" alt="ally"></div>
      <div class='allyLogo'><img src="<?php echo $httpProtocol.$host.$url.'img/logos-alianzas/nec.webp'?>" alt="ally"></div>
      <div class='allyLogo'><img src="<?php echo $httpProtocol.$host.$url.'img/logos-alianzas/panasonic.webp'?>" alt="ally"></div>
      <div class='allyLogo'><img src="<?php echo $httpProtocol.$host.$url.'img/logos-alianzas/samsung.webp'?>" alt="ally"></div>
      <div class='allyLogo'><img src="<?php echo $httpProtocol.$host.$url.'img/logos-alianzas/screengoo.webp'?>" alt="ally"></div>
      <div class='allyLogo'><img src="<?php echo $httpProtocol.$host.$url.'img/logos-alianzas/sharp.webp'?>" alt="ally"></div>
    </div>
  </div>
</section>

Upvotes: 3

Views: 5074

Answers (1)

Michael Benjamin
Michael Benjamin

Reputation: 371113

This is the source of the problem:

grid-gap: 2% 3%

It appears that the percentage grid gaps are being factored into the layout after the rendering engine has set the dimensions of the container.

It may also be an issue with what the percentages are being based on (container height? row height? something else?).

Either way, percentage values for grid gaps are causing grid items to overflow the grid container.

Try using a different unit of length, such as vh, px or em. They seem to work fine.

In the demo below, I used grid-gap: 2vh 3vh.

#allyes {
  position: relative;
  width: 100%;
  height: auto;
  background-image: url('../img/fondo-laptop.webp');
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
  background-attachment: fixed;
}

#allyes #allyesLogos {
  height: auto;
  margin-left: auto;
  width: 60%;
  padding: 2% 3%;
  background: rgba(255, 255, 255, 0.6);
}

#allyes #allyesTitle {
  font-size: var(--bigTitleSize);
  font-family: var(--titleFamily);
  color: white;
  text-transform: uppercase;
  margin-bottom: 5vh;
  text-shadow: 2px 2px rgba(0, 0, 0, 0.6);
}

#allyesLogos .logosTable {
  width: 100%;
  height: auto;
  display: grid;
  margin-bottom: 5vh;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  grid-auto-rows: 70px;
  align-items: center;
  justify-items: center;
  /* grid-gap: 2% 3%; */
    grid-gap: 2vh 3vh; /* adjustment */
  border: 1px solid red; /* demo */
}
<section id="allyes">
  <div id="allyesLogos">
    <h1 id="allyesTitle">alianzas comericales</h1>
    <div class="logosTable">
      <div class='allyLogo'><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></div>
      <div class='allyLogo'><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></div>
      <div class='allyLogo'><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></div>
      <div class='allyLogo'><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></div>
      <div class='allyLogo'><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></div>
      <div class='allyLogo'><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></div>
      <div class='allyLogo'><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></div>
      <div class='allyLogo'><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></div>
      <div class='allyLogo'><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></div>
      <div class='allyLogo'><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></div>
      <div class='allyLogo'><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></div>
      <div class='allyLogo'><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></div>
      <div class='allyLogo'><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></div>
      <div class='allyLogo'><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></div>
      <div class='allyLogo'><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></div>
      <div class='allyLogo'><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></div>
      <div class='allyLogo'><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></div>
    </div>
  </div>
</section>

Also note: The problem described in the question exists in Chrome and Firefox, but not in Edge. The grid container in Edge expands naturally to accommodate grid items along with percentage grid gaps.

Upvotes: 4

Related Questions