如何使仅弹性项目溢出而不是整个父项溢出?

时间:2018-10-22 19:03:07

标签: html css html5 css3 flexbox

我的基本布局页面很小。

中间的内容带有包含过滤器的侧边栏。

请参阅:https://codepen.io/anon/pen/ePLmKB?editors=1100

我希望我的.heading.filter-block-heading始终可见,并且只让实际的滤镜卡可滚动。问题是整个flex列溢出,使我的列标题滚动到视图之外

我需要.main节中的内容才能扩展整个.content节。如果内容很小,则最大高度是视口减去页脚高度。但是,如果内容扩展,则侧边栏也应扩展。

我尝试将overflow: hidden设置为父母,然后将overflow: auto设置为孩子。

body {
  height: 100vh;
  overflow: auto;
}

.app {
  display: flex;
  flex-direction: column;
  position: relative;
  min-width: 1024px;
  width: 100%;
  height: auto;
}

header {
  background-color: #b4d15e;
}

footer {
  height: 56px;
  background-color: #5ed1b4;
}

.content {
  flex-direction: row;
  min-height: calc(100vh - 56px);
  display: flex;
}

.sidebar-1 {
  display: flex;
  flex-direction: column;
  position: relative;
  width: calc(20% + 0px);
  max-width: 300px;
  min-width: 200px;
  overflow-y: auto;
  box-shadow: 0 0 4.9px 0.2px rgba(0, 0, 0, 0.5);
  border-color: #e0e1e6;
  border-style: solid;
  border-width: 0 1px 0 0;
  flex: none;
  background: #f4f5f8;
}

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

.heading h2 {
  margin: 0;
}

.heading>div {
  display: flex;
  padding: 15px 7px 15px 12px;
  align-items: center;
}

.filters {
  position: absolute;
  height: auto;
  margin-top: 50px;
  width: 100%;
}

.filter-block {
  display: flex;
  flex-direction: column;
}

.filter-block-heading {
  min-height: 0;
  display: flex;
  flex-direction: row;
  flex-shrink: 0;
  padding-left: 10px;
  border-bottom: solid 3px #404c9a;
  text-align: center;
  font-size: 14px;
}

.single-filters {
  display: flex;
  flex-direction: column;
  width: 100%
}

.filter-card {
  width: 100% padding: 5px;
  margin-top: 12px;
  border-top: 2px solid red;
  border-bottom: 2px solid green;
}

.filter-card h3 {
  background-color: #A8AAFF;
  position: sticky;
  top: 0px;
  font-weight: bold;
}

.filter-card li {
  margin-top: 6px;
}

.main {
  display: flex;
  flex-direction: column;
  flex: 1 1 0%;
  width: 580px;
  padding: 30px;
}

.sidebar-2 {
  position: relative;
  transition: all 0.25s;
  width: calc(25% + 30px);
  max-width: 300px;
  overflow: hidden;
  box-shadow: 0 0 4.9px 0.2px rgba(0, 0, 0, 0.5);
}
<body>
  <div class='app'>
    <header>
      Header</header>
    <div class='content'>

      <div class='sidebar-1'>
        <div class='heading'>
          <div>
            <h2>All Filters</h2>
            <button>Show all</button>
          </div>
        </div>

        <div class='filters'>
          <div>

            <div class='filter-block'>
              <div class='filter-block-heading'>
                Custom Filters
              </div>

              <div class='single-filters'>

                <div>

                  <div class='filter-card'>
                    <h3>Single Filter</h3>
                    <ul>
                      <li>Option 1</li>
                      <li>Option 2</li>
                      <li>Option 3</li>
                    </ul>
                  </div>
                  <div class='filter-card'>
                    <h3>Single Filter</h3>
                    <ul>
                      <li>Option 1</li>
                      <li>Option 2</li>
                      <li>Option 3</li>
                    </ul>
                  </div>
                  <div class='filter-card'>
                    <h3>Single Filter</h3>
                    <ul>
                      <li>Option 1</li>
                      <li>Option 2</li>
                      <li>Option 3</li>
                    </ul>
                  </div>
                  <div class='filter-card'>
                    <h3>Single Filter</h3>
                    <ul>
                      <li>Option 1</li>
                      <li>Option 2</li>
                      <li>Option 3</li>
                    </ul>
                  </div>
                  <div class='filter-card'>
                    <h3>Single Filter</h3>
                    <ul>
                      <li>Option 1</li>
                      <li>Option 2</li>
                      <li>Option 3</li>
                    </ul>
                  </div>
                  <div class='filter-card'>
                    <h3>Single Filter</h3>
                    <ul>
                      <li>Option 1</li>
                      <li>Option 2</li>
                      <li>Option 3</li>
                    </ul>
                  </div>
                  <div class='filter-card'>
                    <h3>Single Filter</h3>
                    <ul>
                      <li>Option 1</li>
                      <li>Option 2</li>
                      <li>Option 3</li>
                    </ul>
                  </div>
                  <div class='filter-card'>
                    <h3>Single Filter</h3>
                    <ul>
                      <li>Option 1</li>
                      <li>Option 2</li>
                      <li>Option 3</li>
                    </ul>
                  </div>
                  <div class='filter-card'>
                    <h3>Single Filter</h3>
                    <ul>
                      <li>Option 1</li>
                      <li>Option 2</li>
                      <li>Option 3</li>
                    </ul>
                  </div>
                  <div class='filter-card'>
                    <h3>Single Filter</h3>
                    <ul>
                      <li>Option 1</li>
                      <li>Option 2</li>
                      <li>Option 3</li>
                    </ul>
                  </div>

                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class='main'>main</div>
      <div class='sidebar-2'>sidebar 2</div>
    </div>
    <footer>Footer</footer>
  </div>
