代码之家  ›  专栏  ›  技术社区  ›  yarek

css3flex:如何创建两列:100%+固定宽度?

  •  1
  • yarek  · 技术社区  · 8 年前

    #header {
      background-color: grey;
    }
    #container {
      display:flex;
      height: calc(100vh - 50px);
    }
    #chatAndUserContainer {
      display: flex;
      width: 100%;
    }
    #chatContainer {
      background-color: red;
      width:100%;
    }
    #usersContainer {
      background-color: green;
      width:320px;
    }
    <div id="header">header</div>
    <div id="container">
      <div id="chatAndUserContainer">
        <div id="chatContainer">
          chatContainer
        </div>
        <div id="usersContainer">
          usersContainer
        </div>
        
      </div>
    </div>

    问题是:当渲染时,

    有什么办法纠正吗?(我需要使用显示:柔性,不是绝对的)

    2 回复  |  直到 8 年前
        1
  •  4
  •   Temani Afif    8 年前

    你面对的是 收缩 效果。因为总宽度(100%+320px)大于100%,所以两个元素都会收缩,以适合其父容器。

    #header {
      background-color: grey;
    }
    #container {
      display:flex;
      height: calc(100vh - 50px);
    }
    #chatAndUserContainer {
      display: flex;
      width: 100%;
    }
    #chatContainer {
      background-color: red;
      width:100%;
    }
    #usersContainer {
      background-color: green;
      width:320px;
      flex-shrink:0;
    }
    <div id="header">header</div>
    <div id="container">
      <div id="chatAndUserContainer">
        <div id="chatContainer">
          chatContainer
        </div>
        <div id="usersContainer">
          usersContainer
        </div>
        
      </div>
    </div>

    或者不用 width:100% 换成 flex:1

    #header {
      background-color: grey;
    }
    #container {
      display:flex;
      height: calc(100vh - 50px);
    }
    #chatAndUserContainer {
      display: flex;
      width:100%;
    }
    #chatContainer {
      background-color: red;
        flex:1;
    }
    #usersContainer {
      background-color: green;
      width:320px;
    }
    <div id=“header”>页眉</div>
    <div id=“聊天和用户容器”>
    </div>
    <div id=“usersContainer”>
    用户容器
    
    </div>
        2
  •  0
  •   undg    8 年前

    你可以用 calc() #chatContainer ,或 min-width #usersContainer

    #header {
      background-color: grey;
    }
    #container {
      display:flex;
      height: calc(100vh - 50px);
    }
    #chatAndUserContainer {
      display: flex;
      width: 100%;
    }
    #chatContainer {
      background-color: red;
      width:calc(100% - 320px);
    }
    #usersContainer {
      background-color: green;
      width:320px;
      min-width: 320px;
    }
    <div id="header">header</div>
    <div id="container">
      <div id="chatAndUserContainer">
        <div id="chatContainer">
          chatContainer
        </div>
        <div id="usersContainer">
          usersContainer
        </div>
        
      </div>
    </div>