CSS: select last ordered flex children

最后都变了- 提交于 2020-01-01 08:57:10

问题


Given an arbitary number of flexbox children with an inline order attribute used for sorting, how would one select the last ordered element in CSS?

<ul style="display:flex">
  <li style="order: 4">a</li>
  <li style="order: 5">b</li>
  <li style="order: 3">c</li>
  <li style="order: 1">d</li>
  <li style="order: 2">e</li>
</ul>

:last-of-type doesn't work on flex children, and the list can be any length, so selecting by order index isn't an option. I'm not interested in JavaScript solutions.


回答1:


The only close solution to this is may be

div[style="order: 6"] {
    background-color: yellow;
}

And this will color the background with yellow for the 6th order, but since we don't know the length of the array, it seems like you'll need some javascript or jQuery with similar selector sintaxis where the number 6 in this case will be a variable with the array's length.




回答2:


Your code isnt complete. You have to provide a flex wrapper to your divs (display: flex), so that =order: will validate. Therefore, .wrapper div:last-child should do the trick.



来源:https://stackoverflow.com/questions/29709793/css-select-last-ordered-flex-children

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!