CSS / Flexbox: полосы прокрутки на элементе Flex? - PullRequest
1 голос
/ 24 января 2020

Как вы получаете полосы прокрутки на div, который является гибким элементом? У меня есть следующие настройки:

    <div class="main">
        <div class="search-bar">
            <input type="text"></input>
        </div>

        <div class="search-results">
            <table>
                <tr>
                    <td>
                        <div class="main-info">
                            Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test
                        </div>
                    </td>
                </tr>

                <tr>
                    <td>
                        <div class="main-info">
                            Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test
                        </div>
                    </td>
                </tr>

                <tr>
                    <td>
                        <div class="main-info">
                            Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test
                        </div>
                    </td>
                </tr>
            </table>
        </div>
    </div>

С CSS:

    .main-info {
        width: 650px;
        height: 350px;
        box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
    }
    .main {
        display: flex;
        flex-direction: column;
    }
    .search-bar {
        flex : 1;
    }
    .search-results {
        flex: 1;
        overflow-y: auto;
    }

Вся страница получает полосу прокрутки, когда элементы таблицы не помещаются на странице. Но я хочу, чтобы search-results div был с вертикальной прокруткой, в то время как search-bar остается фиксированным в верхней части страницы. Я заметил, что если я задаю max-height для search-results и задаю ему небольшое значение, например 100px, то div получит полосу прокрутки, но как мне получить ее без указания этой минимальной высоты?

JSFiddle : https://jsfiddle.net/gw7f8shy/

Ответы [ 2 ]

0 голосов
/ 24 января 2020

Вы должны использовать свойства max-height в css

<div class="main">
        <div class="search-bar">
            <input type="text"></input>
        </div>

        <div class="search-results">
            <table>
                <tr>
                    <td>
                        <div class="main-info">
                            Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test
                        </div>
                    </td>
                </tr>

                <tr>
                    <td>
                        <div class="main-info">
                            Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test
                        </div>
                    </td>
                </tr>

                <tr>
                    <td>
                        <div class="main-info">
                            Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test
                        </div>
                    </td>
                </tr>
            </table>
        </div>
    </div>

CSS код здесь:

.main-info {
        width: 650px;
        height: 350px;
        box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
    }
    .main {
        display: flex;
        flex-direction: column;
    }
    .search-bar {
        flex : 1;
    }
    .search-results {
        flex: 1;
        overflow-y: auto;
        max-height: 400px;
    }
0 голосов
/ 24 января 2020

Надеюсь, это то, что вы ожидаете.

измените эти CSS свойства.

.search-bar {
    flex: 1;
    position: sticky; /* or u can use position:fixed */
    top: 0;
    z-index: 9;
}

.main-info {
  width: 650px;
  height: inherit;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}

.main-info {
  height: calc(100vh - 38px);
}

.main {
  display: flex;
  flex-direction: column;
}

.search-bar {
  flex: 1;
  position: sticky;
  /* or u can use position:fixed */
  top: 0;
  z-index: 9;
}

.search-results {
  flex: 1;
  max-height: calc(100vh - 38px);
  overflow-y: auto;
}
<html>

<head>
  <title>Subway-Mobile</title>
</head>

<body>

  <div class="main">
    <div class="search-bar">
      <input type="text" />
    </div>

    <div class="search-results">
      <table>
        <tr>
          <td>
            <div class="main-info">
              Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test
            </div>
          </td>
        </tr>

        <tr>
          <td>
            <div class="main-info">
              Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test
            </div>
          </td>
        </tr>

        <tr>
          <td>
            <div class="main-info">
              Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test
            </div>
          </td>
        </tr>


        <tr>
          <td>
            <div class="main-info">
              Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test
            </div>
          </td>
        </tr>

        <tr>
          <td>
            <div class="main-info">
              Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test
            </div>
          </td>
        </tr>


        <tr>
          <td>
            <div class="main-info">
              Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test
            </div>
          </td>
        </tr>
      </table>
    </div>
  </div>
</body>

</html>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...