简体   繁体   English

仅使用CSS Flexbox控制垂直堆叠元素的顺序

[英]Control order of vertically stacked elements using only CSS Flexbox

I'm trying to use the CSS Flexible Box Layout Module to control the order that elements are rendered. 我正在尝试使用CSS Flexible Box Layout Module来控制元素的呈现顺序。 Here's some sample HTML (and fiddle ): 这是一些示例HTML(和小提琴 ):

<div id='outer-container'>

    <div id='blue-objects' class='object-container'>
        <p>In here we have a number of blue things.</p>
        <p>The amount of content could be very small, or very large.</p>
    </div>

    <div id='green-objects' class='object-container'>
        <p>This is very similar to the blue objects block, in that the amount of content is unknown at design-time, and it could be very small or very large.</p>
        <p>It could be a complicated, nested set of child objects, although in this example there is nothing more than two &lt;p&gt; elements.</p>
    </div>
</div>

/* Started with a columnar layout as given by the Flexplorer: http://bennettfeely.com/flexplorer/ */
.outer-container {    
    display: -webkit-flex;
    display: flex;
    -webkit-flex-flow: column wrap;
    flex-flow: column wrap;

}

.object-container {
    -webkit-flex: 1 auto;
    flex: 1 auto;
    border: 1px dotted black;
    padding: 0 10px;
    margin: 10px;
}

#blue-objects {
    order: 2;
    font-weight: bold;
}

#green-objects {
    order: 1;
    font-style: italic;
}

My goal is to be able to use pure CSS to control the order the #blue-objects and #green-objects elements appear. 我的目标是能够使用纯CSS来控制#blue-objects和#green-objects元素的显示顺序。 All I've been able to accomplish so far is control the horizontal sort order. 到目前为止我所能完成的只是控制水平排序顺序。

The spec describes the order property as follows : 规范描述了order属性如下

A flex container lays out its content in order-modified document order, starting from the lowest numbered ordinal group and going up. Flex容器按顺序修改的文档顺序列出其内容,从最低编号的序数组开始并向上。

I would have thought this would mean that the #green-objects would appear before #blue-objects, because of the order values being 1 and 2, respectively... but it's not working in Chrome 36, Firefox 31, or IE 11. 我原以为这意味着#green-objects会出现在#blue-objects之前,因为order值分别为1和2 ......但它在Chrome 36,Firefox 31或IE 11中不起作用

According to caniuse.com, flexbox has good support in modern browsers. 据caniuse.com称,flexbox在现代浏览器中得到了很好的支持。 What am I doing wrong ? 我做错了什么? How can I control the order of a vertically stacked set of elements using the CSS Flexible Box Layout module? 如何使用CSS Flexible Box Layout模块控制垂直堆叠元素的顺序? Help with this fiddle would be greatly appreciated. 非常感谢帮助这个小提琴。

You should call #outer-container and not .outer-container in CSS file. 你应该在CSS文件中调用#outer-container .outer-container而不是.outer-container Then the flex model will be triggered. 然后将触发flex模型。

To only put blue objects last, then just apply: order:1; 要仅将蓝色对象放在最后,那么只需应用: order:1; to it and nothing to others: DEMO 它对别人没有任何意义: DEMO

Order defaut value is 0. 订单defaut值为0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM