【问题标题】:How to keep dynamically flowing CSS Multi Columns to a fixed width only using CSS?如何仅使用 CSS 将动态流动的 CSS 多列保持为固定宽度?
【发布时间】:2020-07-17 17:23:41
【问题描述】:

我有一个带边的主容器。旁边有一个固定的宽度,我正在使用 CSS Grid 来绘制这个容器。在主容器内,有一系列包含动态数据的列,这些列会根据需要自动流动、对齐以及将数据从一列转移到另一列。此列设置使用 CSS 多列,但列的宽度会根据用户浏览器的屏幕大小而改变。我想找到一个 CSS 解决方案,将这些列保持在设定的宽度。有没有办法自动扩展最后绘制的列?或者有没有办法阻止动态多列计数扩展?我试过align-items:start,但没有看到效果。有没有办法通过容器将列保持在设定的宽度?

@charset "UTF-8";
/* https://stackoverflow.com/questions/7785374/how-to-prevent-column-break-within-an-element */
html, body {
  padding: 0;
  margin: 0;
}

section {
  padding: 3px;
}

input {
  width: 100%;
  box-sizing: border-box;
}

label {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: block;
  font-family: "Miriam Libre";
  font-size: 10px;
  color: #213C3A;
}

.Cols {
  margin: 0;
  padding: 0;
  columns: 40ch;
  column-count: auto;
  column-fill: auto;
  column-rule: 1px dotted #3DB199;
  height: 100vh;
  /* ↓ columns defined by width */
  overflow: auto;
}

li {
  width: 100%;
  display: inline-block;
  padding: 3px;
  border-bottom: 1px solid #D36B66;
}

.G {
  display: grid;
}
.G-wAside {
  grid-template-columns: 1fr minmax(0, 10vw) 10vw;
  gap: 1vw;
}
.G-23 {
  grid-template-columns: 66.66% 33.33%;
}

.HVpZ {
  height: 100%;
}

.WVpZ {
  width: 100%;
}

.filler {
  background: #CCE0E2;
}

.fieldset {
  margin: 5px 3px;
}
.fieldset > * {
  margin: 1px 0;
}
.fieldset label {
  margin-right: 3px;
}

.placeholder-uploader {
  height: 320px;
  width: 100%;
  background: #FAF9E9;
}
.placeholder-grid {
  width: 640px;
  height: 230px;
  background: #FAF9E9;
  position: absolute;
}

.col-container {
  overflow: hidden;
  position: relative;
  height: 100px;
  grid-row-start: span 2;
}
<main class="G HVpZ WVpZ G-wAside">
  <section>
    <ul class="Cols">
      <li>
        <div class="fieldset">
          <label>New List Is Just Long</label>
          <input type="text"></input>
        </div>
        <div class="fieldset">
          <label>Label Is Just Long</label>
          <input type="text"></input>
        </div>
        <div class="fieldset">
          <label>Label Is Just Long</label>
          <input type="text"></input>
        </div>
        <div class="fieldset">
          <label>Label Is Just Long</label>
          <input type="text"></input>
        </div>
        <div class="col-container">
          <div class="placeholder-grid"></div>
        </div>

        <div class="G G-23">
          <div class="fieldset">
            <label>Grid Label DIV Name</label>
            <input type="text"></input>
          </div>
          <div class="fieldset">
            <label>Label Is Just Longest Of All</label>
            <input type="text"></input>
          </div>
        </div>
        <div class="fieldset">
          <label>Label Is Just Long</label>
          <input type="text"></input>
        </div>
      </li>
      <li>
        <div class="col-container">
          <div class="placeholder-grid"></div>
        </div>
      </li>
      <li>
        <p>Praesent vestibulum semper erat ut varius. Donec sed erat ac odio ultricies bibendum et sit amet lectus. In placerat consequat mi, sed commodo mauris ornare non. Integer nibh leo, dignissim in purus at, pretium porta nibh. Nulla vestibulum imperdiet
          sollicitudin. Morbi euismod justo metus, quis imperdiet nunc eleifend ultrices. Donec nec maximus lacus. Aenean tincidunt mattis nisi, eget molestie dolor iaculis vel. Praesent faucibus risus vel arcu tincidunt, id varius orci bibendum. Duis
          posuere est in diam luctus ultrices id in sapien. Fusce bibendum pharetra velit, sit amet commodo diam pharetra ac.</p>
      </li>
    </ul>
  </section>
  <section class="filler">&larr; How to auto-fill this space to keep css multi columns in previous grid item to set width of 40ch? &rarr;</section>
  <section class="bg">Aside</section>
