Flexbox垂直重新排序项目

时间:2018-02-19 16:58:34

标签: html css css3 flexbox vertical-alignment

我一直想弄清楚如何垂直重新排列两个项目。我记得刚才用flexbox做了类似的事情并且已经找到了再次做的方法,但是当我尝试实现flexbox并给项目订单号时没有任何反应。我需要的是.trail-title堆叠在.trail-items以下。

如何让他们使用flexbox垂直切换位置?

这是我创建的JSFidde: https://jsfiddle.net/tb1sv7n8/15/



#flash-breadcrumbs {
  display: flex;
  flex-wrap: wrap;
  -webkit-flex-flow: column wrap;
  flex-flow: column wrap;
  flex-direction: column;
}

.trail-items {
  order: 1;
  height: 1em;
}

.trail-title {
  height: 1em;
}

.breadcrumbs,
.breadcrumbs a {
  color: #544f47;
  font-size: .9em;
  font-weight: normal;
}

<nav id="flash-breadcrumbs" class="breadcrumb-trail breadcrumbs">
  <div class="tg-container">
    <h1 class="trail-title">Title of Article: The Love of This</h1>
    <ul class="trail-items">
      <li class="trail-item"><span>This here is the trail: The Love of This</span></li>
    </ul>
  </div>
</nav>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:2)

flex-direction值从column更改为column-reverse要比为每个弹性项应用不同的order值要简单得多。但这仅适用于flex项目,因此您应将其直接应用于.tg-container。优化了CSS:

#flash-breadcrumbs .tg-container {
  display: flex;
  -webkit-flex-flow: column-reverse wrap;
  flex-flow: column-reverse wrap;
}

.trail-items,
.trail-title {
  height: 1em;
}

.breadcrumbs,
.breadcrumbs a {
  color: #544f47;
  font-size: .9em;
  font-weight: normal;
}
<nav id="flash-breadcrumbs" class="breadcrumb-trail breadcrumbs">
  <div class="tg-container">
    <h1 class="trail-title">Title of Article: The Love of This</h1>
    <ul class="trail-items">
      <li class="trail-item"><span>This here is the trail: The Love of This</span></li>
    </ul>
  </div>
</nav>

答案 1 :(得分:1)

您需要将flex应用于.tg-container div,因为它是两者的父级,并将order属性的值更改为-1 .trail-items div,然后将其置于.trail-title

之上

&#13;
&#13;
.tg-container {
  display: flex;
  /*flex-wrap: wrap; not necessarry*/
  -webkit-flex-flow: column wrap;
  flex-flow: column wrap; /* this is enough */
  /*flex-direction: column; not necessarry*/
}

.trail-items {
  order: -1; /* modified */
  height: 1em;
}

.trail-title {
  height: 1em;
}

.breadcrumbs, .breadcrumbs a {
  color: #544f47;
  font-size: .9em;
  font-weight: normal;
}
&#13;
<nav id="flash-breadcrumbs" class="breadcrumb-trail breadcrumbs">
  <div class="tg-container">
    <h1 class="trail-title">Title of Article: The Love of This</h1>
    <ul class="trail-items">
      <li class="trail-item">
        <span>This here is the trail: The Love of This</span>
      </li>
    </ul>
  </div>
</nav>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

您使用了错误的CSS选择器:Flex容器必须为#flash-breadcrumbs .tg-container - 在您的代码.tg-container中,因为#flash-breadcrumbs的子项是唯一的弹性项...

然后您可以根据需要添加order设置:

#flash-breadcrumbs .tg-container {
  display: flex;
  flex-wrap: wrap;
  -webkit-flex-flow: column wrap;
  flex-flow: column wrap;
  flex-direction: column;
}

.trail-items {
  order: 1;
  height: 1em;
}

.trail-title {
  order: 2;
  height: 1em;
}

.breadcrumbs,
.breadcrumbs a {
  color: #544f47;
  font-size: .9em;
  font-weight: normal;
}
<nav id="flash-breadcrumbs" class="breadcrumb-trail breadcrumbs">
  <div class="tg-container">
    <h1 class="trail-title">Title of Article: The Love of This</h1>
    <ul class="trail-items">
      <li class="trail-item"><span>This here is the trail: The Love of This</span></li>
    </ul>
  </div>
</nav>