Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
492 views
in Technique[技术] by (71.8m points)

css - Bootstrap change order of columns

How can I change the order of columns for Bootstrap 4's flexbox grid system?

Code I have:

<div class="contents">
  <div class="row row-1">
    <div class="col-sm-6">Content Left</div>
    <div class="col-sm-6">Content Right</div>
  </div>
  <div class="row row-2">
    <div class="col-sm-6">Content Right</div>
    <div class="col-sm-6">Content Left</div>
  </div>
  <div class="row row-3">
    <div class="col-sm-6">Content Left</div>
    <div class="col-sm-6">Content Right</div>
  </div>
  <div class="row row-4">
    <div class="col-sm-6">Content Right</div>
    <div class="col-sm-6">Content Left</div>
  </div>
</div>

I want to set it so that every even row would have it's columns orders reversed. CSS I have so far:

.row:nth-child(2n) .col-sm-6:first-child{
   float:right;
}

JSFiddle: https://jsfiddle.net/bq1L3gax/

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Bootstrap 5 (update 2021)

Since flexbox is still used in Bootstrap 5, changing the order of columns works in the same way. However the order-6 to order-12 have been dropped. These classes are now available for re-ordering in Bootstrap 5..

  • order-{breakpoint}-first
  • order-{breakpoint}-last
  • order-{breakpoint}-0
  • order-{breakpoint}-1
  • order-{breakpoint}-2
  • order-{breakpoint}-3
  • order-{breakpoint}-4
  • order-{breakpoint}-5

The possible {breakpoint} values are none(for xs), sm, md, lg, xl or xxl

Bootstrap 5 order examples


Bootstrap 4 (update 2018)

There's no need for extra CSS. Use the flexbox ordering utils...

Demo: https://codeply.com/go/nEpPysXuNe

<div class="contents">
  <div class="row row-1">
    <div class="col-sm-6">Content Left</div>
    <div class="col-sm-6">Content Right</div>
  </div>
  <div class="row row-2">
    <div class="col-sm-6">Content Right</div>
    <div class="col-sm-6 order-first">Content Left</div>
  </div>
  <div class="row row-3">
    <div class="col-sm-6">Content Left</div>
    <div class="col-sm-6">Content Right</div>
  </div>
  <div class="row row-4">
    <div class="col-sm-6">Content Right</div>
    <div class="col-sm-6 order-first">Content Left</div>
  </div>
</div>

The ordering class are now order-first, order-1, order-2, etc...

https://codeply.com/go/DYHIdw8TXH


Another method is using the flexbox direction utils...

<div class="row flex-row-reverse flex-md-row">
    <div class="col-6">A</div>
    <div class="col-6">B</div>
</div>

https://codeply.com/go/bi01mV3n0n


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...