kornavol
kornavol

Reputation: 21

Grid items overflowing the container

I've prepared three containers on the body. Header, main, and footer. The main uses a grid and contains a couple of children. The problem is that lower children are overflowing a container in height and in this case also they overflowing a footer.

The more elements I have, the more overflow is.

My goal is for "main" contains child elements without overflow.

I also would be appreciated for an explanation, why the main don't authorize automatically height having regard to I don't have any fixed size.

You can check, how it's working now on codepen: https://codepen.io/kornavol/pen/gOLXKKo

body {
  width: 80%;
  text-align: center;
  margin: auto;
}

.grid-container {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  grid-template-rows: minmax(0, 1fr);
  gap: 3%;
  margin: 20px auto 20px
}

.card {
  box-shadow: 0 0 20px #777;
  border-radius: 25px;
  text-align: center;
}

.card>img {
  width: 100%;
  height: 40%;
  object-fit: cover;
  border-radius: 0;
  border-top-left-radius: 25px;
  border-top-right-radius: 25px;
}

.card>a {
  text-decoration: none;
  color: black;
}

.card>a>h4 {
  width: 100px;
  background-color: rgb(255, 150, 22);
  border-radius: 5px;
  margin: auto;
}

.banner {
  width: 100%;
  height: 20vw;
  object-fit: cover;
}

header>h1 {
  height: 60px;
  line-height: 60px;
  border-left: 10px solid #00ffff;
  background-color: rgb(96, 104, 111);
  border-top-right-radius: 15px;
  color: white;
}

footer>img {
  border-top-left-radius: 15px;
  border-top-right-radius: 15px;
}

footer>h1 {
  height: 60px;
  line-height: 60px;
  border-left: 10px solid #00ffff;
  background-color: rgb(96, 104, 111);
  border-radius: 0;
  border-bottom-left-radius: 15px;
  color: white;
}
<div id="root">
  <div class="App">
    <header>
      <h1>Welcome on My Store</h1><img class="banner" style="border-bottom-left-radius: 25px; border-bottom-right-radius: 25px;" src="https://images.unsplash.com/photo-1472851294608-062f824d29cc?ixlib=rb-1.2.1&amp;ixid=eyJhcHBfaWQiOjEyMDd9&amp;auto=format&amp;fit=crop&amp;w=1050&amp;q=80"
        alt="Something went wrong! Please try again later." title="How's the scene">
    </header>
    <main class="grid-container">
      <div class="card"><img src="https://images.unsplash.com/photo-1512499617640-c74ae3a79d37?ixlib=rb-1.2.1&amp;ixid=eyJhcHBfaWQiOjEyMDd9&amp;auto=format&amp;fit=crop&amp;w=966&amp;q=80" alt="Something went wrong! Please try again later." title="How's the scene">
        <h3>Iphone</h3>
        <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Enim, distinctio facilis dolores ullam autem ad commodi.</p>
        <h4>Price: $1100</h4>
        <a href="">
          <h4>Add to Cart</h4>
        </a>
      </div>
      <div class="card"><img src="https://images.unsplash.com/photo-1593642632505-1f965e8426e9?ixlib=rb-1.2.1&amp;ixid=eyJhcHBfaWQiOjEyMDd9&amp;auto=format&amp;fit=crop&amp;w=925&amp;q=80" alt="Something went wrong! Please try again later." title="How's the scene">
        <h3>Samsung</h3>
        <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Enim, distinctio facilis dolores ullam autem ad commodi.</p>
        <h4>Price: $1200</h4>
        <a href="">
          <h4>Add to Cart</h4>
        </a>
      </div>
      <div class="card"><img src="https://images.unsplash.com/photo-1550029402-226115b7c579?ixlib=rb-1.2.1&amp;ixid=eyJhcHBfaWQiOjEyMDd9&amp;auto=format&amp;fit=crop&amp;w=701&amp;q=80" alt="Something went wrong! Please try again later." title="How's the scene">
        <h3>Huawei</h3>
        <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Enim, distinctio facilis dolores ullam autem ad commodi.</p>
        <h4>Price: $1300</h4>
        <a href="">
          <h4>Add to Cart</h4>
        </a>
      </div>
      <div class="card"><img src="https://images.unsplash.com/photo-1491933382434-500287f9b54b?ixlib=rb-1.2.1&amp;ixid=eyJhcHBfaWQiOjEyMDd9&amp;auto=format&amp;fit=crop&amp;w=700&amp;q=80" alt="Something went wrong! Please try again later." title="How's the scene">
        <h3>Nokia</h3>
        <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Enim, distinctio facilis dolores ullam autem ad commodi.</p>
        <h4>Price: $1400</h4>
        <a href="">
          <h4>Add to Cart</h4>
        </a>
      </div>
      <div class="card"><img src="https://images.unsplash.com/photo-1516163109866-e9d98630a0a6?ixlib=rb-1.2.1&amp;ixid=eyJhcHBfaWQiOjEyMDd9&amp;auto=format&amp;fit=crop&amp;w=1050&amp;q=80" alt="Something went wrong! Please try again later." title="How's the scene">
        <h3>Pixel</h3>
        <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Enim, distinctio facilis dolores ullam autem ad commodi.</p>
        <h4>Price: $1500</h4>
        <a href="">
          <h4>Add to Cart</h4>
        </a>
      </div>
      <div class="card"><img src="https://images.unsplash.com/photo-1572797258555-4f33f86f443f?ixlib=rb-1.2.1&amp;ixid=eyJhcHBfaWQiOjEyMDd9&amp;auto=format&amp;fit=crop&amp;w=1048&amp;q=80" alt="Something went wrong! Please try again later." title="How's the scene">
        <h3>Test</h3>
        <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Enim, distinctio facilis dolores ullam autem ad commodi.</p>
        <h4>Price: $1600</h4>
        <a href="">
          <h4>Add to Cart</h4>
        </a>
      </div>


    </main>
    <footer><img class="banner" style="border-top-left-radius: 25px; border-top-right-radius: 25px;" src="https://images.unsplash.com/photo-1542581509-7e87190743b6?ixlib=rb-1.2.1&amp;ixid=eyJhcHBfaWQiOjEyMDd9&amp;auto=format&amp;fit=crop&amp;w=1050&amp;q=80"
        alt="Something went wrong! Please try again later." title="How's the scene">
      <p>- 2021 -</p>
      <h1 class="la">We're looking forward to see you again</h1>
    </footer>
  </div>
