代码之家  ›  专栏  ›  技术社区  ›  Ray Lu

CSS布局,使用CSS对div重新排序[重复]

css
  •  32
  • Ray Lu  · 技术社区  · 16 年前

    鉴于HTML

    <div>
       <div id="content1"> content 1</div>
       <div id="content2"> content 2</div>
       <div id="content3"> content 3</div>
    </div>
    

    渲染为

    content 1
    content 2
    content 3
    

    有没有一种方法可以在不更改HTML部分的情况下仅使用CSS来呈现它,如下所示。

    content 1
    content 3
    content 2
    
    9 回复  |  直到 16 年前
        1
  •  29
  •   Luke H    11 年前

    这可以在支持 CSS3 flexbox concept flexbox-order .

    看见 here

    only in current versions of most browsers

    随着时间的推移,flexbox的支持也有所改善。。

        2
  •  25
  •   Marcin Robaszyński    12 年前

    这对我很有用: http://tanalin.com/en/articles/css-block-order/

    HTML

    <div id="example">
      <div id="block-1">First</div>
      <div id="block-2">Second</div>
      <div id="block-3">Third</div>
    </div>
    

    CSS

    #example {display: table; width: 100%; }
    
    #block-1 {display: table-footer-group; } /* Will be displayed at the bottom of the pseudo-table */
    #block-2 {display: table-row-group;    } /* Will be displayed in the middle */
    #block-3 {display: table-header-group; } /* Will be displayed at the top */
    

    如前所述,这应该适用于大多数浏览器。查看链接了解更多信息。

        3
  •  5
  •   Community CDub    8 年前


    CSS positioning div above another div when not in that order in the HTML

    基本上,您必须使用Javascript才能使其在任何方面都可靠。

        4
  •  3
  •   justinbelcher    16 年前

    这是绝对定位的经典用例之一——从源代码顺序更改渲染。但是,您需要知道div的维度才能可靠地做到这一点,如果不知道,javascript是您唯一的求助工具。

        5
  •  3
  •   ndorfin    16 年前

    我在Firefox3中与Firebug混在一起,并得出以下结论:

    <div>
      <div id="content_1" style="height: 40px; width: 40px; background-color: rgb(255, 0, 0); margin-bottom: 40px;">1</div>
      <div id="content_2" style="width: 40px; height: 40px; background-color: rgb(0, 255, 0); float: left;">2</div>
      <div id="content_3" style="width: 40px; height: 40px; background-color: rgb(0, 0, 255); margin-top: -40px;">3</div>
    </div>
    

    这并不完美,因为您需要知道每个容器的高度,并将该高度值应用于最后一个元素的负上边距和第一个元素的下边距。

        6
  •  1
  •   Andrew Hare    16 年前

    我是通过这样做来实现的:

    #content2 { position:relative;top:15px; }
    #content3 { position:relative; top:-17px; }
    

    但请记住,一旦你有了动态内容,这将不会对你起作用。我发布此示例的原因是,如果不了解有关您内容的更多具体信息,我无法给出更好的答案。然而,这种方法应该为您指出使用相对定位的正确方向。

        7
  •  0
  •   Samir Talwar PruthviRaj Reddy    16 年前

    一个词回答:不。看看XSLT(XML样式表语言转换),这是一种专门用于处理XML的语言。

        8
  •  0
  •   Matthew James Taylor    16 年前

    如果您知道每个元素的高度,那么在订单周围交换是垂直相对定位的简单情况。如果你不知道高度,那么你要么给他们高度,如果有任何溢出,允许div获得滚动条,要么用JavaScript计算,并动态添加相对位置。

        9
  •  -3
  •   meem    10 年前

    $('#content2').insertAfter($('#content3'));