CSS Grid, получая полосу прокрутки, когда я не должен быть - PullRequest
0 голосов
/ 01 октября 2019

У меня есть базовая раскладка . Я пытаюсь создать макет с помощью CSS-сетки.

По какой-то причине я получаю полосу прокрутки, когда меня не должно быть, так как нет содержимого такого размера, чтобы гарантировать полосу прокрутки.

Я создал этот кодекс .

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://www.viva.org.uk/sites/default/files/styles/large/public/Albert%20Einstein_0.jpg?itok=A5nVyyns"
        alt="celadet"
        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">Help</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: 1;
  grid-template-rows: auto 1fr auto;
  align-items: center;
}

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

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

.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;
}

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

.footer-nav__item {
  display: flex;
  justify-content: center;
  align-items: center;
}

.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;
}

Ответы [ 4 ]

2 голосов
/ 01 октября 2019

Я не уверен на 100% в этом.

Но я думаю, что виновником всего этого является css, применяемый к:

<li class="footer-nav__item footer-nav__item--separator">·</li>

То есть

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

Размер шрифта по умолчанию: 16px

Теперь, чтобы узнать, какой высоты потребуется нашему тексту, умножим font-size на line-height (em, rem, px или любая другая единица измерения не имеет значения для высоты строки)

3em * 16px = 48px // Size of the character 
48px * .5 = 24px  // The height of the text

Как вы можете видеть, персонаж намного больше высоты, поэтому он выплеснется и появится полоса прокрутки.

Решения:

  1. Дайтенижний колонтитул больше высоты
  2. Настройте line-height или font-size
  3. Если это всего лишь одна страница, просто скрыть переполнение кажется самым простым решением
0 голосов
/ 01 октября 2019

Вам необходимо обновить grid-template-rows: auto; css для удаления полосы прокрутки.

.l-grid {
    display: grid;
    width: 1100px;
    margin: 0 auto;
    height: 100%;
    grid-template-rows: auto;
    align-items: center;
}
0 голосов
/ 01 октября 2019

Попробуйте:

body{
   overflow-y:hidden;
}

Это может помочь вам!

0 голосов
/ 01 октября 2019

Вы можете сделать это следующим образом: Удалить высоту из .l-grid css или добавить высоту: auto

   

.l-grid {
      display: grid;
      width: 1100px;
      height:auto;
      margin: 0 auto;
      grid-template-columns: 1;
      grid-template-rows: auto 1fr auto;
      align-items: center;
 }
...