</main>

【问题讨论】:

  • 我无法理解您的问题。我有一个想法,但不确定我的理解是否正确。因此,您有一个带有输入字段的部分,这些字段会根据视口的大小改变大小。您希望此部分具有 固定 宽度。此外,您希望aside 部分填补空白。对吗?

标签: css css-grid fixed-width css-multicolumn-layout


【解决方案1】:

我不知道这是否能满足您的需求,也许它可以与之前的答案结合起来。

既然输入列的大小是固定的,为什么不为此部分使用表格布局呢?可能需要对您的设计的响应性进行一些其他 CSS 更改。

@charset "UTF-8";

/* https://stackoverflow.com/questions/7785374/how-to-prevent-column-break-within-an-element */

html,
body {
  padding: 0;
  margin: 0;
}

section {
  padding: 3px;
}

input {
  width: 100%;
  box-sizing: border-box;
}

label {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: block;
  font-family: "Miriam Libre";
  font-size: 10px;
  color: #213C3A;
}

.Cols {
  margin: 0;
  padding: 0;
  columns: 40ch;
  column-count: auto;
  column-fill: auto;
  column-rule: 1px dotted #3DB199;
  height: 100vh;
  /* ↓ columns defined by width */
  overflow: scroll;
}

li {
  width: 100%;
  display: inline-block;
  padding: 3px;
  border-bottom: 1px solid #D36B66;
}

.G {
  display: grid;
}

.G-wAside {
  grid-template-columns: 1fr minmax(0, 10vw) 10vw;
  gap: 1vw;
}

.G-23 {
  grid-template-columns: 66.66% 33.33%;
}

.HVpZ {
  height: 100%;
}

.WVpZ {
  width: 100%;
}

.filler {
  background: #CCE0E2;
}

.fieldset {
  margin: 5px 3px;
}

.fieldset>* {
  margin: 1px 0;
}

.fieldset label {
  margin-right: 3px;
}

.placeholder-uploader {
  height: 320px;
  width: 100%;
  background: #FAF9E9;
}

.placeholder-grid {
  width: 640px;
  height: 230px;
  background: #FAF9E9;
  position: absolute;
}

.col-container {
  overflow: hidden;
  position: relative;
  height: 100px;
  grid-row-start: span 2;
}

#tbl {
  table-layout: fixed;
}

#tbl td {
  min-width: 25em;
  max-width: 25em;
}

#tbl2 {
  table-layout: fixed;
}

#tbl2 td {
  min-width: 25em;
}

ul {
  list-style-type: none;
  cursor: pointer;
}

li {
  display: inline-block;
}


/* default is vertical     display */
<main class="G HVpZ WVpZ G-wAside">
  <section>
    <ul class="Cols">
      <li>
        <table id="tbl" border="1">
          <tr>
            <td>
              <div class="fieldset">
                <label>New List Is Just Long</label>
                <input type="text"></input>
              </div>
            </td>
          </tr>
          <tr>
            <td>
              <div class="fieldset">
                <label>Label Is Just Long</label>
                <input type="text"></input>
              </div>
            </td>
          </tr>
          <tr>
            <td>
              <div class="fieldset">
                <label>Label Is Just Long</label>
                <input type="text"></input>
              </div>
            </td>
          </tr>
          <tr>
            <td>
              <div class="fieldset">
                <label>Label Is Just Long</label>
                <input type="text"></input>
              </div>
            </td>
          </tr>
          <tr>
            <td>
              <div class="col-container">
                <div class="placeholder-grid"></div>
              </div>
            </td>
          </tr>
          <tr>
            <td>
              <div class="G G-23">
                <div class="fieldset">
                  <label>Grid Label DIV Name</label>
                  <input type="text"></input>
                </div>
                <div class="fieldset">
                  <label>Label Is Just Longest Of All</label>
                  <input type="text"></input>
                </div>
              </div>
            </td>
          </tr>
          <tr>
            <td>
              <div class="fieldset">
                <label>Label Is Just Long</label>
                <input type="text"></input>
              </div>
            </td>
          </tr>
        </table>
      </li>
      <!--
   <li>
    <div class="col-container">
     <div class="placeholder-grid"></div>
    </div>
   </li>
