Flexbox:具有inline-flex +固定项目宽度的行(修复flex-basis错误):在列布局中不起作用

时间:2018-08-24 08:27:18

标签: css flexbox

我想在CSS中获得一个表,在Y上带有一个粘性标头(仅正文滚动),而整个表在X上滚动。

根据Michael_B对我之前的问题here的评论,存在一个flexbox错误,我必须在行上使用width +在单元格上使用html, body { width: 100%; min-height: 100vh; margin: 0; padding: 0; } body { display: flex; align-items: center; justify-content: center; } .table { border: solid black; width: 60vw; height: 80vh; overflow-x: auto; display: flex; flex-direction: column; } .header { border: solid cyan; flex: 0; } .body { flex: 1; overflow-y: auto; border: solid yellow; display: inline-block; } .row { border: solid red; display: inline-flex; flex-direction: row; align-items: center; } .cell { border: solid green; width: 400px; height: 50px; }以确保我的行正确增长。 / p>

这导致我得到以下结果:

<div class="table">
  <div class="header">
    <div class="row">
      <div class="cell">
        cell 1
      </div>
      <div class="cell">
        cell 2
      </div>
      <div class="cell">
        cell 3
      </div>
    </div>
  </div>
  <div class="body">
    <div class="row">
      <div class="cell">
        cell 1
      </div>
      <div class="cell">
        cell 2
      </div>
      <div class="cell">
        cell 3
      </div>
    </div>
    <div class="row">
      <div class="cell">
        cell 1
      </div>
      <div class="cell">
        cell 2
      </div>
      <div class="cell">
        cell 3
      </div>
    </div>
    <div class="row">
      <div class="cell">
        cell 1
      </div>
      <div class="cell">
        cell 2
      </div>
      <div class="cell">
        cell 3
      </div>
    </div>
  </div>
</div>
html, body {
  width: 100%;
  min-height: 100vh;
  margin: 0;
  padding: 0;
}

body {
  display: flex;
  align-items: center;
  justify-content: center;
}

.table {
  border: solid black;
  width: 60vw;
  height: 80vh;
  overflow-x: auto;
}

.header {
  border: solid cyan;
  flex: 0;
  display: inline-block;
  height: 50px;
}

.body {
  flex: 1;
  overflow-y: auto;
  border: solid yellow;
  display: inline-block;
  height: calc(100% - 50px - 16px);
}

.row {
  border: solid red;
  display: inline-flex;
  flex-direction: row;
  align-items: center;
}

.cell {
  border: solid green;
  width: 400px;
  height: 50px;
}



By removing flex column, using inline-block and calc computations, I almost get the behavior I want, except I'd like the scrollY scrollbar to always stay visible

JsFiddle

如您所见,这并不是我想要的。似乎即使使用inline-flex,我的行也不会根据其内容大小增长,并且仅在表上使用flex列布局时才会发生这种情况。

<div class="table">
  <div class="header">
    <div class="row">
      <div class="cell">
        cell 1
      </div>
      <div class="cell">
        cell 2
      </div>
      <div class="cell">
        cell 3
      </div>
    </div>
  </div>
  <div class="body">
    <div class="row">
      <div class="cell">
        cell 1
      </div>
      <div class="cell">
        cell 2
      </div>
      <div class="cell">
        cell 3
      </div>
    </div>
    <div class="row">
      <div class="cell">
        cell 1
      </div>
      <div class="cell">
        cell 2
      </div>
      <div class="cell">
        cell 3
      </div>
    </div>
    <div class="row">
      <div class="cell">
        cell 1
      </div>
      <div class="cell">
        cell 2
      </div>
      <div class="cell">
        cell 3
      </div>
    </div>
  </div>
</div>
<img src="bionic.jpg" alt="festo bionic" class="events">
    <div class="event_description">
        <p> Robotics Demo </p>
        <p> All Day on October 4th, 2018. R:Lab, Emirates Tower</p>
        <p> Join us in interacting with the most technologically advanced robots. </p>
    </div>

JsFiddle

任何人都知道如何实现所需的表格布局吗?我不愿接受任何Flexbox解决方案,但暂时不希望使用基于JS的解决方案。

0 个答案:

没有答案