</body>

1 个答案:

答案 0 :(得分:-1)

由于以下规则,您在视口上出现了滚动条:

.content {
    min-height: calc(100vh - 56px);
}

因此,您要从footer的高度删除56px:

footer {
    height: 56px;
}

但是您没有减去header的高度。这就是为什么有溢出的原因。

尝试以下方法:

revised codepen

/* body {
  height: 100vh;
  overflow: auto;
} */

.app {
  display: flex;
  flex-direction: column;
  position: relative;
  min-width: 1024px;
  width: 100%;
  /* height: auto; */
  height: 100vh; /* new */  
}

.content {
  flex-direction: row;
  /* min-height: calc(100vh - 56px); */
  flex: 1; /* new */
  display: flex;
}

/* no more changes */

header {
  background-color: #b4d15e;
}

footer {
  height: 56px;
  background-color: #5ed1b4;
}


.sidebar-1 {
  display: flex;
  flex-direction: column;
  position: relative;
  width: calc(20% + 0px);
  max-width: 300px;
  min-width: 200px;
  overflow-y: auto;
  box-shadow: 0 0 4.9px 0.2px rgba(0, 0, 0, 0.5);
  border-color: #e0e1e6;
  border-style: solid;
  border-width: 0 1px 0 0;
  flex: none;
  background: #f4f5f8;
}

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

.heading  h2 {
  margin: 0;
}

.heading > div {
  display: flex;
  padding: 15px 7px 15px 12px;
  align-items: center;
}

.filters {
  position: absolute;
  height: auto;
  margin-top: 50px;
  width: 100%;
}

.filter-block {
  display: flex;
  flex-direction: column;
}

.filter-block-heading {
  min-height: 0;
  display: flex;
  flex-direction: row;
  flex-shrink: 0;
  padding-left: 10px;
  border-bottom: solid 3px #404c9a;
  text-align: center;
  font-size: 14px;
}

.single-filters {
  display: flex;
  flex-direction: column;
  width: 100%
}

.filter-card {
  width: 100%
  padding: 5px;
  margin-top: 12px;
  border-top: 2px solid red;
  border-bottom: 2px solid green;
}

.filter-card h3 {
  background-color: #A8AAFF;
  position: sticky;
  top: 0px;
  font-weight: bold;
}

.filter-card li {
  margin-top: 6px;
}

.main {
  display: flex;
  flex-direction: column;
  flex: 1 1 0%;
  width: 580px;
  padding: 30px;
}

.sidebar-2 {
  position: relative;
  transition: all 0.25s;
  width: calc(25% + 30px);
  max-width: 300px;
  overflow: hidden;
  box-shadow: 0 0 4.9px 0.2px rgba(0, 0, 0, 0.5);
}
<body>
  <div class='app'>
    <header>Header</header>
    <div class='content'>
      <div class='sidebar-1'>
        <div class='heading'>
          <div>
            <h2>All Filters</h2>
            <button>Show all</button>
          </div>
        </div>
        <div class='filters'>
          <div>
            <div class='filter-block'>
              <div class='filter-block-heading'>
                Custom Filters
              </div>
              <div class='single-filters'>
                <div>
                  <div class='filter-card'>
                    <h3>Single Filter</h3>
                    <ul>
                      <li>Option 1</li>
                      <li>Option 2</li>
                      <li>Option 3</li>
                    </ul>
                  </div>
                  <div class='filter-card'>
                    <h3>Single Filter</h3>
                    <ul>
                      <li>Option 1</li>
                      <li>Option 2</li>
                      <li>Option 3</li>
                    </ul>
                  </div>
                  <div class='filter-card'>
                    <h3>Single Filter</h3>
                    <ul>
                      <li>Option 1</li>
                      <li>Option 2</li>
                      <li>Option 3</li>
                    </ul>
                  </div>
                  <div class='filter-card'>
                    <h3>Single Filter</h3>
                    <ul>
                      <li>Option 1</li>
                      <li>Option 2</li>
                      <li>Option 3</li>
                    </ul>
                  </div>
                  <div class='filter-card'>
                    <h3>Single Filter</h3>
                    <ul>
                      <li>Option 1</li>
                      <li>Option 2</li>
                      <li>Option 3</li>
                    </ul>
                  </div>
                  <div class='filter-card'>
                    <h3>Single Filter</h3>
                    <ul>
                      <li>Option 1</li>
                      <li>Option 2</li>
                      <li>Option 3</li>
                    </ul>
                  </div>
                  <div class='filter-card'>
                    <h3>Single Filter</h3>
                    <ul>
                      <li>Option 1</li>
                      <li>Option 2</li>
                      <li>Option 3</li>
                    </ul>
                  </div>
                  <div class='filter-card'>
                    <h3>Single Filter</h3>
                    <ul>
                      <li>Option 1</li>
                      <li>Option 2</li>
                      <li>Option 3</li>
                    </ul>
                  </div>
                  <div class='filter-card'>
                    <h3>Single Filter</h3>
                    <ul>
                      <li>Option 1</li>
                      <li>Option 2</li>
                      <li>Option 3</li>
                    </ul>
                  </div>
                  <div class='filter-card'>
                    <h3>Single Filter</h3>
                    <ul>
                      <li>Option 1</li>
                      <li>Option 2</li>
                      <li>Option 3</li>
                    </ul>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class='main'>main</div>
      <div class='sidebar-2'>sidebar 2</div>
    </div>
    <footer>Footer</footer>
  </div>
</body>