// -->
      <li>

        <table id="tbl2" border="1">
          <tr>
            <td>
              <p>Praesent vestibulum semper erat ut varius. Donec sed erat ac odio ultricies bibendum et sit amet lectus. In placerat consequat mi, sed commodo mauris ornare non. Integer nibh leo, dignissim in purus at, pretium porta nibh. Nulla vestibulum
                imperdiet sollicitudin. Morbi euismod justo metus, quis imperdiet nunc eleifend ultrices. Donec nec maximus lacus. Aenean tincidunt mattis nisi, eget molestie dolor iaculis vel. Praesent faucibus risus vel arcu tincidunt, id varius orci
                bibendum. Duis posuere est in diam luctus ultrices id in sapien. Fusce bibendum pharetra velit, sit amet commodo diam pharetra ac.</p>
            </td>
          </tr>
        </table>

      </li>
    </ul>
  </section>
  <section class="filler">
    &larr; How to auto-fill this space to keep css multi columns in previous grid item to set width of 40ch? &rarr;
  </section>
  <section class="bg">Aside</section>
</main>

【讨论】:

    【解决方案2】:

    所以如果我理解正确,你有两个问题:

    1. 我想找到一个 CSS 解决方案,让这些列保持固定宽度。

    2. 有没有办法自动展开最后绘制的列?或者有没有办法阻止动态多列计数扩展?


    回答#1、#2:

    如果您希望网格中的列具有固定大小,请为您的 grid-template-columns 属性使用绝对单位。

    而不是这个:

    grid-template-columns: 1fr minmax(0, 10vw) 10vw;
    

    使用这个:

                              /*Sets the minimal 
                                width to 0 and 
                                fills in the rest 
                                of the available
                                space.
                                  ↓              */
    grid-template-columns: 40ch minmax(0, 1fr);
                    /*      ↑
                       Sets a fixed 
                       width to your 
                       first column */
    

    这样你的第一列的宽度正好是40ch,第二列(aside)填满了其余的空间,这基本上也回答了你的问题#2。

    最后是这样的:

    @charset "UTF-8";
    
    /* https://stackoverflow.com/questions/7785374/how-to-prevent-column-break-within-an-element */
    
    html,
    body {
      padding: 0;
      margin: 0;
    }
    
    input {
      width: 100%;
      box-sizing: border-box;
    }
    
    label {
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      display: block;
      font-family: "Miriam Libre";
      font-size: 10px;
      color: #213C3A;
    }
    
    .Cols {
      margin: 0;
      padding: 0;
      columns: 40ch;
      column-count: auto;
      column-fill: auto;
      column-rule: 1px dotted #3DB199;
      height: 100vH;
      /* ↓ columns defined by width */
      overflow: auto;
    }
    
    li {
      width: 100%;
      display: inline-block;
      padding: 3px;
      border-bottom: 1px solid #D36B66;
    }
    
    .G {
      display: grid;
    }
    
    .G-wAside {
      grid-template-columns: 40ch minmax(0, 1fr);
      gap: 1vw;
    }
    
    .G-23 {
      grid-template-columns: 66.66% 33.33%;
    }
    
    .HVpZ {
      height: 100%;
    }
    
    .WVpZ {
      width: 100%;
    }
    
    .filler {
      background: #CCE0E2;
    }
    
    .fieldset {
      margin: 5px 3px;
    }
    
    .fieldset>* {
      margin: 1px 0;
    }
    
    .fieldset label {
      margin-right: 3px;
    }
    
    .placeholder-uploader {
      height: 320px;
      width: 100%;
      background: #FAF9E9;
    }
    
    .placeholder-grid {
      width: 640px;
      height: 230px;
      background: #FAF9E9;
      position: absolute;
    }
    
    .col-container {
      overflow: hidden;
      position: relative;
      height: 100px;
      grid-row-start: span 2;
    }
    <main class="G HVpZ WVpZ G-wAside">
      <section>
        <ul class="Cols">
          <li>
            <div class="fieldset">
              <label>New List Is Just Long</label>
              <input type="text">
            </div>
            <div class="fieldset">
              <label>Label Is Just Long</label>
              <input type="text">
            </div>
            <div class="fieldset">
              <label>Label Is Just Long</label>
              <input type="text">
            </div>
            <div class="fieldset">
              <label>Label Is Just Long</label>
              <input type="text">
            </div>
            <div class="col-container">
              <div class="placeholder-grid"></div>
            </div>
    
            <div class="G G-23">
              <div class="fieldset">
                <label>Grid Label DIV Name</label>
                <input type="text">
              </div>
              <div class="fieldset">
                <label>Label Is Just Longest Of All</label>
                <input type="text">
              </div>
            </div>
            <div class="fieldset">
              <label>Label Is Just Long</label>
              <input type="text">
            </div>
          </li>
          <li>
            <div class="col-container">
              <div class="placeholder-grid"></div>
            </div>
          </li>
          <li>
            <p>Praesent vestibulum semper erat ut varius. Donec sed erat ac odio ultricies bibendum et sit amet lectus. In placerat consequat mi, sed commodo mauris ornare non. Integer nibh leo, dignissim in purus at, pretium porta nibh. Nulla vestibulum imperdiet
              sollicitudin. Morbi euismod justo metus, quis imperdiet nunc eleifend ultrices. Donec nec maximus lacus. Aenean tincidunt mattis nisi, eget molestie dolor iaculis vel. Praesent faucibus risus vel arcu tincidunt, id varius orci bibendum. Duis
              posuere est in diam luctus ultrices id in sapien. Fusce bibendum pharetra velit, sit amet commodo diam pharetra ac.</p>
          </li>
        </ul>
      </section>
      <section class="filler">&larr; How to auto-fill this space to keep css multi columns in previous grid item to set width of 40ch? &rarr;</section>
    </main>

    PS:

    在您的代码中,您将关闭 &lt;input&gt; 标记。这不是必需的,并且是语法错误。因此,我在示例代码中对其进行了修复。


    编辑#1

    嗨@Aaron3219。感谢您的回答。我的问题是:“有没有办法阻止动态多列计数扩展?”主要问题是在.Cols {} 中,我有一个设置宽度columns: 40ch 和column-count: auto。我想确保当这些列显示时,无论是 1 列还是 10 列,严格来说,它们具有 40ch 的设置宽度,当视口宽度发生变化时不会响应。

    就我而言,没有。 column-width 属性(您在 columns 中使用的简写)是针对浏览器的最小列宽而不是绝对宽度的建议。

    或者,正如w3schools 所说:

    column-width 属性指定列宽。

    列数将是显示元素中所有内容所需的最小列数。

    column-width 是一个灵活的属性。将column-width 视为浏览器的最小宽度建议。一旦浏览器不能以您指定的宽度容纳至少两列,那么这些列将停止并放入一列中。

    当然,有一些方法可以实现您想要的结构,例如使用 flexbox 或网格。我确信使用columns 是一种不适合您打算做的方法。

    【讨论】:

    • 嗨@Aaron3219。感谢您的回答。我的问题是:“有没有办法阻止动态多列计数扩展?”主要问题是在.Cols {} 中,我有一个设置宽度columns: 40ch 和column-count: auto。我想确保当这些列显示时,无论是 1 列还是 10 列,严格来说,它们具有 40ch 的设置宽度,当视口宽度发生变化时不会响应。
    猜你喜欢
    • 2015-01-03
    • 1970-01-01
    • 2013-02-15
    • 1970-01-01
    • 2013-06-08
    • 2012-04-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多