如何调整元素大小并占用所有剩余空间?

时间:2017-09-28 18:59:26

标签: html css css3 resizable css-grid

我有一个有3个部分的网格。我想要的是中间的“地图”部分可以调整大小,当它变小时,“点”部分将扩展以占用所有剩余的空间。

#grid {
    display: grid;
    grid-template-rows: 1fr 6fr 2fr;
    grid-template-areas:
            "slider"
             "map"
             "points";
  width: 200px;
  height: calc(100vh - 10px);
}

.one {
  grid-area: slider;
  background-color: yellow;
}

.three {
  grid-area: points;
  background-color: green;
}

.resizeme {
  grid-area: map;
  resize: vertical;
  overflow: auto;
  background-color: orange;
  
}
<div id="grid">
  <div id="item1" class="one">hello1</div>
  <div id="item2" class="resizeme">you can resize me</div>
  <div id="item3" class="three">three</div>
</div>

1 个答案:

答案 0 :(得分:3)

不使用fr作为可调整大小元素的高度,而是使用auto。然后根据需要在项目本身上设置最小高度。

#grid {
  display: grid;
  grid-template-rows: 1fr auto 2fr;
  grid-template-areas: "slider" "map" "points";
  width: 200px;
  height: calc(100vh - 10px);
}

.one {
  grid-area: slider;
  background-color: yellow;
}

.three {
  grid-area: points;
  background-color: green;
}

.resizeme {
  grid-area: map;
  resize: vertical;
  overflow: auto;
  background-color: orange;
  /* min-height: 50px; */
}
<div id="grid">
  <div id="item1" class="one">hello1</div>
  <div id="item2" class="resizeme">you can resize me</div>
  <div id="item3" class="three">three</div>
</div>