如何避免CSS网格中的图像溢出容器?

时间:2019-05-12 12:31:28

标签: css image css-grid

我尝试在CSS Grid上制作一个高度为100%的页面,但是当我将图像放入其容器中并缩小窗口时,内容在其他容器上垂直溢出。它仅在基于100vh的页面上发生。.我仅尝试使用Grid属性,但没有任何效果

有人可以解决吗?

我当前的代码: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;
 }

我仍在学习Grid Layout,因此欢迎一些有关最小化CSS代码的建议:-)

2 个答案:

答案 0 :(得分:0)

我尝试使此视图尽可能简单

这是jsfiddle的链接,看看: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>

答案 1 :(得分:0)

我找到了解决方案。删除CSS代码上的align-items,溢出问题消失。要使图像居中,请使用margin:auto。

新代码:https://jsfiddle.net/k1fmodv5/

CSS

static void Main(string[] args)
{
   Console.Write("Ange ett ord: ");
   string ord = Console.ReadLine();
   kryptering(ord);
}

public static void kryptering(string p)
{
   int ordTest = p.Length;
   for (int i = 0; i < ordTest; i++)
   {
      int q = p[i] + 1;
      char svar = Convert.ToChar(q);
      Console.Write(q);
   }
}

谢谢大家的帮助!