将某一列从一行移动到下一行。

huangapple go评论69阅读模式
英文:

Flexbox - Move a certain column from a row to the next row

问题

    <div class="rowblock">
        <div class="col">10% 宽度</div>
        <div class="col">80% 宽度</div>
    </div>
    <div class="rowblock">
        <div class="col">100% 下一行</div>
    </div>
    <div class="rowblock">
        <div class="col">10%</div>
    </div>
英文:

I am trying to create the following structure:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.rowblock {
 display: flex;
 padding: 0.5rem;
 width: 100%;
 flex-direction: row;
}
.col {
 height: 50px;
 border: 1px solid #ccc;
}

<!-- language: lang-html -->

&lt;div class=&quot;rowblock&quot;&gt;
 &lt;div class=&quot;col&quot;&gt;10% Width&lt;/div&gt;
 &lt;div class=&quot;col&quot;&gt;80% Width&lt;/div&gt;
 &lt;div class=&quot;col&quot;&gt;100% on Next Row&lt;/div&gt;
 &lt;div class=&quot;col&quot;&gt;10%&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

I am trying to move the 3rd column to the next row to occupy the full width. I tried using flex grow but it did not work. Please suggest an approach.

答案1

得分: 2

以下是已翻译的内容:

在修改后的代码中,我添加了 flex-wrap: wrap; 属性到 .rowblock 类,以允许列在必要时换行。

对于列,我使用了 flex-basis 属性来指定初始宽度为百分比。第一列宽度为 10%,第二列宽度为 80%,第三列宽度为 100%(将其强制移到下一行),第四列宽度为 10%。

通过这些修改,您应该能够实现所需的结构和指定的列宽度。

**更新:**

我使用了 `order` 属性并在 `flex-basis` 属性中减去了 2px,以弥补列边框添加的像素。

请让我知道如果您需要任何其他的帮助。

英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.rowblock {
  display: flex;
  padding: 0.5rem;
  width: 100%;
  flex-wrap: wrap;
}

.col {
  height: 50px;
  border: 1px solid #ccc;
  flex-grow: 1;
}

.col:nth-child(1) {
  order: 1;
  flex-basis: calc(10% - 2px);
}

.col:nth-child(2) {
  order: 2;
  flex-basis: calc(80% - 2px);
}

.col:nth-child(3) {
  order: 4;
  flex-basis: calc(100% - 2px);
}

.col:nth-child(4) {
  order: 3;
  flex-basis: calc(10% - 2px);
}

<!-- language: lang-html -->

&lt;div class=&quot;rowblock&quot;&gt;
 &lt;div class=&quot;col&quot;&gt;10% Width&lt;/div&gt;
 &lt;div class=&quot;col&quot;&gt;80% Width&lt;/div&gt;
 &lt;div class=&quot;col&quot;&gt;100% on Next Row&lt;/div&gt;
 &lt;div class=&quot;col&quot;&gt;10%&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

In the modified code, I added the flex-wrap: wrap; property to the .rowblock class to allow the columns to wrap onto the next line when necessary.

For the columns, I used the flex-basis property to specify the initial width as a percentage. The first column has a width of 10%, the second column has 80%, the third column has 100% (which forces it onto the next row), and the fourth column has 10%.

With these modifications, you should achieve the desired structure with the specified column widths.

UPDATED:

I've updated the code with the order property as well as subtracting 2px in the flex-basis property to make up for the px's added by the col border.

答案2

得分: 1

请看以下翻译:

尝试这个:

&lt;!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false --&gt;

&lt;!-- 语言: lang-css --&gt;

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

    .col {
      padding: 10px;
      box-sizing: border-box;
      border: 1px solid black;
      flex-grow: 1;
    }

    .col:nth-child(1) {
      order: 1;
    }

    .col:nth-child(2) {
      order: 2;
    }

    .col:nth-child(3) {
      flex-basis: 100%;
      width: 100%;
      order: 4;
    }

    .col:nth-child(4) {
      flex-basis: 10%;
      order: 3;
    }

&lt;!-- 语言: lang-html --&gt;

    &lt;div class=&quot;rowblock&quot;&gt;
      &lt;div class=&quot;col&quot;&gt;10% 宽度&lt;/div&gt;
      &lt;div class=&quot;col&quot;&gt;80% 宽度&lt;/div&gt;
      &lt;div class=&quot;col&quot;&gt;100% 下一行&lt;/div&gt;
      &lt;div class=&quot;col&quot;&gt;10%&lt;/div&gt;
    &lt;/div&gt;

&lt;!-- 结束代码片段 --&gt;

它启用了 flex 包裹功能,使用 `order` 属性将第三列移到行的末尾,并将第三列的宽度设置为 100%。

* 这些元素只在布局中移动;它们在 DOM 中的顺序不变。
英文:

Try this:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

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

.col {
  padding: 10px;
  box-sizing: border-box;
  border: 1px solid black;
  flex-grow: 1;
}

.col:nth-child(1) {
  order: 1;
}

.col:nth-child(2) {
  order: 2;
}

.col:nth-child(3) {
  flex-basis: 100%;
  width: 100%;
  order: 4;
}

.col:nth-child(4) {
  flex-basis: 10%;
  order: 3;
}

<!-- language: lang-html -->

&lt;div class=&quot;rowblock&quot;&gt;
 &lt;div class=&quot;col&quot;&gt;10% Width&lt;/div&gt;
 &lt;div class=&quot;col&quot;&gt;80% Width&lt;/div&gt;
 &lt;div class=&quot;col&quot;&gt;100% on Next Row&lt;/div&gt;
 &lt;div class=&quot;col&quot;&gt;10%&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

It enables flex wrapping, moves the third column to the end of the row using the order property*, and gives the third column a width of 100%.

* The elements are only moved in layout; their order in the DOM is unchanged.

huangapple
  • 本文由 发表于 2023年5月18日 02:07:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76275034.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定