How can I reorder my divs using only CSS?

后端 未结 24 1129
傲寒
傲寒 2020-11-22 01:53

Given a template where the HTML cannot be modified because of other requirements, how is it possible to display (rearrange) a div above another div

相关标签:
24条回答
  • 2020-11-22 01:56

    Well, with a bit of absolute positioning and some dodgy margin setting, I can get close, but it's not perfect or pretty:

    #wrapper { position: relative; margin-top: 4em; }
    #firstDiv { position: absolute; top: 0; width: 100%; }
    #secondDiv { position: absolute; bottom: 0; width: 100%; }
    

    The "margin-top: 4em" is the particularly nasty bit: this margin needs to be adjusted according to the amount of content in the firstDiv. Depending on your exact requirements, this might be possible, but I'm hoping anyway that someone might be able to build on this for a solid solution.

    Eric's comment about JavaScript should probably be pursued.

    0 讨论(0)
  • 2020-11-22 01:59

    If you know, or can enforce the size for the to-be-upper element, you could use

    position : absolute;
    

    In your css and give the divs their position.

    otherwise javascript seems the only way to go:

    fd = document.getElementById( 'firstDiv' );
    sd = document.getElementById( 'secondDiv' );
    fd.parentNode.removeChild( fd );
    sd.parentNode.insertAfter( fd, sd );
    

    or something similar.

    edit: I just found this which might be useful: w3 document css3 move-to

    0 讨论(0)
  • 2020-11-22 02:00

    A CSS-only solution (works for IE10+) – use Flexbox's order property:

    Demo: http://jsfiddle.net/hqya7q6o/596/

    #flex { display: flex; flex-direction: column; }
    #a { order: 2; }
    #b { order: 1; }
    #c { order: 3; }
    <div id="flex">
       <div id="a">A</div>
       <div id="b">B</div>
       <div id="c">C</div>
    </div>

    More info: https://developer.mozilla.org/en-US/docs/Web/CSS/order

    0 讨论(0)
  • 2020-11-22 02:00

    If you just use css, you can use flex.

    .price {
        display: flex;
        align-items: center;
        justify-content: center;
    
        flex-direction: row-reverse; //revert horizontally
        //flex-direction: column-reverse; revert vertically
    }
    <div class="price">
          <div>first block</div>
          <div>second block</div>
    </div>

    0 讨论(0)
  • 2020-11-22 02:00

    CSS really shouldn't be used to restructure the HTML backend. However, it is possible if you know the height of both elements involved and are feeling hackish. Also, text selection will be messed up when going between the divs, but that's because the HTML and CSS order are opposite.

    #firstDiv { position: relative; top: YYYpx; height: XXXpx; }
    #secondDiv { position: relative; top: -XXXpx; height: YYYpx; }
    

    Where XXX and YYY are the heights of firstDiv and secondDiv respectively. This will work with trailing elements, unlike the top answer.

    0 讨论(0)
  • 2020-11-22 02:01

    As others have said, this isn't something you'd want to be doing in CSS. You can fudge it with absolute positioning and strange margins, but it's just not a robust solution. The best option in your case would be to turn to javascript. In jQuery, this is a very simple task:

    $('#secondDiv').insertBefore('#firstDiv');
    

    or more generically:

    $('.swapMe').each(function(i, el) {
        $(el).insertBefore($(el).prev());
    });
    
    0 讨论(0)
提交回复
热议问题