英文:
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
- 在
#parent
和#sub-child
上应用display: flex
; - 为
#parent
设置flex-direction: column-reverse
,为#sub-child
设置flex-direction: column
; - 对于
#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>
英文:
- apply
display: flex
on both#parent
and#sub-child
; - give
flex-direction: column-reverse
for the#parent
andflex-direction: column
for#sub-child
; - 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 -->
<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>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论