Исправить элементы в верхней части области просмотра - PullRequest
3 голосов
/ 18 марта 2019

Это довольно странное затруднение, потому что я в конечном итоге использовал flexbox для выравнивания пунктов меню в моих sidenav и header, а также в сетке для отображения / выделения пространства для моих компонентов nav и header,

Я пробовал способы сделать липкие боковые панели (или заголовки), основанные исключительно на сетке или flexbox, но они не работают вообще.

Вот пример того, что у меня есть:

<div class="grid-container">
    <nav>
        <div class="menu-item">Link</div>
    </nav>
    <div class="header"> Header Content Here </div>
    <main> Main Section. This should be able to scroll while Nav and 
           Header are sticking to the top
    </main>
</div>

CSS:

.grid-container {
    grid-template-columns: 200px 1fr;
    grid-template-areas: 
      "sidenav header"
      "sidenav main";
      box-shadow: 5px 10px !important;
  }

nav {
  grid-area: sidenav;
  box-shadow: 0 -1px 12px 2px whitesmoke;
  background-color: white;
  display: none;
  z-index: 100;
  flex-direction: column;
}

.header {
  grid-area: header;
  background-color: #f8f9ff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0px 16px;
}

main {
  grid-area: main;
  background-color: #f8f9ff;
}

Ответы [ 2 ]

1 голос
/ 18 марта 2019

Комбинировать свойства сетки и flexbox, как вы пытаетесь сделать, - это правильно - вам нужно добавить немного больше, чтобы они работали:

  • добавить высоту области просмотра в вашу сетку, чтобы вы моглисделайте ваши header и sidebar липкими.
  • вы можете указать header высоту, используя grid-template-rows (см. фрагмент ниже)
  • , добавьте overflow-y: auto, чтобы заставить только main переполнить.

См. демонстрацию ниже:

body  {
  margin: 0;
}

.grid-container {
  display: grid;
  height: 100vh; /* Add total height of the grid */
  grid-template-columns: 200px 1fr; /* sets sidenav width */
  grid-template-rows: 75px 1fr; /* sets header height */
  grid-template-areas: "sidenav header" "sidenav main";
  box-shadow: 5px 10px !important;
}

nav {
  grid-area: sidenav;
  box-shadow: 0 -1px 12px 2px whitesmoke;
  background-color: white;
  z-index: 100;
  border: 1px solid;
}

.header {
  grid-area: header;
  background-color: #f8f9ff;
  display: flex; /* flexbox to center items */
  align-items: center;
  justify-content: space-between;
  padding: 0px 16px;
  border: 1px solid;
}

main {
  grid-area: main;
  background-color: #f8f9ff;
  border: 1px solid;
  overflow-y: auto; /* add scrolling */
}
<div class="grid-container">
  <nav>
    <div class="menu-item">Link</div>
  </nav>
  <div class="header"> Header Content Here </div>
  <main> Main Section. This should be able to scroll while Nav and Header are sticking to the top<br/> Lorel ipsum some text here <br/> Lorel ipsum some text here <br/> Lorel ipsum some text here <br/> Lorel ipsum some text here <br/> Lorel ipsum some text here
    <br/> Lorel ipsum some text here <br/> Lorel ipsum some text here <br/> Lorel ipsum some text here <br/> Lorel ipsum some text here <br/> Lorel ipsum some text here <br/> Lorel ipsum some text here <br/> Lorel ipsum some text here <br/> Lorel ipsum
    some text here <br/> Lorel ipsum some text here <br/> Lorel ipsum some text here <br/> Lorel ipsum some text here <br/> Lorel ipsum some text here <br/> Lorel ipsum some text here <br/> Lorel ipsum some text here <br/> Lorel ipsum some text here <br/>    Lorel ipsum some text here <br/> Lorel ipsum some text here <br/> Lorel ipsum some text here <br/> Lorel ipsum some text here <br/> Lorel ipsum some text here <br/> Lorel ipsum some text here <br/> Lorel ipsum some text here <br/> Lorel ipsum some
    text here <br/> Lorel ipsum some text here <br/> Lorel ipsum some text here <br/>
  </main>
</div>
0 голосов
/ 18 марта 2019

Начните с добавления display: grid в контейнер и удаления display: none из элемента nav. Затем добавьте overflow: auto к элементу main, чтобы включить функцию прокрутки.

.grid-container {
  height: 100vh;
  display: grid;
  grid-template-columns: 200px 1fr;
  grid-template-areas: "sidenav header"
                       "sidenav main";
}

nav {
  grid-area: sidenav;
  background-color: aqua;
}

header {
  grid-area: header;
  height: 50px;
  background-color: orange;
}

main {
  grid-area: main;
  overflow: auto;
  background-color: #f8f9ff;
}

body {
  margin: 0;
}
<div class="grid-container">
  <nav>nav section</nav>
  <header>header section</header>
  <main>Main Section. This should be able to scroll while Nav and Header are sticking to the top<br><br>Main Section. This should be able to scroll while Nav and Header are sticking to the top<br><br>Main Section. This should be able to scroll while Nav and
    Header are sticking to the top<br><br>Main Section. This should be able to scroll while Nav and Header are sticking to the top<br><br>Main Section. This should be able to scroll while Nav and Header are sticking to the top<br><br>Main Section. This
    should be able to scroll while Nav and Header are sticking to the top<br><br>Main Section. This should be able to scroll while Nav and Header are sticking to the top<br><br>Main Section. This should be able to scroll while Nav and Header are sticking
    to the top<br><br>Main Section. This should be able to scroll while Nav and Header are sticking to the top<br>Main Section. This should be able to scroll while Nav and Header are sticking to the top<br><br>Main Section. This should be able to scroll
    while Nav and Header are sticking to the top<br><br>Main Section. This should be able to scroll while Nav and Header are sticking to the top<br><br>
  </main>
</div>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...