Can I use CSS to reverse the display order of two elements?

后端 未结 4 699
花落未央
花落未央 2021-01-15 08:41

This sounds crazy, but bear with me. I\'m writing a page that basically consists of the following:

相关标签:
4条回答
  • 2021-01-15 09:12

    The following code will sort your issue and according to caniuse.com is compatible with all browsers except for IE7 and below.

    Full support on CSS table display can be found here

    HTML

    <div id="container">
        <div id="child1">1</div>
        <div id="child2">2</div>
    </div>
    

    CSS

    #child2 {
        display:table-header-group;   
    }
    #child1 {
        display:table-footer-group;    
    }
    
    0 讨论(0)
  • 2021-01-15 09:13

    Yes, with flex boxes - http://jsfiddle.net/F8XMk/

    #container{
      display: flex;    
      flex-flow: column;
    }
    
    #child1{
      order: 2;
    }
    
    #child2{
      order: 1;    
    }
    

    Newer browser support for flex is pretty good. You could also hack your way through it with negative margins :)

    0 讨论(0)
  • 2021-01-15 09:14

    You can make it this way:

    <div id="container">
    <div id="child1">Content 1</div>
    <div id="child2">Content 2</div>
    <div id="child3">Content 1</div>
    </div>
    

    Css for Display:

    #child3 {display: none}
    

    Css for Print:

    #child1 {display: none}
    
    0 讨论(0)
  • 2021-01-15 09:30

    No, there is no way to do this in pure CSS. CSS focuses on the presentation part, not the structure of the data that is being displayed.

    The next best thing that comes to mind is having duplicate content:

    <div class="child1 even">...</div>
    <div class="child2 odd">...</div>
    <div class="child1 odd">...</div>
    <div class="child2 even">...</div>
    

    and hiding the odd class in one view, evenin another.

    It's not great, and it may come with SEO side-effects in some situations, I'm not sure.

    It's the only way that comes to mind that doesn't involve server-side processing or JavaScript, though.

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