How can I reorder my divs using only CSS?

后端 未结 24 1169
傲寒
傲寒 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 02:12

    There is absolutely no way to achieve what you want through CSS alone while supporting pre-flexbox user agents (mostly old IE) -- unless:

    1. You know the exact rendered height of each element (if so, you can absolutely position the content). If you're dealing with dynamically generated content, you're out of luck.
    2. You know the exact number of these elements there will be. Again, if you need to do this for several chunks of content that are generated dynamically, you're out of luck, especially if there are more than three or so.

    If the above are true then you can do what you want by absolutely positioning the elements --

    #wrapper { position: relative; }
    #firstDiv { position: absolute; height: 100px; top: 110px; }
    #secondDiv { position: absolute; height: 100px; top: 0; }
    

    Again, if you don't know the height want for at least #firstDiv, there's no way you can do what you want via CSS alone. If any of this content is dynamic, you will have to use javascript.

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

    Negative top margins can achieve this effect, but they would need to be customized for each page. For instance, this markup...

    <div class="product">
    <h2>Greatest Product Ever</h2>
    <p class="desc">This paragraph appears in the source code directly after the heading and will appear in the search results.</p>
    <p class="sidenote">Note: This information appears in HTML after the product description appearing below.</p>
    </div>
    

    ...and this CSS...

    .product { width: 400px; }
    .desc { margin-top: 5em; }
    .sidenote { margin-top: -7em; }
    

    ...would allow you to pull the second paragraph above the first.

    Of course, you'll have to manually tweak your CSS for different description lengths so that the intro paragraph jumps up the appropriate amount, but if you have limited control over the other parts and full control over markup and CSS then this might be an option.

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

    A solution with a bigger browser support then the flexbox (works in IE≥9):

    #wrapper {
      -webkit-transform: scaleY(-1);
      -ms-transform: scaleY(-1);
      transform: scaleY(-1);
    }
    #wrapper > * {
      -webkit-transform: scaleY(-1);
      -ms-transform: scaleY(-1);
      transform: scaleY(-1);
    }
    <div id="wrapper">
        <div id="firstDiv">
            Content to be below in this situation
        </div>
        <div id="secondDiv">
            Content to be above in this situation
        </div>
    </div>
    Other elements

    In contrast to the display: table; solution this solution works when .wrapper has any amount of children.

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

    In your CSS, float the first div by left or right. Float the second div by left or right same as first. Apply clear: left or right the same as the above two divs for the second div.

    For example:

    #firstDiv {
        float: left;
    }
    
    #secondDiv {
        float: left;
        clear: left;
    }
    
    0 讨论(0)
  • 2020-11-22 02:17

    With CSS3 flexbox layout module, you can order divs.

    #wrapper {
      display: flex;
      flex-direction: column;
    }
    #firstDiv {
      order: 2;
    }
    
    <div id="wrapper">
      <div id="firstDiv">
        Content1
      </div>
      <div id="secondDiv">
        Content2
      </div>
    </div>
    
    0 讨论(0)
  • 2020-11-22 02:17

    Just use flex for the parent div by specifying display: flex and flex-direction : column. Then use order to determine which of the child div comes first

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