</div>

Upvotes: 2

Views: 679

Answers (3)

Yogesh tiwari
Yogesh tiwari

Reputation: 16

hello the problem is while assign the values to footer you also have to defined it as a grid so to solve this problem in 3 lines you just have to add a id to footer and declare it
as grid after that just asign top padding to that id html

css css #footer{ display: grid; padding-top: 150px;}

Upvotes: 0

Michael Benjamin
Michael Benjamin

Reputation: 371113

The gap: 3% rule in the container is causing the main element to overflow the footer.

Try using grid-column-gap: 3% instead. You can add the remaining gaps to the grid items themselves: .card { margin: 10px }

OR, do the math so that the gap property can work on its own in the container.

body {
  width: 80%;
  text-align: center;
  margin: auto;
}

.grid-container {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  grid-template-rows: minmax(0, 1fr);
  grid-column-gap: 3%; /* adjustment */
  margin: 20px auto 20px
}

.card {
  margin: 10px; /* new */
  box-shadow: 0 0 20px #777;
  border-radius: 25px;
  text-align: center;
}

.card>img {
  width: 100%;
  height: 40%;
  object-fit: cover;
  border-radius: 0;
  border-top-left-radius: 25px;
  border-top-right-radius: 25px;
}

.card>a {
  text-decoration: none;
  color: black;
}

.card>a>h4 {
  width: 100px;
  background-color: rgb(255, 150, 22);
  border-radius: 5px;
  margin: auto;
}

.banner {
  width: 100%;
  height: 20vw;
  object-fit: cover;
}

header>h1 {
  height: 60px;
  line-height: 60px;
  border-left: 10px solid #00ffff;
  background-color: rgb(96, 104, 111);
  border-top-right-radius: 15px;
  color: white;
}

footer>img {
  border-top-left-radius: 15px;
  border-top-right-radius: 15px;
}

