Flex项目包含输入元素时,不使用Flexbox flex-grow属性

时间:2019-10-06 17:41:24

标签: css flexbox

我正在使用flexbox制作聊天UI。首先使用flex-grow属性,将布局按2/5的比例分为2列,然后将第一行的1/9和第二的5/1分成两行。 当我没有将输入元素添加到HTML代码中时,这就是我所拥有的:

https://codepen.io/thenr/pen/PoooyEN

#chat {
  display: flex;
  width: 100%;
  height: calc(100vh - 2em);
  overflow-y: hidden;
}

#friends {
  flex-grow: 2;
  display: flex;
  flex-direction: column;
  border: 1px solid black;
}

#messages {
  flex-grow: 5;
  display: flex;
  flex-direction: column;
  border: 1px solid black;
}

#searchfriend {
  flex-grow: 1;
  flex-shrink: 1;
  flex-basis: 120px;
  border: 1px solid black;
  background-color: blue;
}

#searchfriend input[type="text"] {
  margin: 0;
  padding: 1em;
  border: 0;
  border-radius: 0px;
  background-color: #d1d1d1;
}

#friendslist {
  flex-grow: 9;
  border: 1px solid black;
  overflow-y: auto;
}

.friend {
  height: 100px;
  border: 1px solid black;
}

#chatmessages {
  flex-grow: 5;
  border: 1px solid black;
}

#writemessage {
  flex-grow: 1;
  border: 1px solid black;
}
<div id="chat">
  <!-- friends -->
  <div id="friends">
    <div id="searchfriend">

    </div>
    <div id="friendslist">
      <div class="friend"></div>
      <div class="friend"></div>
      <div class="friend"></div>
      <div class="friend"></div>
      <div class="friend"></div>
      <div class="friend"></div>
      <div class="friend"></div>
      <div class="friend"></div>
      <div class="friend"></div>
      <div class="friend"></div>
      <div class="friend"></div>

    </div>
  </div>

  <!-- chat -->
  <div id="messages">
    <div id="chatmessages">



    </div>
    <div id="writemessage">

    </div>
  </div>
</div>

问题是,当我将输入元素添加到HTML代码中时,我设置的比率没有得到遵守,并且包含这些输入元素的flex项目变大了,我不希望这种情况发生。相同的HTML代码,但添加了输入元素:

https://codepen.io/thenr/pen/RwwweQN

#chat {
  display: flex;
  width: 100%;
  height: calc(100vh - 2em);
  overflow-y: hidden;
}

#friends {
  flex-grow: 2;
  display: flex;
  flex-direction: column;
  border: 1px solid black;
}

#messages {
  flex-grow: 5;
  display: flex;
  flex-direction: column;
  border: 1px solid black;
}

#searchfriend {
  flex-grow: 1;
  flex-shrink: 1;
  flex-basis: 120px;
  border: 1px solid black;
  background-color: blue;
}

#searchfriend input[type="text"] {
  margin: 0;
  padding: 1em;
  border: 0;
  border-radius: 0px;
  background-color: #d1d1d1;
}

#friendslist {
  flex-grow: 9;
  border: 1px solid black;
  overflow-y: auto;
}

.friend {
  height: 100px;
  border: 1px solid black;
}

#chatmessages {
  flex-grow: 5;
  border: 1px solid black;
}

#writemessage {
  flex-grow: 1;
  border: 1px solid black;
}
<div id="chat">
  <!-- friends -->
  <div id="friends">
    <div id="searchfriend">
      <input type="text" name="" id="searchtext" placeholder="Search friend...">

    </div>
    <div id="friendslist">
      <div class="friend"></div>
      <div class="friend"></div>
      <div class="friend"></div>
      <div class="friend"></div>
      <div class="friend"></div>
      <div class="friend"></div>
      <div class="friend"></div>
      <div class="friend"></div>
      <div class="friend"></div>
      <div class="friend"></div>
      <div class="friend"></div>

    </div>
  </div>

  <!-- chat -->
  <div id="messages">
    <div id="chatmessages">



    </div>
    <div id="writemessage">
      <form action="" id="formchat">
        <input type="text" name="msgtext" id="msgtext" autocomplete="off">
        <button id="msgsend">Send</button>
      </form>
    </div>
  </div>
</div>

1 个答案:

答案 0 :(得分:0)

您可以使用flex简写 <template v-slot:body="props"> <tbody> <tr v-for="item in props.items"> <td class="d-block d-sm-table-cell" v-for="field in Object.keys(item)"> {{item[field]}} </td> </tr> </tbody> </template> / flex:2代替flex: 5 / flex-grow:2

这将避免也必须设置flex-grow:5flex-shrink