Flex问题:排序元素 + 2列

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

Flex problem : order elements + 2 columns

问题

Thanks to flexbox, based on this code, I would like col-1, col-3, and col-5 to be placed below each other in a column on the right, and col-2 and col-4 to be placed below each other in a left column.

这段代码使用flexbox,我希望col-1、col-3和col-5在右侧以列的形式依次排列,而col-2和col-4在左侧依次排列。

Is it possible?

是否可能?

The desired result: texts on the left and videos on the right.
我期望的结果是:文本在左侧,视频在右侧。

I tried this CSS but the videos stay below the texts. Columns do not work.

我尝试了这段CSS代码,但视频仍然位于文本下方。列没有按照预期工作。

英文:

Thanks to flexbox, based on this code, I would like col-1, col-3 and col-5 to be placed below each other in a column on the right and col-2 and col-4 to be placed below each other. below the others in a left column.

<div class="row">
	<div class="col-1">TEXTE 1</div>
	<div class="col-2">VIDEO 1</div>
	<div class="col-3">TEXTE 2</div>
	<div class="col-4">VIDEO 2</div>
	<div class="col-5">TEXTE 3</div>
</div>

Is it possible ?

The desired result: texts on the left and videos on the right.
I tried this CSS but the videos stay below the texts. Columns do not work.

section.cta-video .row{
        display: flex;
        flex-direction: column;
        justify-content: space-between;
    }

    section.cta-video .row .col-1, section.cta-video .row .col-3, section.cta-video .row .col-5 {
        order: 1;
        flex-basis: 50%;
    }

    section.cta-video .row .col-2, section.cta-video .row .col-4 {
        order: 2;
        flex-basis: 50%;
    }

答案1

得分: 3

以下是翻译好的内容:

"Is it necessary that it's a flex layout?" 可以使用 flex 布局吗?

"You can achieve this easily using grid." 您可以轻松使用 grid 实现这一点。

"grid-template-columns: 1fr 1fr turns it into a 2-column layout." grid-template-columns: 1fr 1fr 可以将其变成两列布局。

"You can then use the nth-child pseudo selectors to select all even-numbered children to start in the first grid column, and all the odd-numbered children to start in the second grid column:" 您可以使用 nth-child 伪选择器选择所有偶数序号的子元素放在第一列,所有奇数序号的子元素放在第二列:

.row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-auto-flow: dense; /* 返回到正常布局 */
}

.row div:nth-child(even) {
  grid-column-start: 1;
}

.row div:nth-child(odd) {
  grid-column-start: 2;
}
<div class="row">
  <div class="col-1">TEXTE 1</div>
  <div class="col-2">VIDEO 1</div>
  <div class="col-3">TEXTE 2</div>
  <div class="col-4">VIDEO 2</div>
  <div class="col-5">TEXTE 3</div>
</div>
英文:

Is it necessary that it's a flex layout?

You can achieve this easily using grid.

grid-template-columns: 1fr 1fr turns it into a 2-column layout.

You can then use the nth-child pseudo selectors to select all even-numbered children to start in the first grid column, and all the odd-numbered children to start in the second grid column:

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

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

.row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-auto-flow: dense; /* returns it to a normal layout */
}

.row div:nth-child(even) {
  grid-column-start: 1;
}

.row div:nth-child(odd) {
  grid-column-start: 2;
}

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

&lt;div class=&quot;row&quot;&gt;
	&lt;div class=&quot;col-1&quot;&gt;TEXTE 1&lt;/div&gt;
	&lt;div class=&quot;col-2&quot;&gt;VIDEO 1&lt;/div&gt;
	&lt;div class=&quot;col-3&quot;&gt;TEXTE 2&lt;/div&gt;
	&lt;div class=&quot;col-4&quot;&gt;VIDEO 2&lt;/div&gt;
	&lt;div class=&quot;col-5&quot;&gt;TEXTE 3&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 1

你可以使用网格布局,特别是 grid-template-areas 来解决这个问题:

关于 grid-template-areas 的更多信息,请查看 CSS tricks 这里

.row {
  display: grid;
  grid-template-areas: "a b" "c d" "e f";
}

.col-1 { grid-area:b; }
.col-3 { grid-area:d; }
.col-5 { grid-area:f; }
<div class="row">
  <div class="col-1">TEXTE 1</div>
  <div class="col-2">VIDEO 1</div>
  <div class="col-3">TEXTE 2</div>
  <div class="col-4">VIDEO 2</div>
  <div class="col-5">TEXTE 3</div>
</div>
英文:

You can use grid, in particular grid-template-areas to solve this:

More info on grid-template-areas on CSS tricks here

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

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

.row {
  display: grid;
  grid-template-areas: &quot;a b&quot; &quot;c d&quot; &quot;e f&quot;;
}

.col-1 { grid-area:b; }
.col-3 { grid-area:d; }
.col-5 { grid-area:f; }

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

&lt;div class=&quot;row&quot;&gt;
	&lt;div class=&quot;col-1&quot;&gt;TEXTE 1&lt;/div&gt;
	&lt;div class=&quot;col-2&quot;&gt;VIDEO 1&lt;/div&gt;
	&lt;div class=&quot;col-3&quot;&gt;TEXTE 2&lt;/div&gt;
	&lt;div class=&quot;col-4&quot;&gt;VIDEO 2&lt;/div&gt;
	&lt;div class=&quot;col-5&quot;&gt;TEXTE 3&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月12日 16:12:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76454698.html
匿名

发表评论

匿名网友

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

确定