How to avoid an image overflowing container in CSS Grid?

Ask a question
4

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 :-)

offer bounty
add comment

0 Answer

Your Answer