首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CSS网格的动态高度

CSS网格的动态高度
EN

Stack Overflow用户
提问于 2019-08-19 15:57:26
回答 2查看 1.1K关注 0票数 0

我有一个3行的列,它们都有一个动态高度。我想要的是第一排有最小高度,第二排占据自由空间,第三排也有最小高度。

这是我能做到的,问题是我不想让中间行把第三行推下去。基本上,我希望第二行占据所有的空间,并有一个卷轴。

按照我的示例,当我添加一些li元素时,就会出现问题。

代码语言:javascript
复制
* {
  box-sizing: border-box;
}

body {
  overflow-y: hidden;
  height: 100vh;
}

.grid {
  display: grid;
  grid-template-rows: min-content 1fr min-content;
  height: 100%
}

ul {
  list-style: none;
  overflow-y: auto;
  height: 70%;
}

li {
  margin: 5px;
  background-color: red;
}

.s-1 {
  background-color: green;
  min-height: 100px;
}

.s-3 {
  background-color: blue;
  min-height: 100px;
}
代码语言:javascript
复制
<section class="grid">
  <div class="s-1">Section 1</div>

  <div>
    <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
      <li>4</li>
      <li>5</li>
      <li>6</li>

    </ul>
  </div>

  <div class="s-3">Section 3</div>
</section>

EN

回答 2

Stack Overflow用户

发布于 2019-08-19 16:04:04

代码语言:javascript
复制
.row1 {
  height: 50px;
  width: 100%;
  background: red;
  position: fixed;
  top: 0px;
  left: 0px;
  z-index: 2
}
.row2 {
  height: 100vh;
  background: green;
  position: relative;
  z-index: 1;
}
.row3 {
  height: 50px;
  background: orange;
  position: fixed;
  bottom: 0px;
  left: 0px;
  z-index: 2;
  width: 100%;
}
代码语言:javascript
复制
<div class="row1"></div>
<div class="row2">
fsdfsdfsdfsdf <br/>
fsdfsdfsdfsdf <br/>
fsdfsdfsdfsdf <br/>
fsdfsdfsdfsdf <br/>
fsdfsdfsdfsdf <br/>
fsdfsdfsdfsdf <br/>
fsdfsdfsdfsdf <br/>
fsdfsdfsdfsdf <br/>
fsdfsdfsdfsdf <br/>
fsdfsdfsdfsdf <br/>
fsdfsdfsdfsdf <br/>
fsdfsdfsdfsdf <br/>
fsdfsdfsdfsdf <br/>
</div>
<div class="row3"></div>

也许这就是你要找的?

票数 0
EN

Stack Overflow用户

发布于 2019-08-19 16:25:36

使用网格- grid-template-rows: 100px calc(100% - 200px) 100px;

当我使用calc时,我设置了我想要的高度,100%的页面减去顶部和底部。

在代码笔:(https://codepen.io/omergal/pen/dybpOJr)中,我们看不到卷轴,因为它位于顶部和底部之间

代码语言:javascript
复制
* {
  box-sizing: border-box;
}

body {
  overflow-y: hidden;
  height: 100vh;
  margin: 0;
}

.grid {
  display: grid;
  grid-template-rows: 100px calc(100% - 200px) 100px;
  height: 100%;
}

ul {
  list-style: none;
  overflow-y: auto;
  height: 100%;
  margin:0;
  padding:0;
}

li {
  margin: 5px;
  background-color: red;
}

.s-1 {
  background-color: green;
  height: 100%;
}

.s-3 {
  background-color: blue;
  height: 100%;
}
代码语言:javascript
复制
<section class="grid">
  <div class="s-1">Section 1</div>

  <div>
    <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
      <li>4</li>
      <li>5</li>
      <li>6</li>
      <li>2</li>
      <li>3</li>
      <li>4</li>
      <li>5</li>
      <li>6</li>
      <li>1</li>
      <li>2</li>
      <li>3</li>
      <li>4</li>
      <li>5</li>
      <li>6</li>

    </ul>
  </div>

  <div class="s-3">Section 3</div>
</section>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57560224

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档