footer>h1 {
  height: 60px;
  line-height: 60px;
  border-left: 10px solid #00ffff;
  background-color: rgb(96, 104, 111);
  border-radius: 0;
  border-bottom-left-radius: 15px;
  color: white;
}
<div id="root">
  <div class="App">
    <header>
      <h1>Welcome on My Store</h1><img class="banner" style="border-bottom-left-radius: 25px; border-bottom-right-radius: 25px;" src="https://images.unsplash.com/photo-1472851294608-062f824d29cc?ixlib=rb-1.2.1&amp;ixid=eyJhcHBfaWQiOjEyMDd9&amp;auto=format&amp;fit=crop&amp;w=1050&amp;q=80"
        alt="Something went wrong! Please try again later." title="How's the scene">
    </header>
    <main class="grid-container">
      <div class="card"><img src="https://images.unsplash.com/photo-1512499617640-c74ae3a79d37?ixlib=rb-1.2.1&amp;ixid=eyJhcHBfaWQiOjEyMDd9&amp;auto=format&amp;fit=crop&amp;w=966&amp;q=80" alt="Something went wrong! Please try again later." title="How's the scene">
        <h3>Iphone</h3>
        <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Enim, distinctio facilis dolores ullam autem ad commodi.</p>
        <h4>Price: $1100</h4>
        <a href="">
          <h4>Add to Cart</h4>
        </a>
      </div>
      <div class="card"><img src="https://images.unsplash.com/photo-1593642632505-1f965e8426e9?ixlib=rb-1.2.1&amp;ixid=eyJhcHBfaWQiOjEyMDd9&amp;auto=format&amp;fit=crop&amp;w=925&amp;q=80" alt="Something went wrong! Please try again later." title="How's the scene">
        <h3>Samsung</h3>
        <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Enim, distinctio facilis dolores ullam autem ad commodi.</p>
        <h4>Price: $1200</h4>
        <a href="">
          <h4>Add to Cart</h4>
        </a>
      </div>
      <div class="card"><img src="https://images.unsplash.com/photo-1550029402-226115b7c579?ixlib=rb-1.2.1&amp;ixid=eyJhcHBfaWQiOjEyMDd9&amp;auto=format&amp;fit=crop&amp;w=701&amp;q=80" alt="Something went wrong! Please try again later." title="How's the scene">
        <h3>Huawei</h3>
        <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Enim, distinctio facilis dolores ullam autem ad commodi.</p>
        <h4>Price: $1300</h4>
        <a href="">
          <h4>Add to Cart</h4>
        </a>
      </div>
      <div class="card"><img src="https://images.unsplash.com/photo-1491933382434-500287f9b54b?ixlib=rb-1.2.1&amp;ixid=eyJhcHBfaWQiOjEyMDd9&amp;auto=format&amp;fit=crop&amp;w=700&amp;q=80" alt="Something went wrong! Please try again later." title="How's the scene">
        <h3>Nokia</h3>
        <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Enim, distinctio facilis dolores ullam autem ad commodi.</p>
        <h4>Price: $1400</h4>
        <a href="">
          <h4>Add to Cart</h4>
        </a>
      </div>
      <div class="card"><img src="https://images.unsplash.com/photo-1516163109866-e9d98630a0a6?ixlib=rb-1.2.1&amp;ixid=eyJhcHBfaWQiOjEyMDd9&amp;auto=format&amp;fit=crop&amp;w=1050&amp;q=80" alt="Something went wrong! Please try again later." title="How's the scene">
        <h3>Pixel</h3>
        <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Enim, distinctio facilis dolores ullam autem ad commodi.</p>
        <h4>Price: $1500</h4>
        <a href="">
          <h4>Add to Cart</h4>
        </a>
      </div>
      <div class="card"><img src="https://images.unsplash.com/photo-1572797258555-4f33f86f443f?ixlib=rb-1.2.1&amp;ixid=eyJhcHBfaWQiOjEyMDd9&amp;auto=format&amp;fit=crop&amp;w=1048&amp;q=80" alt="Something went wrong! Please try again later." title="How's the scene">
        <h3>Test</h3>
        <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Enim, distinctio facilis dolores ullam autem ad commodi.</p>
        <h4>Price: $1600</h4>
        <a href="">
          <h4>Add to Cart</h4>
        </a>
      </div>


    </main>
    <footer><img class="banner" style="border-top-left-radius: 25px; border-top-right-radius: 25px;" src="https://images.unsplash.com/photo-1542581509-7e87190743b6?ixlib=rb-1.2.1&amp;ixid=eyJhcHBfaWQiOjEyMDd9&amp;auto=format&amp;fit=crop&amp;w=1050&amp;q=80"
        alt="Something went wrong! Please try again later." title="How's the scene">
      <p>- 2021 -</p>
      <h1 class="la">We're looking forward to see you again</h1>
    </footer>
  </div>
</div>

Upvotes: 1

Ahmet &#199;etintaş
Ahmet &#199;etintaş

Reputation: 156

is it appropriate that way?

.grid-container {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) ;
    grid-template-rows: minmax(0, 1fr);
    margin: 20px auto 20px
   
}


.card {
    box-shadow: 0 0 20px #777;
    border-radius: 25px;
    text-align: center;
    display: inline-table;
    padding: 3%;
    margin: 3%;
}

.card > img {
    width: 100%;
    height: 40%;
    max-height: 250px;
    object-fit: cover;   
    border-radius: 0;
    border-top-left-radius: 25px;
    border-top-right-radius: 25px;
}

Upvotes: 0

Related Questions