Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
383 views
in Technique[技术] by (71.8m points)

css完成高度的自适应

class Demo extends React.Component {

  state = {
    list: []
  }

  handleAdd = () => {
    const { list } = this.state;
    this.setState({ list: [...list, list.length] })
  }
  
  render() {
    const { list } = this.state
    return (
      <div className={styles.container}>
        <div className={styles.top}>
          <Button type="primary" onClick={this.handleAdd}>增加</Button>
        </div>
        <div className={styles.middle}>
          {
            list.map(item => <p className={styles.item}>{item}</p>)
          }
        </div>
        <div className={styles.bottom} />
      </div>
    );
  }
}
.container {
  display: flex;
  flex-direction: column;
  .top {
    width: 100px;
    height: 100px;
    background: lightcoral;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .bottom {
    width: 100px;
    height: 100px;
    background: lightblue;
  }
  .middle {
    flex: 1;
  }
  .item {
    width: 80px;
    height: 40px;
    margin: 5px;
    background: lightgreen;
  }
}

图片.png

  • 中间绿色的部分,不足高度时,就是自己的高度。
  • 中间绿色部分超出高度时,有最大高度,可滚动。
  • 绿色的最大高度时浏览器的高度减去顶部和底部,不同浏览器可能不同。
  • 不希望通过js获取元素高度,css能否搞定?

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

为中间部分添加

max-height:calc(100% - top高度 - 底部高度);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...