Is it possible to change the order of list items using CSS3?

后端 未结 3 1886
遥遥无期
遥遥无期 2020-12-15 22:23

Is it possible to change the order of list items using CSS3?

For example, if a list is coded in HTML in 1,2,3,4,5 order, but I want it to show in 5,1,2,3,4 order.

相关标签:
3条回答
  • 2020-12-15 22:42

    You can do it using flexbox.

    Here's a fiddle I created for you: https://jsfiddle.net/x56hayht/

    ul {
      display: flex;
      flex-direction: column;
    }
    ul li:first-child {
      order: 5;
    }
    ul li:nth-child(2) {
      order: 4;
    }
    ul li:nth-child(3) {
      order: 3;
    }
    ul li:nth-child(4) {
      order: 2;
    }
    <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
      <li>4</li>
      <li>5</li>
    </ul>

    According to csstricks:

    The order property is a sub-property of the Flexible Box Layout module.

    Flex items are displayed in the same order as they appear in the source document by default.

    The order property can be used to change this ordering.

    Syntax:

    order: number

    Hope it helps. Cheers!

    0 讨论(0)
  • 2020-12-15 22:50

    If you need just reverse, use:

    ul {
        display: flex;
        flex-direction: column-reverse;
    }
    
    0 讨论(0)
  • 2020-12-15 22:53

    Yes, you can using the flexible box model's order css property. Be aware that the parent element must have display:flex set

    ul {
      display: -webkit-box;
      display: -moz-box;
      display: -ms-flexbox;
      display: -moz-flex;
      display: -webkit-flex;
      display: flex;
      
      -moz-flex-flow: wrap;
      -webkit-flex-flow: wrap;
      flex-flow: wrap;
    }
    ul li {
      width: 100%
    }
    ul li:nth-of-type(1) {
      order: 2;
    }
    ul li:nth-of-type(2) {
      order: 3;
    }
    ul li:nth-of-type(3) {
      order: 4;
    }
    ul li:nth-of-type(4) {
      order: 5;
    }
    ul li:nth-of-type(5) {
      order: 1;
    }
    <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
      <li>4</li>
      <li>5</li>
    </ul>

    Also be aware that not all browsers support the flexible box model as outlined here...http://caniuse.com/#feat=flexbox

    However, there are quite a few polyfills out there you can use to bring support for older browsers if you need to support older browsers

    0 讨论(0)
提交回复
热议问题