使用CSS Grid时内容不会在移动设备中包装

时间:2019-10-03 10:51:56

标签: html css

我认为最好用gif来描述。这是正在发生的事情:

https://imgur.com/a/xhM2gJG

我希望内容继续在小屏幕上显示,但是它停止这样做了,并且出现了水平滚动条。

这里是codepen of the code

这是代码本身,因为StackOverflow坚持我也必须在这里放置代码。

HTML

<div class="l-grid">
  <header>
    <h1 class="logo"><span class="logo__brand-color">M</span>y Site</h1>
  </header>
  <main>
    <div class="quote">
      <img
        src="https://cdn.mos.cms.futurecdn.net/c7dppKDbG3JXuMfybV5tUX-320-80.jpg"
        alt="albert einstein"
        class="quote__image"
      />
      <p class="quote__body">
        A hundred times every day I remind myself that my inner and outer
        life are based on the labors of other men, living and dead, and that
        I must exert myself in order to give in the same measure as I have
        received and am still receiving.
      </p>
      <h3 class="quote__author">Albert Einstein</h3>
    </div>
    <div class="search">
      <input
        type="text"
        class="search__field"
        placeholder="Search quotes"
      />
    </div>
  </main>
  <footer>
    <ul class="footer-nav">
      <li class="footer-nav__item">
        <a href="#" class="footer-nav__link">About</a>
      </li>
      <li class="footer-nav__item footer-nav__item--separator">&middot;</li>
      <li class="footer-nav__item">
        <a href="#" class="footer-nav__link">Contribute</a>
      </li>
      <li class="footer-nav__item footer-nav__item--separator">&middot;</li>
      <li class="footer-nav__item">
        <a href="#" class="footer-nav__link">Contact</a>
      </li>
    </ul>
  </footer>
</div>

CSS

* {
  margin: 0;
  padding: 0;
  text-decoration: none;
  list-style: none;
  border: none;
  box-sizing: border-box;
}

:root {
  font-size: 100%;
}

html,
body {
  height: 100%;
}

body {
  background-color: #292c37;
}

.l-grid {
  display: grid;
  width: 1100px;
  margin: 0 auto;
  height: 100%;
  grid-template-columns: auto;
  grid-template-rows: auto 2fr 2fr;
  align-items: flex-end;
  grid-gap: 20px;
}

header {
  padding: 20px 0;
}

.logo {
  color: #fff;
  font-family: "Satisfy", cursive;
  font-weight: 100;
}

.logo__brand-color {
  color: hsl(355, 78%, 39%, 80%);
}

main {
  display: grid;
  grid-template-columns: auto;
  grid-template-rows: 1fr auto;
  grid-gap: 20px;
}

.quote {
  display: grid;
  grid-template-columns: auto 1fr;
  grid-template-rows: auto auto;
  font-size: 1rem;
  grid-gap: 50px;
}

.quote__image {
  border-radius: 50%;
  width: 140px;
  height: 140px;
  object-fit: cover;
  border: 2px solid hsl(0, 0%, 0%, 60%);
  border-style: inset;
}

.quote__body {
  font-family: "Lora", serif;
  color: hsl(0, 0%, 100%, 85%);
  line-height: 1.93em;
  font-size: 1.2em;
}

.quote__author {
  color: hsl(355, 78%, 39%, 80%);
  grid-column: span 2;
  text-align: right;
  font-size: 1.5em;
}

.search {
  background-color: #363a49;
  padding: 20px;
  border-radius: 3px;
}

.search__field {
  background-color: #292c37;
  border: none;
  border-radius: 3px;
  height: 43px;
  width: 100%;
  color: hsl(0, 0%, 100%, 60%);
  font-family: "Lora", serif;
  padding: 10px;
}

footer {
  font-size: 1rem;
  align-self: start;
}

.footer-nav {
  display: grid;
  grid-template-columns: repeat(5, 100px);
  justify-content: center;
  align-self: flex-start;
}

.footer-nav__item,
.footer-nav__link {
  color: hsl(0, 0%, 100%, 50%);
  text-align: center;
}

.footer-nav__link {
  font-size: 1em;
  font-family: "Roboto", sans-serif;
}

.footer-nav__item--separator {
  font-size: 3em;
  line-height: 0.5em;
}

@media (max-width: 1100px) {
  body {
    padding: 20px;
  }

  .l-grid {
    width: auto;
  }

  .quote {
    grid-template-columns: auto;
    grid-template-rows: auto auto;
    grid-gap: 10px;
  }

  .quote__image {
    display: none;
  }

  .quote__body {
    grid-column: auto;
  }

  .quote__author {
    grid-column: auto;
  }

  header {
    padding: 0;
  }
}

我尝试通过开发人员工具进行检查,以查看是否存在一些填充或空白问题,但没有发现。我不确定我还能尝试什么。

1 个答案:

答案 0 :(得分:1)

降低autofilll属性中的数字,如下所示 宽100像素的列使最少500像素的总数达到5列,因此低于500像素时响应度不好,或者,

.footer-nav {
    display: grid;
    grid-template-columns: repeat(5, 64px);
    justify-content: center;
    align-self: flex-start;
}