0

I try to make a page on CSS Grid with 100% heigth but hen I put an image in its container and I reduce the window, the content overflows vertically on my other container. It's only happened on a page based on 100vh.. I tried hard only with Grid proprieties but nothing works

Someone has a solution ?

My current code : https://jsfiddle.net/kxaobqcr/3/ HTML

    <header>
      <div class="type"> <h1>Title</h1> </div>
      <div class="nav">
        <a href="#"> x </a>
        <a href="#"> x </a>
        <a href="#"> x </a>
        <a href="#"> x </a>
        <a href="#"> x </a>
        <a href="#"> x </a>
      </div>
    </header>

    <section class="content">
      <div class="library"> <img src="https://i.pinimg.com/originals/2d/8f/3e/2d8f3ef4a6bac30d0a4c7a44f7b3d574.jpg" alt=""> 
      </div>
    </section>
  </body>

CSS

html,body {
  margin: 0;
  padding: 0;
  height: 100%;
  width: auto;
  font-family: 'Libre Baskerville', serif;
}

body {
  display: grid;
  width: 100vw;
  height: 100vh;
  grid-template-columns: repeat(12, 1fr);
  grid-template-rows: repeat(12, 1fr);
  grid-gap: 10px 10px;
}

header {
  display: grid;
  grid-template-columns: repeat(12, 1fr);
  grid-template-rows: repeat(2, 1fr);
  grid-row: 1/ 3;
  grid-column: 1/ 13;
  grid-gap: 10px 10px;
  background-color: grey;
}

.type {
  display: grid;
  grid-row: 1/ 2;
  grid-column: 1/ 8;
  grid-template-columns: 1fr;
  grid-template-rows: 1fr;
  align-self: center;
  background-color: lightcoral;
}

.nav {
  display: grid;
  grid-row: 2/ 3;
  grid-column: 1/ 8;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: 1fr;
  align-self: center;
  background-color: crimson;
}

a {
   color: black;
   text-decoration: none;
   padding: 10px;
 }

img {
  max-width: 100%;
  max-height: 100%;
  object-fit: cover;
}

.content {
  display: grid;
  grid-template-columns: repeat(12, 1fr);
  grid-template-rows: repeat(12, 1fr);
  grid-column: 1/ 13;
  grid-row: 3 / 13;
  grid-gap: 10px 10px;
 }

 .library {
   grid-column: 4/ 10;
   grid-row: 4 / 10;
   justify-self: center;
   align-self: center;
 }

I am still learning Grid Layout, some advices for minimize my CSS code would be welcomed :-)

maher
  • 43
  • 1
  • 8
  • please read the tag description before using them – Temani Afif May 12 '19 at 12:36
  • contents of `header` is overflowing? add `min-height: 0` to `type` element... see https://jsfiddle.net/4j5m1s6r/ – kukkuz May 12 '19 at 12:39
  • @TemaniAfif sure, I will to be careful. sorry – maher May 12 '19 at 12:41
  • @kukkuz thank you it's make it better but can you explain me why the min-height properties has an effect on it ? – maher May 12 '19 at 12:48
  • by default *grid items* take the width / height of its content (similar to *flex items*) a reference can be seen [here](https://stackoverflow.com/questions/46303488).. a couple of examples [here](https://stackoverflow.com/questions/55007253/css-grid-creates-an-imaginary-column) and [here](https://stackoverflow.com/questions/55773596/how-to-use-overflow-hidden-on-1fr-grid-element/55773652#55773652) – kukkuz May 12 '19 at 12:51

2 Answers2

1

I try to make this view as simple as I can

This is a link of jsfiddle, have a look: https://jsfiddle.net/dupinderdhiman/zum2kxpw/5/

a{
  padding: 10px;
  border-top: 1px solid blue;
  border-bottom: 1px solid blue;
}

img {
  max-width: 100%;
  max-height: 100%;
  object-fit: cover;
}
<!-- 
  Bootstrap docs: https://getbootstrap.com/docs
-->

<div class="container">
  <div class="row">
    <div class="col-12">
     <h1>Title</h1> 
    </div>
    <div class="col-12">
       <div class="nav">
        <a href="#"> x </a>
        <a href="#"> x </a>
        <a href="#"> x </a>
        <a href="#"> x </a>
        <a href="#"> x </a>
        <a href="#"> x </a>
      </div>
    </div>
  </div>
  <div class="row">
  <div class='col-12 mt-1'>
    <img src="https://i.pinimg.com/originals/2d/8f/3e/2d8f3ef4a6bac30d0a4c7a44f7b3d574.jpg" alt="">
  </div>
  </div>  
</div>
Dupinder Singh
  • 7,175
  • 6
  • 37
  • 61
1

I found a solution. Delete align-items on css code and the overflow problem disappears. To center the image just use margin: auto.

New code : https://jsfiddle.net/k1fmodv5/

CSS

html,body {
  margin: 0;
  padding: 0;
  height: 100%;
  width: auto;
  font-family: 'Libre Baskerville', serif;
}

body {
  display: grid;
  width: 100vw;
  height: 100vh;
  grid-template-columns: repeat(12, 1fr);
  grid-template-rows: repeat(12, 1fr);
  grid-gap: 10px 10px;
}

header {
  display: grid;
  grid-template-columns: repeat(12, 1fr);
  grid-template-rows: repeat(2, 1fr);
  grid-row: 1/ 3;
  grid-column: 1/ 13;
  grid-gap: 10px 10px;
  background-color: grey;
}

.type {
  display: grid;
  grid-row: 1/ 2;
  grid-column: 1/ 8;
  grid-template-columns: 1fr;
  grid-template-rows: 1fr;
  align-self: center;
  background-color: lightcoral;
  min-height: 0;
}

.nav {
  display: grid;
  grid-row: 2/ 3;
  grid-column: 1/ 8;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: 1fr;
  align-self: center;
  background-color: crimson;
}

a {
   color: black;
   text-decoration: none;
   padding: 10px;
 }

img {
  max-width: 100%;
  max-height: 100%;
  object-fit: cover;
}

.content {
  display: grid;
  grid-template-columns: repeat(12, 1fr);
  grid-template-rows: repeat(12, 1fr);
  grid-column: 1/ 13;
  grid-row: 3 / 13;
  grid-gap: 10px 10px;
 }

 .library {
   grid-column: 4/ 10;
   grid-row: 4 / 10;
   margin:auto;
 }

Thank you all for your help !

maher
  • 43
  • 1
  • 8