反转只有特定的Div子元素的顺序。

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

Reverse the order of only certain Div children elements

问题

#parent {
    display: flex;
    flex-direction: column;
}

#sub-child {
    display: flex;
    flex-direction: column-reverse;
}
英文:

How can you reverse the order of only certain div's children elements with pure CSS?

For example:

I want

<div id="parent">
    <a>A</a>
    <a>B</a>
    <a>C</a>
    <a>D</a>
    <div id="sub-child">
        <a>X</a>
        <a>Y</a>
        <a>Z</a>
   </div>
</div>

to be displayed as:

D

C

B

A

X

Y

Z

I tried:

#parent {
    display:flex;
    flex-direction: column-reverse; 
}

However, It is reversing everything inside parent (Not giving desired result)

答案1

得分: 2

  1. #parent#sub-child 上应用 display: flex
  2. #parent 设置 flex-direction: column-reverse,为 #sub-child 设置 flex-direction: column
  3. 对于 #sub-child 使用负数 order,以保持该节点位于底部。
#parent {
  display: flex;
  flex-direction: column-reverse;
}

#sub-child {
  order: -1;
  display: flex;
  flex-direction: column;
}
<div id="parent">
  <a>A</a>
  <a>B</a>
  <a>C</a>
  <a>D</a>
  <div id="sub-child">
    <a>X</a>
    <a>Y</a>
    <a>Z</a>
  </div>
</div>
英文:
  1. apply display: flex on both #parent and #sub-child;
  2. give flex-direction: column-reverse for the #parent and flex-direction: column for #sub-child;
  3. use a negative order for #sub-child so to keep that node at the bottom

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

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

#parent {
 display: flex;
 flex-direction: column-reverse;
}

#sub-child {
  order: -1;
  display: flex;
  flex-direction: column;
}

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

&lt;div id=&quot;parent&quot;&gt;
    &lt;a&gt;A&lt;/a&gt;
    &lt;a&gt;B&lt;/a&gt;
    &lt;a&gt;C&lt;/a&gt;
    &lt;a&gt;D&lt;/a&gt;
    &lt;div id=&quot;sub-child&quot;&gt;
        &lt;a&gt;X&lt;/a&gt;
        &lt;a&gt;Y&lt;/a&gt;
        &lt;a&gt;Z&lt;/a&gt;
   &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月17日 16:06:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76269847.html
匿名

发表评论

匿名网友

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

确定