是否可以根据内容设置比例网格列?

时间:2018-02-08 03:18:58

标签: css css3 layout flexbox css-grid

使用Flexbox或CSS Grid,是否可以根据其中一列的内容调整列的大小?

这是我想要的:

我有2列,

|------------------------container--------------------------|
|                                                           |
||----------------------------------------|----------------||
||               main                     |      side      ||
||----------------------------------------|----------------||
|                                                           |
|-----------------------------------------------------------|

我希望根据其内容自动调整大小,我希望的宽度是的两倍, 最低限度。它可以变大,但不会变小。

随着容器的增加, main 将以相同的速度增长,而仍然是其自动宽度。

|--------------------------container increases----------------------|
|                                                                   |
||------------------------------------------------|----------------||
||                   main increases               |      side      ||
||------------------------------------------------|----------------||
|                                                                   |
|-------------------------------------------------------------------|

容器可以缩小到的3倍,此时 main 将是的两倍宽>。我希望这是断点。

|--------------------container----------------------|
|                                                   |
||--------------------------------|----------------||
||               main             |      side      ||
||--------------------------------|----------------||
|                                                   |
|---------------------------------------------------|

如果容器再缩小,我希望列堆叠,其中两列现在都是100%宽度:

|--------------------container--------------------|
|                                                 |
||-----------------------------------------------||
||               main                            ||
||-----------------------------------------------||
||-----------------------------------------------||
||                           side                ||
||-----------------------------------------------||
|                                                 |
|-------------------------------------------------|

这是我试过的:

我尝试使用Flexbox和Grid执行此操作,但我无法根据侧栏确定主列的大小。

.container-grid {
  display: grid;
  grid-template-columns: 1fr auto; /* works, but doesn’t wrap when left column gets too small */
  /* I also tried the repeat() syntax, but that only worked for similarly-sized columns */
}
.container-flex {
  display: flex;
  flex-wrap: wrap;
}
.main { flex: 2; } .side { flex: 1; } /* same problem, doesn’t wrap */
.main ( flex: 0 1 auto; } .side { flex: 0 0 auto; } /* wraps columns too early, any time main is smaller than intrinsic width */

我尝试过的唯一解决方案是知道侧栏的固定宽度(我必须在不知道其内容的情况下进行预测),然后使用宽度为3倍的媒体查询。当然,媒体查询仅适用于窗口宽度,而不适用于容器宽度,因此我必须使用容器的任何父元素来计算媒体查询。

.container-grid {
  display: grid;
  grid-template-columns: 1fr 15em;
}
@media screen and (max-width: 45em) {
  .container-grid {
    grid-template-columns: 1fr;
  }
}

1 个答案:

答案 0 :(得分:2)

以下是使用Flexbox的一种解决方案,其中main上的 flex-grow 值比side上的值大得多。

有了这个,side将在包装时填充父级,而在宽屏幕上,main上的值更高, flex-growth side几乎没有。

min-widthmain的{​​{1}}是side的两倍,它会在它变小之前换行,为此我们可以使用百分比,因为它基于父母的宽度。

因此,在这种情况下,main的两倍大小将是父母的2/3,即66.666%。

Fiddle demo

Stack snippet

.container-flex {
  display: flex;
  flex-wrap: wrap;
}

.main {
  flex: 10000;
  min-width: 66.666%;
  background: lightblue;
}

.side {
  flex: 1 0 auto;
  background: lightgreen;
}

span {
  display: inline-block;
  padding: 20px;
}
<div class="container-flex">

  <div class="main">
    <span>Fill remain, min. twice the "side"</span>
  </div>

  <div class="side">
    <span>Take content size</span>
  </div>

</div>

相关问题