• flex布局实现 内容区域高度自适应


    如果可以实现记得点赞分享,谢谢老铁~

    一、背景说明

    对于纵向排列布局,且上中下个个模块都是自动高度。当我们针对中间部分需要自适应高度且进行滚动时,那我们就可以用flex: 1 来处理。

    二 、先看效果图

    请添加图片描述

    二 、flex布局
    DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>flex布局实现 内容区域高度自适应title>
      <style>
        * {
          padding: 0;
          margin: 0;
        }
        .directoryCon {
          height: 100vh;
          width: 100%;
          background-color: aqua;
          display: flex;
          flex-direction: column;
          justify-content: space-between;
        }
        .directoryCon .headerTitle {
          height: 100px;
          background-color: blue;
          display: flex;
          justify-content: center;
          align-items: center;
        }
        .directoryCon .main {
          flex: 1;
          background-color: rgb(0, 174, 255);
          overflow: hidden;
          display: flex;
        }
        .directoryCon .main .menu{
          display: flex;
          flex-direction: column;
        }
        .directoryCon .main .menu .logo{
          height: 100px;
          width: 100%;
          background-color: blueviolet;
          display: flex;
          align-items: center;
          justify-content: center;
        }
        .directoryCon .main .menu .list{
          flex: 1;
          overflow-y: scroll;
          background-color: rgb(255, 166, 0);
        }
        .directoryCon .main .menu ul>li{
            height: 200px;
            width: 250px;
            display: flex;
            justify-content: center;
            align-items: center;
            border-bottom: 1px solid #000;
        }
    
        .directoryCon .footer {
          height: 100px;
          background-color: rgb(137, 206, 97);
          display: flex;
          justify-content: center;
          align-items: center;
        }
      style>
    head>
    <body>
      <div class="directoryCon">
        <div class="headerTitle">
          <h1>头部h1>
        div>
        <div class="main">
          <div class="menu">
            <div class="logo">
              <h1>FLEX布局h1>
            div>
            <ul  class="list">
              <li>我是菜单li><li>我是菜单li><li>我是菜单li><li>我是菜单li><li>我是菜单li>
              <li>我是菜单li><li>我是菜单li><li>我是菜单li><li>我是菜单li><li>我是菜单li>
              <li>我是菜单li><li>我是菜单li><li>我是菜单li><li>我是菜单li><li>我是菜单li>
              <li>我是菜单li><li>我是菜单li><li>我是菜单li><li>我是菜单li><li>我是菜单li>
            ul>
          div>
        div>
        <div class="footer">
          <h1>底部h1>
        div>
      div>
    body>
    html>
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    二 、解析

    在 Flexbox 布局中,我们可以通过设置容器的 display 属性为 flex 实现自适应高度。

    当我们将容器的 display 属性设置为 flex 时,容器中的所有子元素都会成为 Flexbox 容器的项目。

    我们可以通过设置 flex-direction 属性来确定 Flexbox 容器中子元素的排列方向。默认情况下,子元素的排列方向是 flex-direction: row,即按照行方向排列。

    在实现自适应高度时,需要将容器的 display 属性设置为 flex 和 flex-direction 属性设置为 column,就可以轻松地解决传统布局中内容自适应高度的问题。

    同时,我们还设置了容器的 height 属性为 height: 100vh ,为容器定义了一个固定的高度。并通过设置所有子元素的 flex 属性为 1,让它们平分容器的剩余空间。

    收工!谢谢老铁们的点赞收藏~

  • 相关阅读:
    Redis - 启动
    AVL平衡二叉树
    如何使用Spring和Thymeleaf轻松地在HTML表单映射上显示多个复选框
    SpringBoot入门
    C++ Primer 第5章 语句
    使用CSS渲染不同形状
    《实现领域驱动设计》笔记——架构
    三、TensorBoard
    MFC Windows 程序设计[198]之时间日期控件(附源码)
    探花交友_第1章_项目介绍以及实现登录功能_第3节_实现登录功能
  • 原文地址:https://blog.csdn.net/Gas_station/article/details/132848806