Flexbox具有一个固定的列宽

RGr*_*ths 22 css flexbox

我试图flexbox用两列来实现,左边是固定宽度,右边是随页面大小改变而缩放的.例如:

<style>
    .flex_container {
        display: flex;
        width: 100%;
    }

    .flex_col_left {
        width: 200px;
    }
    .flex_col_right {
        width: 85%;
    }
</style>

<div class = "flex_container">
    <div class = "flex_col_left">
        .....
    </div>
    <div class = "flex_col_right">
        .....
    </div>
<div>
Run Code Online (Sandbox Code Playgroud)

这在某种程度上起作用,但是当我混合px和%时它感觉不对.这样做是不正确的,如果是这样,什么可能是更好的解决方案?

编辑类命名问题是一个错字,现在已修复.

Nen*_*car 36

%您可以直接使用flex: 1,而不是在右栏中设置宽度, 它将连续占用剩余的自由宽度.

.flex_container {
  display: flex;
  width: 100%;
}
.flex_item_left {
  width: 200px;
  background: lightgreen;
}
.flex_item_right {
  flex: 1;
  background: lightblue;
}
Run Code Online (Sandbox Code Playgroud)
<div class="flex_container">
  <div class="flex_item_left">
    .....
  </div>
  <div class="flex_item_right">
    .....
  </div>
  <div>
Run Code Online (Sandbox Code Playgroud)


Joh*_*nes 5

flex-grow: 1您可以在非固定宽度元素上使用以允许其增长(并且没有宽度设置)。

.flex_container {
  display: flex;
  width: 100%;
}
.flex_item_left {
  width: 200px;
  background: #fa0;
}
.flex_item_right {
  background: #0fa;
  flex-grow: 1;
}
Run Code Online (Sandbox Code Playgroud)
<div class="flex_container">
  <div class="flex_item_left">
    .....
  </div>
  <div class="flex_item_right">
    .....
  </div>
  <div>
Run Code Online (Sandbox Code Playgroud)