Loading…
Everything will be ready in few seconds
- Theory
- Theory
- Comments
Ordering elements with CSS
We can achieve an interesting effect by combining a flexbox and a trick with the :checked ~
selector.
The method works as follows: you can use the checkbox selector to control the order of flex items by changing the direction of the main axis using flex-direction
. The effect works best when the direction of the main axis changes from “top to bottom” to “bottom to top”.
In this case, the flex container should be on the same level in the markup as the checkbox.
Thus, we can achieve CSS sorting without having to use JavaScript.
- index.html
- style.css
Thanks! We’ll fix everything at once!
The code has changed, click “Refresh” or turn autorun on.
You’ve gone to a different page
Click inside the mini-browser to shift the focus onto this window.
- Assign a flexbox layout to the
.sort-list
block, - and set the direction of flow of the main axis to from top to bottom.
- Set the direction of the flow of the main axis to from bottom to top when the checkbox state
.input-sort
is selected for the.sort-list
block.
Toggle the checkbox to sort the list items.
Comments